{"id":22830897,"url":"https://github.com/mantoufan/alipay-global-sdk-php","last_synced_at":"2025-04-04T07:07:22.267Z","repository":{"id":37312567,"uuid":"447497930","full_name":"mantoufan/alipay-global-sdk-php","owner":"mantoufan","description":"Alipay global Third Party SDK with Authorization and Auto debit. Alipay 国际版 A+ 接口第三方 PHP SDK，实现了现金支付、用户授权、自动代扣和 RSA2 加密异步通知应答，示例完整，使用简单","archived":false,"fork":false,"pushed_at":"2022-08-26T15:54:54.000Z","size":85,"stargazers_count":409,"open_issues_count":1,"forks_count":73,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-26T15:50:29.697Z","etag":null,"topics":["alipay","payment","sdk"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mantoufan.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-01-13T07:04:36.000Z","updated_at":"2025-02-11T08:13:07.000Z","dependencies_parsed_at":"2022-09-07T10:40:12.210Z","dependency_job_id":null,"html_url":"https://github.com/mantoufan/alipay-global-sdk-php","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mantoufan%2Falipay-global-sdk-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mantoufan%2Falipay-global-sdk-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mantoufan%2Falipay-global-sdk-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mantoufan%2Falipay-global-sdk-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mantoufan","download_url":"https://codeload.github.com/mantoufan/alipay-global-sdk-php/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247135144,"owners_count":20889421,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["alipay","payment","sdk"],"created_at":"2024-12-12T20:15:55.233Z","updated_at":"2025-04-04T07:07:22.249Z","avatar_url":"https://github.com/mantoufan.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Alipay Global SDK PHP\n## Alipay Global A+ SDK\nThis project is based on [Alipay Global Offical PHP SDK](https://github.com/alipay/global-open-sdk-php)  \nSince official SDK mainly shows how to access the alipay gateway and does not contain complete functions such as authorization and auto debit, I have added some logic and further realized the standard interface of Alipay Global A+\n\n## Attention!\n0.0.3+: `sendNotifyResponse` `sendNotifyResponseWithRSA` are methods of `$notify`, eg:  \n```php\n$notify = $alipayGlobal-\u003egetNotify();\n$notify-\u003esendNotifyResponse(); // $alipayGlobal-\u003esendNotifyResponse() is desprecated\n$notify-\u003esendNotifyResponseWithRSA(); // $alipayGlobal-\u003esendNotifyResponseWithRSA() is desprecated\n/* getNotifyResponse and getNotifyResponseWithRSA are added,\n   so you can process it by yourself in memory frameworks like Webman */\n$notifyResponseWithRSA =  $notify-\u003egetNotifyResponse();\n$notifyResponseWithRSA =  $notify-\u003egetNotifyResponseWithRSA();\n```\n\n## Demo\nThe use and functionality of the SDK have been shown with **Examples** in the project folder\n## Use\n```Shell\ncomposer require mantoufan/alipay-global-sdk-php\n```\n## How to use\n### Initialize\n```php\n$alipayGlobal = new Mantoufan\\AliPayGlobal(array(\n    'client_id' =\u003e 'SANDBOX_5Y3A2N2YEB3002022', // Client ID\n    'endpoint_area' =\u003e 'ASIA', // Optional: NORTH_AMERIA / ASIA / EUROPE\n    'merchantPrivateKey' =\u003e '', // Merchant Private Key\n    'alipayPublicKey' =\u003e '', // Alipay Public Key\n    'is_sandbox' =\u003e true, // Whether to use the Sandbox environment\n));\n```\nRequired fields will be mark with `*`  \n### Online payment - Payment - Pay (Cashier Payment)\nAPI: [ac/ams/payment_cashier](https://global.alipay.com/docs/ac/ams/payment_cashier)  \nDEMO: [pay/cashier](https://p.yzhan.co/alipay-global-sdk-php/example/?type=pay/cashier)\n```php\nuse Mantoufan\\model\\CustomerBelongsTo;\nuse Mantoufan\\model\\TerminalType;\ntry {\n  $result = $alipayGlobal-\u003epayCashier(array(\n      'customer_belongs_to' =\u003e CustomerBelongsTo::ALIPAY_CN, // * Users pay with Alipay Chinese wallet，Optional: ALIPAY_CN / ALIPAY_HK / TRUEMONEY / TNG / GCASH / DANA / KAKAOPAY / EASYPAISA / BKASH\n      'notify_url' =\u003e '', // Asynchronous callback Url\n      'return_url' =\u003e '', // Synchronize callback Url\n      'amount' =\u003e array(\n          'currency' =\u003e 'USD', // Currency of payment\n          'value' =\u003e '1', // Amount of payment\n      ),\n      'order' =\u003e array(\n          'id' =\u003e null, // Order No\n          'desc' =\u003e 'Order Desc', // Order Description\n          'extend_info' =\u003e array(\n              'china_extra_trans_info' =\u003e array(\n                  'business_type' =\u003e 'MEMBERSHIP', // Business Type of Order\n              ),\n          ),\n      ),\n      'payment_request_id' =\u003e null, // Cash payments could be null\n      'settlement_strategy' =\u003e array(\n          'currency' =\u003e 'USD', // Currency used for settlement\n      ),\n      'terminal_type' =\u003e TerminalType::WEB, // * Optional: WEB / WAP / APP\n      'os_type' =\u003e null, // OS System Type\n  ));\n  header('Location: ' . $result-\u003enormalUrl); // Return URL of the alipay cashier\n} catch (Exception $e) {\n  echo $e-\u003egetMessage(); // Output Error\n}\n```\n### Online Payment - Authorization - Consult\nAPI: [ac/ams/authconsult](https://global.alipay.com/docs/ac/ams/authconsult)  \nDEMO: [auth/consult](https://p.yzhan.co/alipay-global-sdk-php/example/?type=auth/consult)\n```php\nuse Mantoufan\\tool\\IdTool;\nuse Mantoufan\\model\\ScopeType;\nuse Mantoufan\\model\\TerminalType;\n$auth_state = IdTool::CreateAuthState();\ntry {\n    $result = $alipayGlobal-\u003eauthConsult(array(\n        'customer_belongs_to' =\u003e CustomerBelongsTo::ALIPAY_CN, // * Users pay with Alipay Chinese wallet，Optional: ALIPAY_CN / ALIPAY_HK / TRUEMONEY / TNG / GCASH / DANA / KAKAOPAY / EASYPAISA / BKASH\n        'auth_client_id' =\u003e null, // Unique ID of the secondary merchant\n        'auth_redirect_url' =\u003e '', // * URL that User is redirected to after User agrees to authorize\n        'scopes' =\u003e array(ScopeType::AGREEMENT_PAY), // * Optional AGREEMENT_PAY / BASE_USER_INFO / USER_INFO / USER_LOGIN_ID / HASH_LOGIN_ID / SEND_OTP\n        'auth_state' =\u003e $auth_state, // * It will be returned when User agrees to authorize needs to be guaranteed\n        'terminal_type' =\u003e TerminalType::WEB, // * Optional: WEB / WAP / APP\n        'os_type' =\u003e null, // OS System Type\n    ));\n    header('Location: ' . $result-\u003enormalUrl); // Return URL of User Authorization page With authCode\n} catch (Exception $e) {\n    echo $e-\u003egetMessage(); // Output Error\n}\n```\n### Online Payment - Authorization - ApplyToken - Using AuthCode\nAPI: [ac/ams/accesstokenapp](https://global.alipay.com/docs/ac/ams/accesstokenapp)  \nDEMO: [auth/apply_token/auth_code](https://p.yzhan.co/alipay-global-sdk-php/example/?type=auth/apply_token/auth_code)\n```php\nuse Mantoufan\\model\\CustomerBelongsTo;\nuse Mantoufan\\model\\GrantType;\n$auth_code = $_GET['authCode'] ?? '';\ntry {\n    $result = $alipayGlobal-\u003eauthApplyToken(array(\n        'grant_type' =\u003e GrantType::AUTHORIZATION_CODE, // * Value should be AUTHORIZATION_CODE\n        'customer_belongs_to' =\u003e CustomerBelongsTo::ALIPAY_CN, // * Users pay with Alipay Chinese wallet，Optional: ALIPAY_CN / ALIPAY_HK / TRUEMONEY / TNG / GCASH / DANA / KAKAOPAY / EASYPAISA / BKASH\n        'auth_code' =\u003e $auth_code, // * AuthCode get from return URL of User Authorization page\n        'refresh_token' =\u003e null, // Just leave null\n    ));\n\n    $access_token = $result-\u003eaccessToken; // Access token is used for Aduto Debit\n    $access_token_expiry_time = $result-\u003eaccessTokenExpiryTime; // Access token expiry time\n    $refresh_token = $result-\u003erefreshToken; // Refresh token is used for update access token\n    $refresh_token_expiry_time = $result-\u003erefreshTokenExpiryTime; // Refresh token expiry time\n    session_start(); // Start Session\n    $_SESSION['access_token'] = $access_token; // Store Accesstoken in session\n} catch (Exception $e) {\n    echo $e-\u003egetMessage(); // Output Error\n}\n```\n### Online Payment - Authorization - ApplyToken - Using RefreshToken\nAPI: [ac/ams/accesstokenapp](https://global.alipay.com/docs/ac/ams/accesstokenapp)  \nDEMO: [auth/apply_token/refresh_token](https://p.yzhan.co/alipay-global-sdk-php/example/?type=auth/apply_token/refresh_token)\n```php\nuse Mantoufan\\model\\CustomerBelongsTo;\nuse Mantoufan\\model\\GrantType;\n$refresh_token = $_GET['refreshToken'] ?? '';\ntry {\n    $result = $alipayGlobal-\u003eauthApplyToken(array(\n        'grant_type' =\u003e GrantType::REFRESH_TOKEN, // * Value should be REFRESH_TOKEN\n        'customer_belongs_to' =\u003e CustomerBelongsTo::ALIPAY_CN, // * Users pay with Alipay Chinese wallet，Optional: ALIPAY_CN / ALIPAY_HK / TRUEMONEY / TNG / GCASH / DANA / KAKAOPAY / EASYPAISA / BKASH\n        'auth_code' =\u003e null, // Just leave null\n        'refresh_token' =\u003e $refresh_token, // * RefreshToken get from authApplyToken Using AuthCode\n    ));\n\n    $access_token = $result-\u003eaccessToken; // Access token is used for Aduto Debit\n    $access_token_expiry_time = $result-\u003eaccessTokenExpiryTime; // Access token expiry time\n    $refresh_token = $result-\u003erefreshToken; // Refresh token is used for update access token\n    $refresh_token_expiry_time = $result-\u003erefreshTokenExpiryTime; // Refresh token expiry time\n    session_start(); // Start Session\n    $_SESSION['access_token'] = $result-\u003eaccessToken; // Store Accesstoken in session\n} catch (Exception $e) {\n    echo $e-\u003egetMessage(); // Output Error\n}\n```\n### Online payment - Payment - Pay (Auto Debit)\nAPI: [ac/ams/payment_agreement](https://global.alipay.com/docs/ac/ams/payment_agreement)  \nDEMO: [pay/agreement](https://p.yzhan.co/alipay-global-sdk-php/example/?type=pay/agreement)\n```php\ntry {\n    session_start();\n    $result = $alipayGlobal-\u003epayAgreement(array(\n        'notify_url' =\u003e '', // Asynchronous callback Url\n        'return_url' =\u003e '', // Synchronous callback Url\n        'amount' =\u003e array(\n            'currency' =\u003e 'USD', // Currency of payment\n            'value' =\u003e '1', // Amount of payment\n        ),\n        'order' =\u003e array(\n            'id' =\u003e null, // Order No\n            'desc' =\u003e 'Order Desc', // Order Description\n            'extend_info' =\u003e array(\n                'china_extra_trans_info' =\u003e array(\n                    'business_type' =\u003e 'MEMBERSHIP', // Business Type of Order\n                ),\n            ),\n        ),\n        'goods' =\u003e array(\n            array(\n                'id' =\u003e null, // Goods ID\n                'name' =\u003e 'Goods Name', // Goods Name\n                'category' =\u003e null, // Goods Category\n                'brand' =\u003e null, // Goods Brand\n                'unit_amount' =\u003e null, // Goods Charge Unit\n                'quantity' =\u003e null, // Goods Quantity\n                'sku_name' =\u003e null, // Goods SKU Name\n            ),\n        ),\n        'merchant' =\u003e array( // Secondary merchant Info\n            'MCC' =\u003e null,\n            'name' =\u003e null,\n            'display_name' =\u003e null,\n            'address' =\u003e null,\n            'register_date' =\u003e null,\n            'store' =\u003e null,\n            'type' =\u003e null,\n        ),\n        'buyer' =\u003e array( // Buyer Info\n            'id' =\u003e null, // Buyer ID\n            'name' =\u003e array(\n                'first_name' =\u003e 'David', // * Buyer First Name\n                'last_name' =\u003e 'Chen', // * Buyer Last Name\n            ),\n            'phone_no' =\u003e null, // Buyer Phone Number\n            'email' =\u003e null, // Buyer Email\n        ),\n        'payment_request_id' =\u003e null, // Auto Debit payments could be null\n        'payment_method' =\u003e array(\n            'payment_method_type' =\u003e CustomerBelongsTo::ALIPAY_CN, // * Users pay with Alipay Chinese wallet，Optional: ALIPAY_CN / ALIPAY_HK / TRUEMONEY / TNG / GCASH / DANA / KAKAOPAY / EASYPAISA / BKASH\n            'payment_method_id' =\u003e $_SESSION['access_token'], // * AccessToken returned by applyToken\n        ),\n        'settlement_strategy' =\u003e array(\n            'currency' =\u003e 'USD', // Currency used for settlement\n        ),\n        'terminal_type' =\u003e TerminalType::WEB, // * Optional: WEB / WAP / APP\n        'os_type' =\u003e null, // OS Type\n    ));\n    var_dump($result); // Output Result\n} catch (Exception $e) {\n    echo $e-\u003egetMessage(); // Output Error\n}\n```\n### Online payment - Payment - NotifyPayment\nAPI: [ac/ams/paymentrn_online](https://global.alipay.com/docs/ac/ams/paymentrn_online)  \nDEMO: [notify](https://p.yzhan.co/alipay-global-sdk-php/example/?type=notify)\n```php\ntry {\n    /* Get Asynchronous Payment Notifications */\n    $notify = $alipayGlobal-\u003egetNotify();\n    /* Default Value same as:\n    $notify = $alipayGlobal-\u003egetNotify(array(\n        'httpMethod' =\u003e $_SERVER['REQUEST_METHOD'],\n        'path' =\u003e $_SERVER['REQUEST_URI'],\n        'clientId' =\u003e $_SERVER['HTTP_CLIENT_ID'],\n        'rsqTime' =\u003e $_SERVER['HTTP_REQUEST_TIME'],\n        'rsqBody' =\u003e file_get_contents('php://input'),\n        'signature' =\u003e $_SERVER['HTTP_SIGNATURE']\n    ));*/\n    /* Webman Example:\n    $notify = $alipayGlobal-\u003egetNotify(array(\n        'httpMethod' =\u003e $request-\u003emethod(),\n        'path' =\u003e $request-\u003euri(),\n        'clientId' =\u003e $request-\u003eheader('client-id'),\n        'rsqTime' =\u003e $request-\u003eheader('request-time'),\n        'rsqBody' =\u003e $request-\u003erawBody(),\n        'signature' =\u003e $request-\u003eheader('signature')\n    ));*/\n\n    // Do something\n\n    // Method 1: use header () and echo response\n    $notify-\u003esendNotifyResponseWithRSA(); // Tell Alipay Global the notice has been received and there is no need to send it again\n\n    // Method 2: Or Get headers and body, process it by yourself\n    $notifyResponseWithRSA =  $notify-\u003egetNotifyResponseWithRSA();\n    // Webman Example:\n    // response($notifyResponseWithRSA['body'], 200, $notifyResponseWithRSA['headers']);\n} catch (Exception $e) {\n    echo $e-\u003egetMessage(); // Output Error\n}\n```\n### Online Payment - Authorization - NotifyAuthorization\nAPI: [ac/ams/notifyauth](https://global.alipay.com/docs/ac/ams/notifyauth)  \nDEMO: [notify/auth/auth_code](https://p.yzhan.co/alipay-global-sdk-php/example/?type=notify/auth/auth_code)\n```php\ntry {\n    /* Get Asynchronous Payment Notifications */\n    $notify = $alipayGlobal-\u003egetNotify();\n    /* Default Value same as:\n    $notify = $alipayGlobal-\u003egetNotify(array(\n        'httpMethod' =\u003e $_SERVER['REQUEST_METHOD'],\n        'path' =\u003e $_SERVER['REQUEST_URI'],\n        'clientId' =\u003e $_SERVER['HTTP_CLIENT_ID'],\n        'rsqTime' =\u003e $_SERVER['HTTP_REQUEST_TIME'],\n        'rsqBody' =\u003e file_get_contents('php://input'),\n        'signature' =\u003e $_SERVER['HTTP_SIGNATURE']\n    ));*/\n    /* Webman Example:\n    $notify = $alipayGlobal-\u003egetNotify(array(\n        'httpMethod' =\u003e $request-\u003emethod(),\n        'path' =\u003e $request-\u003euri(),\n        'clientId' =\u003e $request-\u003eheader('client-id'),\n        'rsqTime' =\u003e $request-\u003eheader('request-time'),\n        'rsqBody' =\u003e $request-\u003erawBody(),\n        'signature' =\u003e $request-\u003eheader('signature')\n    ));*/\n\n    // Do something\n\n    $rsqBody = $notify-\u003egetRsqBody(); // Get Response Body of Notification\n    $authorization_notify_type = $reqBody-\u003eauthorizationNotifyType; // Determine Notification Type\n    if ($authorization_notify_type === 'AUTHCODE_CREATED') { // If Notification Type is sent AuthCode\n        $_SESSION['auth_code'] = $reqBody-\u003eauthCode; // Get AuthCode\n    }\n\n    // Method 1: use header () and echo response\n    $notify-\u003esendNotifyResponseWithRSA(); // Tell Alipay Global the notice has been received and there is no need to send it again\n\n    // Method 2: Or Get headers and body, process it by yourself\n    $notifyResponseWithRSA =  $notify-\u003egetNotifyResponseWithRSA();\n    // Webman Example:\n    // response($notifyResponseWithRSA['body'], 200, $notifyResponseWithRSA['headers']);\n} catch (Exception $e) {\n    echo $e-\u003egetMessage(); // Output Error\n}\n```\n### Online Payment - Refund - Refund\nAPI: [ac/ams/refund_online](https://global.alipay.com/docs/ac/ams/refund_online)  \nDEMO: [refund/refund_online](https://p.yzhan.co/alipay-global-sdk-php/example/?type=refund/refund_online)  \n```php\ntry {\n    $result = $alipayGlobal-\u003esendRefund(array(\n        'paymentId' =\u003e '20181129190741010007000000XXXX', // Unique ID assigned by Alipay for the original payment to be refunded.\n        'refundRequestId' =\u003e 'S7mMoYxQxWjJDWwm2NG4WxmNbM5z3GvSB6PEPvMeYP21PQUtrX9hXlgbQMajt2on', // Unique ID assigned by the merchant to identify a refund request.\n        'refundAmount' =\u003e array(\n            'currency' =\u003e 'USD', // Currency of refund\n            'value' =\u003e '100', // Amount of refund\n        )\n    ));\n    var_dump($result);\n} catch (Exception $e) {\n    echo $e-\u003egetMessage();\n}\n```\n### Return Url\nDEMO: [return](https://p.yzhan.co/alipay-global-sdk-php/example/?type=return)\n```php\n/** Return immediately after payment or authorization\n * After Payment, user will be redirected only\n * After Authorization, user will be redirected with authCode\n * Suggestion: The Return URL is only used as a reminder\n * It's beter to process business in asynchronous payment notification and asynchronous authorization notification\n**/\necho 'Payment Or Authorization completed';\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmantoufan%2Falipay-global-sdk-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmantoufan%2Falipay-global-sdk-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmantoufan%2Falipay-global-sdk-php/lists"}