atdev / viva-php
Viva Wallet 本地结账 V2 API PHP 封装库
1.0.4
2022-03-02 13:11 UTC
Requires
- php: >=5.6.0
- egulias/email-validator: >=2.0
- guzzlehttp/guzzle: ^6.0 || ^7.0
Requires (Dev)
- codeception/aspect-mock: ^4.0
- phpunit/phpunit: ^9.0
README
这是一个 Viva Wallet 本地结账 V2 API 的封装库: https://developer.vivawallet.com/native-checkout-v2/
如何使用
此库通过 Composer 安装。您需要添加 atdev/viva-php
依赖。
composer require atdev/viva-php:~1.0
先决条件
请从 https://developer.vivawallet.com/native-checkout-v2/ 完成所有先决条件步骤,并获取您的 Client ID
和 Client Secret
。您需要设置一个以本地结账 V2 作为集成方法的支付源,并获取 源代码
。
获取卡费率令牌
如在此处所述,在前端创建支付表单和 Charge Token
: https://developer.vivawallet.com/native-checkout-v2/ 您需要在前端拥有 访问令牌
和 基本 URL
,您可以通过以下方式获取它们
$baseUrl = \ATDev\Viva\Transaction\Url::getUrl("[Test Mode]"); // Test mode, default is false $accessToken = (new \ATDev\Viva\Account\Authorization()) ->setClientId("[Client ID]") // Client ID, Provided by wallet ->setClientSecret("[Client Secret]") // Client Secret, Provided by wallet ->setTestMode("[Test Mode]") // Test mode, default is false, can be skipped ->getAccessToken();
现在,当您拥有 Charge Token
时,您可以进行实际交易。
交易
CHARGE
$customer = (new \ATDev\Viva\Transaction\Customer()) ->setEmail("[Customer Email]") ->setPhone("[Customer Phone]") ->setFullName("[Customer Full Name]"); $transaction = (new ATDev\Viva\Transaction\Charge()) ->setClientId("[Client ID]") // Client ID, Provided by wallet ->setClientSecret("[Client Secret]") // Client Secret, Provided by wallet ->setTestMode("[Test Mode]") // Test mode, default is false, can be skipped ->setSourceCode("[Source Code]") // Source code, provided by wallet ->setAmount((int) "[Amount]") // The amount to charge in currency's smallest denomination (e.g amount in pounds x 100) ->setInstallments((int) "[Installments]") // Installments, can be skipped if not used ->setChargeToken("[Charge Token]") // Charge token obtained at front end ->setCustomer($customer); $result = $transaction->send(); if (!empty($transaction->getError())) { // Log the error message // $error = $transaction->getError(); } else { // Save transaction id // $transactionId = $result->transactionId; }
AUTHORIZATION
$customer = (new \ATDev\Viva\Transaction\Customer()) ->setEmail("[Customer Email]") ->setPhone("[Customer Phone]") ->setFullName("[Customer Full Name]"); $transaction = (new ATDev\Viva\Transaction\Authorization()) ->setClientId("[Client ID]") // Client ID, Provided by wallet ->setClientSecret("[Client Secret]") // Client Secret, Provided by wallet ->setTestMode("[Test Mode]") // Test mode, default is false, can be skipped ->setSourceCode("[Source Code]") // Source code, provided by wallet ->setAmount((int) "[Amount]") // The amount to pre-auth in currency's smallest denomination (e.g amount in pounds x 100) ->setInstallments((int) "[Installments]") // Installments, can be skipped if not used ->setChargeToken("[Charge Token]") // Charge token obtained at front end ->setCustomer($customer); $result = $transaction->send(); if (!empty($transaction->getError())) { // Log the error message // $error = $transaction->getError(); } else { // Save transaction id // $transactionId = $result->transactionId; }
CAPTURE
确保您已启用账户中的定期付款。
$transaction = (new \ATDev\Viva\Transaction\Capture()) ->setClientId("[Client ID]") // Client ID, Provided by wallet ->setClientSecret("[Client Secret]") // Client Secret, Provided by wallet ->setTestMode("[Test Mode]") // Test mode, default is false, can be skipped ->setTransactionId("[Transaction ID]") // Transaction id of authorization transaction ->setAmount((int) "[Amount]"); // The amount to capture in currency's smallest denomination (e.g amount in pounds x 100) $result = $transaction->send(); if (!empty($transaction->getError())) { // Log the error message // $error = $transaction->getError(); } else { // Save transaction id // $transactionId = $result->transactionId; }
CANCEL
确保您已启用账户中的退款。
$transaction = (new \ATDev\Viva\Transaction\Cancel()) ->setClientId("[Client ID]") // Client ID, Provided by wallet ->setClientSecret("[Client Secret]") // Client Secret, Provided by wallet ->setTestMode("[Test Mode]") // Test mode, default is false, can be skipped ->setSourceCode("[Source Code]") // Source code, provided by wallet ->setTransactionId("[Transaction ID]") // Transaction id of charge, authorization or capture transaction ->setAmount((int) "[Amount]"); // The amount to refund in currency's smallest denomination (e.g amount in pounds x 100) $result = $transaction->send(); if (!empty($transaction->getError())) { // Log the error message // $error = $transaction->getError(); } else { // Save transaction id // $transactionId = $result->transactionId; }
在后端获取费用令牌
您可以在后端获取费用令牌。在自定义集成中可能需要,更多详细信息请参阅: https://developer.vivawallet.com/native-checkout-v2/
$transaction = (new \ATDev\Viva\Transaction\ChargeToken()) ->setClientId("[Client ID]") // Client ID, Provided by wallet ->setClientSecret("[Client Secret]") // Client Secret, Provided by wallet ->setTestMode("[Test Mode]") // Test mode, default is false, can be skipped ->setAmount((int) "[Amount]"); // The amount in currency's smallest denomination (e.g amount in pounds x 100) ->setCvc("[Cvc code]") // Card cvc code ->setNumber("[Card number]") // Card number ->setHolderName("[Holder name]") // Card holder name ->setExpirationYear((int) "[Expiration Year]") // Card expiration year ->setExpirationMonth((int) "[Expiration Month]") // Card expiration month ->setSessionRedirectUrl("[Session redirect url]"); // Url to redirect when authentication session finished $result = $transaction->send(); if (!empty($transaction->getError())) { // Log the error message // $error = $transaction->getError(); } else { // Get charge token // $chargeToken = $result->chargeToken; // $redirectToACSForm = $result->redirectToACSForm; }
检查分期付款
检索卡片上允许的最大分期付款次数。
$transaction = (new \ATDev\Viva\Transaction\Installments()) ->setClientId("[Client ID]") // Client ID, Provided by wallet ->setClientSecret("[Client Secret]") // Client Secret, Provided by wallet ->setTestMode("[Test Mode]") // Test mode, default is false, can be skipped ->setNumber("[Card number]"); // Card number $result = $transaction->send(); if (!empty($transaction->getError())) { // Log the error message // $error = $transaction->getError(); } else { // Get number of installments // $installments = $result->maxInstallments; }
单元测试
测试通过 ./vendor/bin/phpunit tests
运行。尽管库代码设计为与 php 5.6
兼容,但由于 phpunit
版本 9
,测试需要至少 php 7.3
。