cardinity / cardinity-sdk-php
Cardinity信用卡处理API的客户端库
v3.3.4
2024-09-04 06:02 UTC
Requires
- php: >=7.2.5
- guzzlehttp/guzzle: ^6.5.6||^7.4.3
- guzzlehttp/oauth-subscriber: ~0.3
- symfony/validator: 4.0 - 7.0
Requires (Dev)
- monolog/monolog: ~1.0
- phpspec/phpspec: ~6.2||7.*
- phpunit/phpunit: ^8.5
- symfony/yaml: ^4.4
- dev-master
- v3.3.4
- v3.3.3
- v3.3.2
- v3.3.1
- v3.3.0
- v3.2.2
- v3.2.1
- v3.2.0
- v3.1.0
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.1.0
- v2.0.0
- 1.x-dev
- v1.1.1
- v1.1.0
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-PHP-711-allow-amount-integer-or-float-with-4-decimal
- dev-PHP-689-sdk-added-threeds-status-reason-in-payment-response
- dev-update-validator-version
- dev-PHP-656-merchant-advice-code-implement
- dev-add-phpspec-to-github-actions
This package is not auto-updated.
Last update: 2024-09-18 08:41:54 UTC
README
这是Cardinity的API的官方PHP客户端库:Cardinity's API。
库包含API提供的所有功能。库的设计旨在为开发者提供灵活性和易于理解的实现。
文档
更详细的文档和用法示例可以在这里找到。
用法
通过Composer安装
$ php composer.phar require cardinity/cardinity-sdk-php
直接下载
您可以从最新版本开始下载,文件名以cardinity-sdk-php-*.zip
开头。
调用API
初始化客户端对象
use Cardinity\Client; $client = Client::create([ 'consumerKey' => 'YOUR_CONSUMER_KEY', 'consumerSecret' => 'YOUR_CONSUMER_SECRET', ]);
创建新的付款
use Cardinity\Method\Payment; $method = new Payment\Create([ 'amount' => 50.00, 'currency' => 'EUR', 'settle' => false, 'description' => 'some description', 'order_id' => '12345678', 'country' => 'LT', 'payment_method' => Payment\Create::CARD, 'payment_instrument' => [ 'pan' => '4111111111111111', 'exp_year' => 2021, 'exp_month' => 12, 'cvc' => '456', 'holder' => 'Mike Dough' ], 'threeds2_data' => [ "notification_url" => "your_shop_url_for_handling_callback", "browser_info" => [ "accept_header" => "text/html", "browser_language" => "en-US", "screen_width" => 600, "screen_height" => 400, 'challenge_window_size' => "600x400", "user_agent" => "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:21.0) Gecko/20100101 Firefox/21.0", "color_depth" => 24, "time_zone" => -60 ], ], ]);
所有threeds2_data
参数都应该动态设置。
browser_info
的screen_width
、screen_height
、browser_language
、color_depth
、time_zone
参数可以通过javascript
动态收集
document.addEventListener("DOMContentLoaded", function() { document.getElementById("screen_width").value = screen.availWidth; document.getElementById("screen_height").value = screen.availHeight; document.getElementById("browser_language").value = navigator.language; document.getElementById("color_depth").value = screen.colorDepth; document.getElementById("time_zone").value = new Date().getTimezoneOffset(); });
并将其放入一个html
表单中
<!-- ... --> <input type='hidden' id='screen_width' name='screen_width' value='' /> <input type='hidden' id='screen_height' name='screen_height' value='' /> <input type='hidden' id='browser_language' name='browser_language' value='' /> <input type='hidden' id='color_depth' name='color_depth' value='' /> <input type='hidden' id='time_zone' name='time_zone' value='' /> <!-- ... -->
然后应该使用try ... catch
块执行对Cardinity API的调用
$errors = []; try { /** @type Cardinity\Method\Payment\Payment */ $payment = $client->call($method); $status = $payment->getStatus(); if ($status == 'approved') { echo '<p>Your payment approved without 3D secure.</p>'; } elseif ($status == 'pending') { if ($payment->isThreedsV2()) { // $auth object for data required to finalize payment $auth = $payment->getThreeds2Data(); // finalize process should be done here. }else if ($payment->isThreedsV1()) { // $auth object for data required to finalize payment $auth = $payment->getAuthorizationInformation(); // finalize process should be done here. } } } catch (Cardinity\Exception\InvalidAttributeValue $exception) { foreach ($exception->getViolations() as $key => $violation) { array_push($errors, $violation->getPropertyPath() . ' ' . $violation->getMessage()); } } catch (Cardinity\Exception\ValidationFailed $exception) { foreach ($exception->getErrors() as $key => $error) { array_push($errors, $error['message']); } } catch (Cardinity\Exception\Declined $exception) { foreach ($exception->getErrors() as $key => $error) { array_push($errors, $error['message']); } } catch (Cardinity\Exception\NotFound $exception) { foreach ($exception->getErrors() as $key => $error) { array_push($errors, $error['message']); } } catch (Exception $exception) { $errors = [$exception->getMessage()]; } if ($errors) { print_r($errors); }
完成付款
要完成付款,其状态应该是pending
。从3D secure系统接收到的数据应用于创建Finalize $method
。
use Cardinity\Method\Payment; $client = Client::create([ 'consumerKey' => 'YOUR_CONSUMER_KEY', 'consumerSecret' => 'YOUR_CONSUMER_SECRET', ]); if($v2){ $method = new Payment\Finalize( $payment->getId(), // payment object received from API call $auth->getCreq(), // payment object received from API call true // BOOL `true` to enable 3D secure V2 parameters ); }elseif($v1){ $method = new Payment\Finalize( $payment->getId(), // payment object received from API call $auth->getData(), // payment object received from API call false // BOOL `false` to enable 3D secure V1 parameters ); } // again use same try ... catch block try { $payment = $client->call($method); } // same catch blocks ... // ...
获取现有付款
$method = new Payment\Get('cb5e1c95-7685-4499-a2b1-ae0f28297b92'); /** @type Cardinity\Method\Payment\Payment */ $payment = $client->call($method);
API文档
https://developers.cardinity.com/api/v1/
开发状态
已实现所有API v1方法。
测试
对于Windows php vendor/phpunit/phpunit/phpunit