nizerin / alipay-global
支付宝国际版第三方SDK,PHP版本的支付宝国际版SDK
1.0.0
2022-03-31 14:43 UTC
Requires
- php: >=5.6.0
- ext-curl: *
- ext-json: *
- ext-openssl: *
README
支付宝国际版A+ SDK
该项目基于 支付宝国际版官方PHP SDK
由于官方SDK主要展示如何访问支付宝网关,但不包含完整的函数,如授权和自动扣款,因此我添加了一些逻辑,并进一步实现了支付宝全球A+的标准化接口
使用
composer require NiZerin/alipay-global
如何使用
初始化
$alipayGlobal = new NiZerin\AliPayGlobal(array( 'client_id' => 'SANDBOX_5Y3A2N2YEB3002022', // Client ID 'endpoint_area' => 'ASIA', // Optional: NORTH_AMERIA / ASIA / EUROPE 'merchantPrivateKey' => '', // Merchant Private Key 'alipayPublicKey' => '', // Alipay Public Key 'is_sandbox' => true, // Whether to use the Sandbox environment ));
必填字段将用 *
标记
在线支付 - 支付 - 支付(收银台支付)
API: ac/ams/payment_cashier
DEMO: pay/cashier
use NiZerin\Model\CustomerBelongsTo; use NiZerin\Model\TerminalType; try { $result = $alipayGlobal->payCashier(array( 'customer_belongs_to' => CustomerBelongsTo::ALIPAY_CN, // * Users pay with Alipay Chinese wallet,Optional: ALIPAY_CN / ALIPAY_HK / TRUEMONEY / TNG / GCASH / DANA / KAKAOPAY / EASYPAISA / BKASH 'notify_url' => '', // Asynchronous callback Url 'return_url' => '', // Synchronize callback Url 'amount' => array( 'currency' => 'USD', // Currency of payment 'value' => '1', // Amount of payment ), 'order' => array( 'id' => null, // Order No 'desc' => 'Order Desc', // Order Description 'extend_info' => array( 'china_extra_trans_info' => array( 'business_type' => 'MEMBERSHIP', // Business Type of Order ), ), ), 'payment_request_id' => null, // Cash payments could be null 'settlement_strategy' => array( 'currency' => 'USD', // Currency used for settlement ), 'terminal_type' => TerminalType::WEB, // * Optional: WEB / WAP / APP 'os_type' => null, // OS System Type )); header('Location: ' . $result->normalUrl); // Return URL of the alipay cashier } catch (Exception $e) { echo $e->getMessage(); // Output Error }
在线支付 - 授权 - 查询
API: ac/ams/authconsult
use NiZerin\Tool\IdTool; use NiZerin\Model\ScopeType; use NiZerin\Model\TerminalType; $auth_state = IdTool::CreateAuthState(); try { $result = $alipayGlobal->authConsult(array( 'customer_belongs_to' => CustomerBelongsTo::ALIPAY_CN, // * Users pay with Alipay Chinese wallet,Optional: ALIPAY_CN / ALIPAY_HK / TRUEMONEY / TNG / GCASH / DANA / KAKAOPAY / EASYPAISA / BKASH 'auth_client_id' => null, // Unique ID of the secondary merchant 'auth_redirect_url' => '', // * URL that User is redirected to after User agrees to authorize 'scopes' => array(ScopeType::AGREEMENT_PAY), // * Optional AGREEMENT_PAY / BASE_USER_INFO / USER_INFO / USER_LOGIN_ID / HASH_LOGIN_ID / SEND_OTP 'auth_state' => $auth_state, // * It will be returned when User agrees to authorize needs to be guaranteed 'terminal_type' => TerminalType::WEB, // * Optional: WEB / WAP / APP 'os_type' => null, // OS System Type )); header('Location: ' . $result->normalUrl); // Return URL of User Authorization page With authCode } catch (Exception $e) { echo $e->getMessage(); // Output Error }
在线支付 - 授权 - 申请Token - 使用AuthCode
use NiZerin\Model\CustomerBelongsTo; use NiZerin\Model\GrantType; $auth_code = $_GET['authCode'] ?? ''; try { $result = $alipayGlobal->authApplyToken(array( 'grant_type' => GrantType::AUTHORIZATION_CODE, // * Value should be AUTHORIZATION_CODE 'customer_belongs_to' => CustomerBelongsTo::ALIPAY_CN, // * Users pay with Alipay Chinese wallet,Optional: ALIPAY_CN / ALIPAY_HK / TRUEMONEY / TNG / GCASH / DANA / KAKAOPAY / EASYPAISA / BKASH 'auth_code' => $auth_code, // * AuthCode get from return URL of User Authorization page 'refresh_token' => null, // Just leave null )); $access_token = $result->accessToken; // Access token is used for Aduto Debit $access_token_expiry_time = $result->accessTokenExpiryTime; // Access token expiry time $refresh_token = $result->refreshToken; // Refresh token is used for update access token $refresh_token_expiry_time = $result->refreshTokenExpiryTime; // Refresh token expiry time session_start(); // Start Session $_SESSION['access_token'] = $access_token; // Store Accesstoken in session } catch (Exception $e) { echo $e->getMessage(); // Output Error }
在线支付 - 授权 - 申请Token - 使用RefreshToken
use NiZerin\Model\CustomerBelongsTo; use NiZerin\Model\GrantType; $refresh_token = $_GET['refreshToken'] ?? ''; try { $result = $alipayGlobal->authApplyToken(array( 'grant_type' => GrantType::REFRESH_TOKEN, // * Value should be REFRESH_TOKEN 'customer_belongs_to' => CustomerBelongsTo::ALIPAY_CN, // * Users pay with Alipay Chinese wallet,Optional: ALIPAY_CN / ALIPAY_HK / TRUEMONEY / TNG / GCASH / DANA / KAKAOPAY / EASYPAISA / BKASH 'auth_code' => null, // Just leave null 'refresh_token' => $refresh_token, // * RefreshToken get from authApplyToken Using AuthCode )); $access_token = $result->accessToken; // Access token is used for Aduto Debit $access_token_expiry_time = $result->accessTokenExpiryTime; // Access token expiry time $refresh_token = $result->refreshToken; // Refresh token is used for update access token $refresh_token_expiry_time = $result->refreshTokenExpiryTime; // Refresh token expiry time session_start(); // Start Session $_SESSION['access_token'] = $result->accessToken; // Store Accesstoken in session } catch (Exception $e) { echo $e->getMessage(); // Output Error }
在线支付 - 支付 - 支付(自动扣款)
try { session_start(); $result = $alipayGlobal->payAgreement(array( 'notify_url' => '', // Asynchronous callback Url 'return_url' => '', // Synchronous callback Url 'amount' => array( 'currency' => 'USD', // Currency of payment 'value' => '1', // Amount of payment ), 'order' => array( 'id' => null, // Order No 'desc' => 'Order Desc', // Order Description 'extend_info' => array( 'china_extra_trans_info' => array( 'business_type' => 'MEMBERSHIP', // Business Type of Order ), ), ), 'goods' => array( array( 'id' => null, // Goods ID 'name' => 'Goods Name', // Goods Name 'category' => null, // Goods Category 'brand' => null, // Goods Brand 'unit_amount' => null, // Goods Charge Unit 'quantity' => null, // Goods Quantity 'sku_name' => null, // Goods SKU Name ), ), 'merchant' => array( // Secondary merchant Info 'MCC' => null, 'name' => null, 'display_name' => null, 'address' => null, 'register_date' => null, 'store' => null, 'type' => null, ), 'buyer' => array( // Buyer Info 'id' => null, // Buyer ID 'name' => array( 'first_name' => 'David', // * Buyer First Name 'last_name' => 'Chen', // * Buyer Last Name ), 'phone_no' => null, // Buyer Phone Number 'email' => null, // Buyer Email ), 'payment_request_id' => null, // Auto Debit payments could be null 'payment_method' => array( 'payment_method_type' => CustomerBelongsTo::ALIPAY_CN, // * Users pay with Alipay Chinese wallet,Optional: ALIPAY_CN / ALIPAY_HK / TRUEMONEY / TNG / GCASH / DANA / KAKAOPAY / EASYPAISA / BKASH 'payment_method_id' => $_SESSION['access_token'], // * AccessToken returned by applyToken ), 'settlement_strategy' => array( 'currency' => 'USD', // Currency used for settlement ), 'terminal_type' => TerminalType::WEB, // * Optional: WEB / WAP / APP 'os_type' => null, // OS Type )); var_dump($result); // Output Result } catch (Exception $e) { echo $e->getMessage(); // Output Error }
在线支付 - 支付 - 支付通知
try { $notify = $alipayGlobal->getNotify(); // Get Asynchronous Payment Notifications // Do something $alipayGlobal->sendNotifyResponseWithRSA(); // Tell Alipay Global the notice has been received and there is no need to send it again } catch (Exception $e) { echo $e->getMessage(); // Output Error }
在线支付 - 授权 - 授权通知
API: ac/ams/notifyauth
try { $notify = $alipayGlobal->getNotify(); // Get Asynchronous Authorization Notifications // Do something $rsqBody = $notify->getRsqBody(); // Get Response Body of Notification $authorization_notify_type = $reqBody->authorizationNotifyType; // Determine Notification Type if ($authorization_notify_type === 'AUTHCODE_CREATED') { // If Notification Type is sent AuthCode $_SESSION['auth_code'] = $reqBody->authCode; // Get AuthCode } $alipayGlobal->sendNotifyResponseWithRSA(); // Tell Alipay Global the notice has been received and there is no need to send it again } catch (Exception $e) { echo $e->getMessage(); // Output Error }
返回URL
/** Return immediately after payment or authorization * After Payment, user will be redirected only * After Authorization, user will be redirected with authCode * Suggestion: The Return URL is only used as a reminder * It's beter to process business in asynchronous payment notification and asynchronous authorization notification **/ echo 'Payment Or Authorization completed';