亞馬遜支付整合
請注意,Amazon Pay API SDK 只能用於對 pay-api.amazon.com|eu|jp 終端節點的 API 呼叫。
如果您需要使用 mws.amazonservices.com|jp 或 mws-eu.amazonservices.com 終端節點進行 Amazon Pay API 調用,則您將需要使用原始 Amazon Pay SDK (PHP)。
使用composer安裝最新版本的SDK及其相依性:
composer require amzn/amazon-pay-api-sdk-php
使用以下測試腳本驗證安裝:
<?php
include ' vendor/autoload.php ' ;
echo " SDK_VERSION= " . Amazon Pay API Client :: SDK_VERSION . "n" ;
?>
先前 MWS 整合中的 MWS 存取金鑰、MWS 秘密金鑰和 MWS 授權令牌不能與此 SDK 一起使用。
您需要產生自己的公鑰/私鑰對才能使用此 SDK 進行 API 呼叫。
在 Windows 10 中,可以使用 ssh-keygen 指令完成此操作:
ssh-keygen -t rsa -b 2048 -f private.pem
ssh-keygen -f private.pem -e -m PKCS8 > public.pub
在 Linux 或 macOS 中,可以使用 openssl 命令完成此操作:
openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -pubout > public.pub
上面的第一行指令產生私鑰,第二行使用私鑰產生公鑰。
若要將金鑰與您的帳戶關聯,請按照此處的說明取得您的公鑰 ID。
該套件的命名空間是 AmazonPayAPI,因此不會與使用 AmazonPay 命名空間的原始 Amazon Pay MWS SDK 發生衝突。
$ amazonpay_config = array (
' public_key_id ' => ' ABC123DEF456XYZ ' , // RSA Public Key ID (this is not the Merchant or Seller ID)
' private_key ' => ' keys/private.pem ' , // Path to RSA Private Key (or a string representation)
' sandbox ' => true , // true (Sandbox) or false (Production) boolean
' region ' => ' us ' , // Must be one of: 'us', 'eu', 'jp'
' algorithm ' => ' AMZN-PAY-RSASSA-PSS-V2 ' // Amazon Signing Algorithm , Optional : uses AMZN - PAY - RSASSA - PSS if not specified
' integrator_id ' => ' AXXXXXXXXXXXXX ' , // (optional) Solution Provider Platform Id in Amz UID Format
' integrator_version ' => ' 1.2.3 ' , // (optional) Solution Provider Plugin Version in Semantic Versioning Format
' platform_version ' => ' 0.0.4 ' // (optional) Solution Provider Platform Version in Semantic Versioning Format
);
如果您已在賣家中心建立環境特定金鑰(即以 LIVE 或 SANDBOX 開頭的公鑰),請使用這些 PublicKeyId 和 PrivateKey。在這種情況下,不需要將 Sandbox 參數傳遞給 ApiConfiguration。
$ amazonpay_config = array (
' public_key_id ' => ' MY_PUBLIC_KEY_ID ' , // LIVE-XXXXX or SANDBOX-XXXXX
' private_key ' => ' keys/private.pem ' , // Path to RSA Private Key (or a string representation)
' region ' => ' us ' , // Must be one of: 'us', 'eu', 'jp'
' algorithm ' => ' AMZN-PAY-RSASSA-PSS-V2 ' // Amazon Signing Algorithm , Optional : uses AMZN - PAY - RSASSA - PSS if not specified
);
如果您想啟用代理程式支持,可以透過以下方式在 $amazonpay_config 中進行設定:
$ amazonpay_config = array (
' public_key_id ' => ' ABC123DEF456XYZ ' , // RSA Public Key ID (this is not the Merchant or Seller ID)
' private_key ' => ' keys/private.pem ' , // Path to RSA Private Key (or a string representation)
' sandbox ' => true , // true (Sandbox) or false (Production) boolean
' region ' => ' us ' , // Must be one of: 'us', 'eu', 'jp'
' algorithm ' => ' AMZN-PAY-RSASSA-PSS-V2 ' , //Amazon Signing Algorithm, Optional: uses AMZN-PAY-RSASSA-PSS if not specified
' integrator_id ' => ' AXXXXXXXXXXXXX ' , // (optional) Solution Provider Platform Id in Amz UID Format
' integrator_version ' => ' 1.2.3 ' , // (optional) Solution Provider Plugin Version in Semantic Versioning Format
' platform_version ' => ' 0.0.4 ' , // (optional) Solution Provider Platform Version in Semantic Versioning Format
' proxy ' => [
' host ' => ' proxy_host ' ,
' port ' => ' proxy_port ' ,
' username ' => ' proxy_username ' ,
' password ' => ' proxy_password ' ,
]
);
pay-api.amazon.com|eu|jp 端點使用版本控制來允許未來更新。此 SDK 的主要版本將與端點的 API 版本保持一致。
如果您已下載此 SDK 的 1.xy 版本,則下列範例中的 $version 將為「v1」。 2.xy 將是“v2”等。
利用內建的便捷功能輕鬆進行 API 呼叫。進一步向下捲動以查看範例程式碼片段。
使用便利功能時,將使用提供的私鑰對請求負載進行簽名,並向正確的區域端點發出 HTTPS 請求。如果發生請求限制,將使用指數退避方法嘗試最多 3 次 HTTPS 呼叫。
使用此 API 向 Amazon Pay 提供貨件追蹤訊息,以便 Amazon Pay 可以在貨件準備發貨和送達時通知 Alexa 上的買家。請參閱 Delivery Trackers API 文件以取得更多資訊。
請注意,您的解決方案提供者帳戶必須與商家帳戶預先存在關係(有效且有效的 MWS 授權令牌)才能使用此功能。
API整合指南
$headers 欄位對於下面的 create/POST 呼叫不是可選的,因為它至少需要 x-amz-pay-idempotency-key 標頭:
$ headers = array ( ' x-amz-pay-idempotency-key ' => uniqid ());
在生產環境中使用店內 API 呼叫之前,請聯絡您的 Amazon Pay 客戶經理以取得店內整合指南的副本。
進行 API 呼叫需要四個快速步驟:
步驟 1. 建構一個客戶端(使用先前定義的配置陣列)。
$ client = new Amazon Pay API Client ( $ amazonpay_config );
步驟 2. 產生有效負載。
$ payload = ' {"scanData":"UKhrmatMeKdlfY6b","scanReferenceId":"0b8fb271-2ae2-49a5-b35d7","merchantCOE":"US","ledgerCurrency":"USD","chargeTotal":{"currencyCode":"USD","amount":"2.00"},"metadata":{"merchantNote":"Merchant Name","communicationContext":{"merchantStoreName":"Store Name","merchantOrderId":"789123"}}} ' ;
步驟 3. 執行呼叫。
$ result = $ client -> instoreMerchantScan ( $ payload );
步驟4.檢查結果。
$result 將會是一個包含以下鍵的陣列:
前兩項(狀態、反應)至關重要。其餘項目在故障排除時很有用。
要解析 PHP 中的回應,可以使用 PHP json_decode() 函數:
$ response = json_decode ( $ result [ ' response ' ], true );
$ id = $ response [ ' chargePermissionId ' ];
如果您是解決方案提供者並需要代表不同的商家帳戶進行 API 調用,則需要將額外的驗證令牌參數傳遞到 API 呼叫中。
$ headers = array ( ' x-amz-pay-authtoken ' => ' other_merchant_super_secret_token ' );
$ result = $ client -> instoreMerchantScan ( $ payload , $ headers );
執行步驟 2 的另一種方法是使用 PHP 陣列並以程式設計方式產生 JSON 有效負載:
$ payload = array (
' scanData ' => ' UKhrmatMeKdlfY6b ' ,
' scanReferenceId ' => uniqid (),
' merchantCOE ' => ' US ' ,
' ledgerCurrency ' => ' USD ' ,
' chargeTotal ' => array (
' currencyCode ' => ' USD ' ,
' amount ' => ' 2.00 '
),
' metadata ' => array (
' merchantNote ' => ' Merchant Name ' ,
' communicationContext ' => array (
' merchantStoreName ' => ' Store Name ' ,
' merchantOrderId ' => ' 789123 '
)
)
);
$ payload = json_encode ( $ payload );
<?php
include ' vendor/autoload.php ' ;
$ amazonpay_config = array (
' public_key_id ' => ' MY_PUBLIC_KEY_ID ' ,
' private_key ' => ' keys/private.pem ' ,
' region ' => ' US ' ,
' sandbox ' => false ,
' algorithm ' => ' AMZN-PAY-RSASSA-PSS-V2 ' ,
);
$ payload = array (
' amazonOrderReferenceId ' => ' P01-0000000-0000000 ' ,
' deliveryDetails ' => array ( array (
' trackingNumber ' => ' 01234567890 ' ,
' carrierCode ' => ' FEDEX '
))
);
try {
$ client = new Amazon Pay API Client ( $ amazonpay_config );
$ result = $ client -> deliveryTrackers ( $ payload );
if ( $ result [ ' status ' ] === 200 ) {
// success
echo $ result [ ' response ' ] . "n" ;
} else {
// check the error
echo ' status= ' . $ result [ ' status ' ] . ' ; response= ' . $ result [ ' response ' ] . "n" ;
}
} catch ( Exception $ e ) {
// handle the exception
echo $ e . "n" ;
}
?>
<?php
session_start ();
include ' vendor/autoload.php ' ;
$ amazonpay_config = array (
' public_key_id ' => ' MY_PUBLIC_KEY_ID ' ,
' private_key ' => ' keys/private.pem ' ,
' region ' => ' US ' ,
' sandbox ' => true ,
' algorithm ' => ' AMZN-PAY-RSASSA-PSS-V2 ' ,
' integrator_id ' => ' AXXXXXXXXXXXXX ' , // (optional) Solution Provider Platform Id in Amz UID Format
' integrator_version ' => ' 1.2.3 ' , // (optional) Solution Provider Plugin Version in Semantic Versioning Format
' platform_version ' => ' 0.0.4 ' // (optional) Solution Provider Platform Version in Semantic Versioning Format
);
$ payload = array (
' webCheckoutDetails ' => array (
' checkoutReviewReturnUrl ' => ' https://localhost/store/checkout_review ' ,
' checkoutResultReturnUrl ' => ' https://localhost/store/checkout_result '
),
' storeId ' => ' amzn1.application-oa2-client.000000000000000000000000000000000 '
);
$ headers = array ( ' x-amz-pay-Idempotency-Key ' => uniqid ());
try {
$ client = new Amazon Pay API Client ( $ amazonpay_config );
$ result = $ client -> createCheckoutSession ( $ payload , $ headers );
header ( " Content-type:application/json; charset=utf-8 " );
echo $ result [ ' response ' ];
if ( $ result [ ' status ' ] !== 201 ) {
http_response_code ( 500 );
}
} catch ( Exception $ e ) {
// handle the exception
echo $ e . "n" ;
http_response_code ( 500 );
}
?>
<?php
include ' vendor/autoload.php ' ;
$ amazonpay_config = array (
' public_key_id ' => ' YOUR_PUBLIC_KEY_ID ' ,
' private_key ' => ' keys/private.pem ' , // Path to RSA Private Key (or a string representation)
' region ' => ' YOUR_REGION_CODE ' ,
' sandbox ' => true ,
' algorithm ' => ' AMZN-PAY-RSASSA-PSS-V2 ' ,
' integrator_id ' => ' AXXXXXXXXXXXXX ' , // (optional) Solution Provider Platform Id in Amz UID Format
' integrator_version ' => ' 1.2.3 ' , // (optional) Solution Provider Plugin Version in Semantic Versioning Format
' platform_version ' => ' 0.0.4 ' // (optional) Solution Provider Platform Version in Semantic Versioning Format
);
$ payload = array (
' webCheckoutDetails ' => array (
' checkoutReviewReturnUrl ' => ' https://localhost/store/checkout_review ' ,
' checkoutResultReturnUrl ' => ' https://localhost/store/checkout_result '
),
' storeId ' => ' amzn1.application-oa2-client.000000000000000000000000000000000 '
);
$ headers = array ( ' x-amz-pay-Idempotency-Key ' => uniqid ());
try {
$ client = new Amazon Pay API Client ( $ amazonpay_config );
$ result = $ client -> createCheckoutSession ( $ payload , $ headers );
if ( $ result [ ' status ' ] === 201 ) {
// created
$ response = json_decode ( $ result [ ' response ' ], true );
$ checkoutSessionId = $ response [ ' checkoutSessionId ' ];
echo " checkoutSessionId= $ checkoutSessionId n" ;
} else {
// check the error
echo ' status= ' . $ result [ ' status ' ] . ' ; response= ' . $ result [ ' response ' ] . "n" ;
}
} catch ( Exception $ e ) {
// handle the exception
echo $ e . "n" ;
}
?>
<?php
include ' vendor/autoload.php ' ;
$ amazonpay_config = array (
' public_key_id ' => ' YOUR_PUBLIC_KEY_ID ' ,
' private_key ' => ' keys/private.pem ' , // Path to RSA Private Key (or a string representation)
' region ' => ' YOUR_REGION_CODE ' ,
' sandbox ' => true ,
' algorithm ' => ' AMZN-PAY-RSASSA-PSS-V2 ' ,
' integrator_id ' => ' AXXXXXXXXXXXXX ' , // (optional) Solution Provider Platform Id in Amz UID Format
' integrator_version ' => ' 1.2.3 ' , // (optional) Solution Provider Plugin Version in Semantic Versioning Format
' platform_version ' => ' 0.0.4 ' // (optional) Solution Provider Platform Version in Semantic Versioning Format
);
try {
$ checkoutSessionId = ' 00000000-0000-0000-0000-000000000000 ' ;
$ client = new Amazon Pay API Client ( $ amazonpay_config );
$ result = $ client -> getCheckoutSession ( $ checkoutSessionId );
if ( $ result [ ' status ' ] === 200 ) {
$ response = json_decode ( $ result [ ' response ' ], true );
$ checkoutSessionState = $ response [ ' statusDetails ' ][ ' state ' ];
$ chargeId = $ response [ ' chargeId ' ];
$ chargePermissionId = $ response [ ' chargePermissionId ' ];
// NOTE: Once Checkout Session moves to a "Completed" state, buyer and shipping
// details must be obtained from the getCharges() function call instead
$ buyerName = $ response [ ' buyer ' ][ ' name ' ];
$ buyerEmail = $ response [ ' buyer ' ][ ' email ' ];
$ shipName = $ response [ ' shippingAddress ' ][ ' name ' ];
$ shipAddrLine1 = $ response [ ' shippingAddress ' ][ ' addressLine1 ' ];
$ shipCity = $ response [ ' shippingAddress ' ][ ' city ' ];
$ shipState = $ response [ ' shippingAddress ' ][ ' stateOrRegion ' ];
$ shipZip = $ response [ ' shippingAddress ' ][ ' postalCode ' ];
$ shipCounty = $ response [ ' shippingAddress ' ][ ' countryCode ' ];
echo " checkoutSessionState= $ checkoutSessionState n" ;
echo " chargeId= $ chargeId ; chargePermissionId= $ chargePermissionId n" ;
echo " buyer= $ buyerName ( $ buyerEmail ) n" ;
echo " shipName= $ shipName n" ;
echo " address= $ shipAddrLine1 ; $ shipCity $ shipState $ shipZip ( $ shipCounty ) n" ;
} else {
// check the error
echo ' status= ' . $ result [ ' status ' ] . ' ; response= ' . $ result [ ' response ' ] . "n" ;
}
} catch ( Exception $ e ) {
// handle the exception
echo $ e . "n" ;
}
?>
<?php
include ' vendor/autoload.php ' ;
$ amazonpay_config = array (
' public_key_id ' => ' YOUR_PUBLIC_KEY_ID ' ,
' private_key ' => ' keys/private.pem ' , // Path to RSA Private Key (or a string representation)
' region ' => ' YOUR_REGION_CODE ' ,
' sandbox ' => true ,
' algorithm ' => ' AMZN-PAY-RSASSA-PSS-V2 ' ,
' integrator_id ' => ' AXXXXXXXXXXXXX ' , // (optional) Solution Provider Platform Id in Amz UID Format
' integrator_version ' => ' 1.2.3 ' , // (optional) Solution Provider Plugin Version in Semantic Versioning Format
' platform_version ' => ' 0.0.4 ' // (optional) Solution Provider Platform Version in Semantic Versioning Format
);
$ payload = array (
' paymentDetails ' =>