rkr / amazon-pay-sdk-php
Amazon Pay SDK (PHP)
Requires
- php: >=5.5.0
- ext-curl: *
This package is auto-updated.
Last update: 2024-08-29 04:14:40 UTC
README
Amazon Pay API 集成
要求
PHP 5.3 和 5.4 的支持正在被弃用。SDK 在这些旧环境中将正常工作,但未来的版本可能不支持。我们鼓励商家尽早升级到更新的 PHP 版本。
文档
示例
- 查看以下示例集成演示 此处
快速入门
实例化客户端:客户端接受以下格式的参数
- 关联数组
- 包含配置信息的 JSON 文件的路径。
使用 Composer 安装
composer require amzn/amazon-pay-sdk-php
目录树
.
├── composer.json - Configuration for composer
├── LICENSE.txt
├── NOTICE.txt
├── AmazonPay
│ ├── Client.php - Main class with the API calls
│ ├── ClientInterface.php - Shows the public function definitions in Client.php
│ ├── HttpCurl.php - Client class uses this file to execute the GET/POST
│ ├── HttpCurlInterface.php - Shows the public function definitions in the HttpCurl.php
│ ├── IpnHandler.php - Class handles verification of the IPN
│ ├── IpnHandlerInterface.php - Shows the public function definitions in the IpnHandler.php
│ ├── Regions.php - Defines the regions that is supported
│ ├── ResponseParser.php - Parses the API call response
│ └── ResponseInterface.php - Shows the public function definitions in the ResponseParser.php
├── README.md
└── UnitTests
├── ClientTest.php
├── config.json
├── coverage.txt
├── IpnHandlerTest.php
└── Signature.php
参数列表
必填参数
可选参数
设置配置
您的 Amazon Pay 密钥可在您的卖家中心账户中找到
在实例化客户端对象时设置配置
<?php namespace AmazonPay; require_once 'Client.php'; // or, instead of using require_once, you can use the phar file instead // include 'amazon-pay.phar'; // PHP Associative array $config = array( 'merchant_id' => 'YOUR_MERCHANT_ID', 'access_key' => 'YOUR_ACCESS_KEY', 'secret_key' => 'YOUR_SECRET_KEY', 'client_id' => 'YOUR_LOGIN_WITH_AMAZON_CLIENT_ID', 'region' => 'REGION'); // or, instead of setting the array in the code, you can // initialze the Client by specifying a JSON file // $config = 'PATH_TO_JSON_FILE'; // Instantiate the client class with the config type $client = new Client($config);
在沙盒模式下测试
如果未指定,沙盒参数默认为 false
<?php namespace AmazonPay; $config = array( 'merchant_id' => 'YOUR_MERCHANT_ID', 'access_key' => 'YOUR_ACCESS_KEY', 'secret_key' => 'YOUR_SECRET_KEY', 'client_id' => 'YOUR_LOGIN_WITH_AMAZON_CLIENT_ID', 'region' => 'REGION', 'sandbox' => true); $client = new Client($config); // Also you can set the sandbox variable in the config() array of the Client class by $client->setSandbox(true);
设置代理值
可以在实例化客户端对象后通过以下设置器设置代理参数
$proxy = array(); $proxy['proxy_user_host'] // Hostname for the proxy $proxy['proxy_user_port'] // Hostname for the proxy $proxy['proxy_user_name'] // If your proxy requires a username $proxy['proxy_user_password'] // If your proxy requires a password $client->setProxy($proxy);
进行 API 调用
以下是如何进行 GetOrderReferenceDetails API 调用的示例
<?php namespace AmazonPay; $requestParameters = array(); // AMAZON_ORDER_REFERENCE_ID is obtained from the Amazon Pay Address/Wallet widgets // ACCESS_TOKEN is obtained from the GET parameter from the URL. // Required Parameter $requestParameters['amazon_order_reference_id'] = 'AMAZON_ORDER_REFERENCE_ID'; // Optional Parameter $requestParameters['address_consent_token'] = 'ACCESS_TOKEN'; $requestParameters['mws_auth_token'] = 'MWS_AUTH_TOKEN'; $response = $client->getOrderReferenceDetails($requestParameters);
请参阅 API 响应 部分,了解解析 API 响应的信息。
IPN 处理
- 要成功接收 IPN,您需要在您的域上安装有效的 SSL。
- 您可以通过设置通知端点来设置您的卖家中心中的集成设置页面,在设置选项卡中访问集成设置页面。
- IpnHandler.php 类处理 IPN 的源和数据验证
将以下代码添加到任何文件中,并通过在设置选项卡中访问集成设置页面将文件的 URL 设置为商户/集成商 URL。
<?php namespace AmazonPay; require_once 'IpnHandler.php'; // Get the IPN headers and Message body $headers = getallheaders(); $body = file_get_contents('php://input'); // Create an object($ipnHandler) of the IpnHandler class $ipnHandler = new IpnHandler($headers, $body);
请参阅 IPN 响应 部分,了解解析 IPN 响应的信息。
便利方法
收费方法
收费方法结合以下 API 调用
标准支付/周期性支付
- SetOrderReferenceDetails / SetBillingAgreementDetails
- ConfirmOrderReference / ConfirmBillingAgreement
- Authorize / AuthorizeOnBillingAgreement
对于 标准支付,第一个 charge
调用将进行 SetOrderReferenceDetails、ConfirmOrderReference、Authorize API 调用。对于相同订单引用 ID 的后续 charge
方法调用将只进行授权。
对于 周期性支付,第一个 charge
调用将进行 SetBillingAgreementDetails、ConfirmBillingAgreement、AuthorizeOnBillingAgreement API 调用。对于相同账单协议 ID 的后续 charge
方法调用将只进行 AuthorizeOnBillingAgreement 调用。
立即捕获可以设置为
true
以用于数字商品。对于实物商品,强烈建议将“立即捕获”设置为false
,并在货物发出后通过调用capture
API来捕获金额。
// Create an array that will contain the parameters for the charge API call $requestParameters = array(); // Adding the parameters values to the respective keys in the array $requestParameters['amazon_reference_id'] = 'AMAZON_REFERENCE_ID'; // Or // If $requestParameters['amazon_reference_id'] is not provided, // either one of the following ID input is needed $requestParameters['amazon_order_reference_id'] = 'AMAZON_ORDER_REFERENCE_ID'; $requestParameters['amazon_billing_agreement_id'] = 'AMAZON_BILLING_AGREEMENT_ID'; $requestParameters['seller_id'] = null; $requestParameters['charge_amount'] = '100.50'; $requestParameters['currency_code'] = 'USD'; $requestParameters['authorization_reference_id'] = 'UNIQUE STRING'; $requestParameters['transaction_timeout'] = 0; $requestParameters['capture_now'] = false; //true for Digital goods $requestParameters['charge_note'] = 'Example item note'; $requestParameters['charge_order_id'] = '1234-Example-Order'; $requestParameters['store_name'] = 'Example Store'; $requestParameters['platform_Id'] = null; $requestParameters['custom_information'] = 'Any_Custom_String'; $requestParameters['mws_auth_token'] = null; // Get the Authorization response from the charge method $response = $client->charge($requestParameters);
请参阅 API 响应 部分,了解解析 API 响应的信息。
获取配置文件信息(getUserInfo方法)
- 使用按钮小部件返回的访问令牌从亚马逊获取用户的配置文件信息。
- 当用户登录网站时,授权服务器会颁发访问令牌。
- 访问令牌是针对客户端、用户和访问范围的。客户端必须使用访问令牌来检索客户配置文件数据。
<?php namespace AmazonPay; // config array parameters that need to be instantiated $config = array( 'client_id' => 'YOUR_LWA_CLIENT_ID', 'region' => 'REGION'); $client = new Client($config); // Client ID can also be set using the setter function setClientId($client_id) $client->setClientId(‘YOUR_LWA_CLIENT_ID’); // Get the Access Token from the URL $access_token = 'ACCESS_TOKEN'; // Calling the function getUserInfo with the access token parameter returns object $userInfo = $client->getUserInfo($access_token); // Buyer name $userInfo['name']; // Buyer Email $userInfo['email']; // Buyer User Id $userInfo['user_id'];
响应解析
响应以3种格式提供
- XML/原始响应
- 关联数组
- JSON格式
API响应
// Returns an object($response) of the class ResponseParser.php $response = $client->getOrderReferenceDetails($requestParameters); // XML response $response->toXml(); // Associative array response $response->toArray(); // JSON response $response->toJson();
IPN响应
$ipnHandler = new IpnHandler($headers, $body); // Raw message response $ipnHandler->returnMessage(); // Associative array response $ipnHandler->toArray(); // JSON response $ipnHandler->toJson();
日志记录
SDK对清洗后的请求和响应进行日志记录,可以与任何PSR-3兼容的日志记录器(如Monolog)一起工作。
API响应
namespace AmazonPay; require 'vendor/autoload.php'; include 'amazon-pay.phar'; use Monolog\Logger; use Monolog\Handler\StreamHandler; date_default_timezone_set('America/Los_Angeles'); $log = new Logger('TestSDK'); $log->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG)); $client = new Client('us.config'); $client->setLogger($log); $response = $client->getServiceStatus();