亚马逊支付整合
请注意,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 ' =>