alma php client
v2.4.0
이것은 ALMA의 공식 PHP API 클라이언트입니다.
이 PHP API 클라이언트는 수천 개의 전자 상거래 웹 사이트에서 제작에 사용되고 있으며 전체 플러드 통합을 구축하는 데 필요한 엔드 포인트를 제공합니다. 그러나 아직 여기에 문서화 된대로 전체 ALMA API를 구현하지는 않습니다. 아직 구현되지 않은 일부 엔드 포인트를 사용해야한다면 자유롭게 연락하십시오! (또는 더 나은, PR을 제출하십시오 :))
ALMA PHP API 클라이언트 라이브러리는 최근에 지원되는 모든 PHP 버전에 대해 테스트되었습니다. 현대적이고 지원되는 PHP 버전을 적극 권장합니다.
일반적으로 작곡가를 통해이 패키지를 설치합니다.
composer require alma/alma-php-client
alma-php-client.zip 파일을 잡으십시오. require_once " path/to/alma-php-client/vendor/autoload.php " ;API 클라이언트 사용의 예. (자세한 내용은 API 문서 확인)
$ alma = new Alma API Client ( $ apiKey , [ ' mode ' => Alma API Client:: TEST_MODE ]); // ...
$ amountInCents = 150000 ; // 1500 euros
$ customerBillingCountry = "" ; // can be an empty string but NOT null
$ customerShippingCountry = " FR " ; // billing_address has priority over shipping_address (if not empty)
try {
$ eligibilities = $ alma -> payments -> eligibility (
[
' purchase_amount ' => $ amountInCents ,
' billing_address ' => [ // (optional) useful to check eligibility for a specific billing country
' country ' => $ customerBillingCountry // can be an empty string but not null
],
' shipping_address ' => [ // (optional) useful to check eligibility for a specific shipping country
' country ' => $ customerShippingCountry
],
' queries ' =>
[
[
' installments_count ' => 1 ,
' deferred_days ' => 30 ,
],
[
' installments_count ' => 2 ,
],
[
' installments_count ' => 3 ,
],
[
' installments_count ' => 4 ,
],
[
' installments_count ' => 10 ,
],
],
],
$ raiseOnError = true // throws an exception on 4xx or 5xx http return code
// instead of just returning an Eligibility Object with isEligible() === false
);
} catch ( Alma API RequestError $ error ) {
header ( " HTTP/1.1 500 Internal Server Error " );
die ( $ error -> getMessage ());
}
foreach ( $ eligibilities as $ eligibility ) {
if (! $ eligibility -> isEligible ()) {
die ( ' cart is not eligible ' );
}
}
// ... // ...
echo " <form> " ;
echo " <h2>Available feePlans are:</h2> " ;
foreach ( $ alma -> merchants -> feePlans ( $ kind = FeePlan:: KIND_GENERAL , $ installmentsCounts = " all " , $ includeDeferred = true ) as $ feePlan ) {
if (! $ feePlan -> allowed ) {
continue ;
}
printf ( ' <label for="%s">Pay in %s by %s installments count</label> ' , $ feePlan -> getPlanKey (), $ feePlan -> getDeferredDays (), $ feePlan -> getInstallmentsCount ());
printf ( ' <input id="radio-%s" type="radio" name="fee-plan" value="%s"> ' , $ feePlan -> getPlanKey (), $ feePlan -> getPlanKey ());
}
echo " <button type= " submit " >Submit</button> " ;
echo " </form> " ;
// ...이 작업을 수행하기 위해 적격을 사용하는 것을 선호 할 수 있지만 코드 의이 부분을 사용하면 Feeplans 정의에 더 익숙해 질 수 있습니다.
// ...
function formatMoney ( int $ amount ) {
return sprintf ( " %.2f %s " , round ( intval ( $ amount ) / 100 , 2 ), " € " );
}
function formatPercent ( int $ amount ) {
return sprintf ( " %.2f %s " , round ( intval ( $ amount ) / 100 , 2 ), " % " );
}
// ...
foreach ( $ eligibilities as $ eligibility ) {
// display following payment plan (or not eligible message) on feePlan selection using javascript.
printf ( ' <div id="table-%s"> ' , $ eligibility -> getPlanKey ());
if (! $ eligibility -> isEligible ()) {
echo " This fee plan is not eligible! " ;
echo " </div> " ;
continue ;
}
if (! $ paymentPlans = $ eligibility -> getPaymentPlan ()) {
echo " No payment plan found for current eligibility! (that should not happen) " ;
echo " </div> " ;
continue ;
}
echo " <ul> " ;
foreach ( $ paymentPlans as $ paymentPlan ) {
$ planDefinition = sprintf (
" <li>You will pay %s on %s including %s fees & %s of interest</li> " ,
formatMoney ( $ paymentPlan [ ' total_amount ' ]),
( new DateTime ())-> setTimestamp ( $ paymentPlan [ ' due_date ' ])-> format ( ' Y-m-d ' ),
formatMoney ( $ paymentPlan [ ' customer_fee ' ]),
formatMoney ( $ paymentPlan [ ' customer_interest ' ])
);
}
echo " </ul> " ;
echo " <div> " ;
echo " Annual Interest Rate: " . formatPercent ( $ eligibility -> getAnnualInterestRate ()) . " <br> " ;
echo " Order Amount: " . formatMoney ( $ amountInCents );
echo " Total Cost Amount: " . formatMoney ( $ eligibility -> getCustomerTotalCostAmount ());
echo " </div> " ;
echo " </div> " ;
}
// ... // ...
$ payment = $ alma -> payments -> createPayment (
[
' origin ' => ' online ' ,
' payment ' =>
[
' return_url ' => ' <where_the_customer_will_be_redirect_after_alma_checkout> ' ,
' ipn_callback_url ' => ' <your_ipn_callback_url> ' ,
' purchase_amount ' => 150000 ,
' installments_count ' => 4 ,
' custom_data ' =>
[
' my_very_important_key ' => ' <the_context_custom_value> ' ,
],
' locale ' => ' fr ' ,
' billing_address ' =>
[
' first_name ' => ' John ' ,
' last_name ' => ' Doe ' ,
' email ' => ' [email protected] ' ,
' line1 ' => ' 1 rue de Rome ' ,
' postal_code ' => ' 75001 ' ,
' city ' => ' Paris ' ,
' country ' => ' FR ' ,
],
' shipping_address ' =>
[
' first_name ' => ' John ' ,
' last_name ' => ' Doe ' ,
' email ' => ' [email protected] ' ,
' line1 ' => ' 1 rue de Rome ' ,
' postal_code ' => ' 75001 ' ,
' city ' => ' Paris ' ,
' country ' => ' FR ' ,
],
],
' customer ' =>
[
' first_name ' => ' John ' ,
' last_name ' => ' Doe ' ,
' email ' => ' [email protected] ' ,
' phone ' => ' 06 12 34 56 78 ' ,
' addresses ' =>
[
[
' first_name ' => ' John ' ,
' last_name ' => ' Doe ' ,
' email ' => ' [email protected] ' ,
' phone ' => ' 06 12 34 56 78 ' ,
],
],
],
]
);
// store $payment->id and link it to the customer order here ;)
header ( ' Location: ' . $ payment -> url );
exit ();
// ...(지불 생성에 대해 제공되거나 Alma 대시 보드에서 정적으로 정의 될 수 있음)
// ...
if (! isset ( $ _GET [ ' pid ' ]) || empty ( $ _GET [ ' pid ' ])) {
header ( " HTTP/1.1 400 Bad Request " );
die ();
}
// retrieve your local order by payment id
$ order = getOrderByPaymentId ( $ _GET [ ' pid ' ])
if (! $ order ) {
header ( " HTTP/1.1 404 Not Found " );
die ();
}
// check $payment->state & do the order / customer stuff you want here :D
header ( " HTTP/1.1 200 " );
exit ();
// ... // ...
$ payment = $ alma -> payments -> fetch ( $ paymentId );
switch ( $ payment -> state ) {
case Alma API Entities Payment:: STATE_IN_PROGRESS : break ;
case Alma API Entities Payment:: STATE_PAID : break ;
}
// ... Alma PHP 클라이언트는 MIT 라이센스에 따라 배포됩니다.