cmpayments / payments-sdk-php
简化连接到CMPayments支付API的SDK
Requires
- php: >=5.5.0
- cmpayments/guzzle-psp-authentication-middleware: ^1.0
- moneyphp/money: ^3.0
Requires (Dev)
- jakub-onderka/php-parallel-lint: ^0.9.0
- phpunit/phpunit: ^4.7|>=5.0 <5.4
- squizlabs/php_codesniffer: ^2.3
This package is auto-updated.
Last update: 2024-09-21 20:20:49 UTC
README
使用PHP的支付SDK轻松集成CMPayments在线支付解决方案到您的应用程序中。
安装
要安装SDK,只需使用 Composer: composer require cmpayments/payments-sdk-php
要求
- PHP 5.5+
- PHP cURL扩展
- 更新到SSL,支持TLS 1.0或更高版本
依赖项
这些由Composer自动安装。
启动
要对SDK进行任何操作,第一步是创建一个支付 Gateway
实例
<?php use CMPayments\PaymentSdk\Credentials; use CMPayments\PaymentSdk\Gateway; $gateway = new Gateway(new Credentials('your-api-key', 'your-api-secret'));
列出iDEAL发行商
要获取iDEAL发行商列表,只需让 Gateway
执行 IdealIssuerListRequest
<?php use CMPayments\PaymentSdk\Requests\IdealIssuerListRequest; $issuers = $gateway->execute(new IdealIssuerListRequest()); foreach ($issuers as $name => $id) { // $name is now 'ABN AMRO Bank', 'Rabobank', 'ING', etc. // $id is now 'ABNANL2A', 'RABONL2U', 'INGBNL2A', etc. }
开始支付
CMPayments API支持包含0..n个 Payment
项目下的 Charge
概念。要开始支付,必须创建一个 Charge
和一个 Payment
。这可以在一个请求中完成
<?php use CMPayments\PaymentSdk\Entities\Charge; use CMPayments\PaymentSdk\Entities\IdealPayment; use CMPayments\PaymentSdk\Requests\CreateChargeRequest; use Money\Money; // Create both a charge and a payment object $payment = new IdealPayment(Money::EUR(500), 'RABONL2U', 'your-unique-purchase-id', 'A description of the transaction'); $charge = new Charge(Money::EUR(500), [$payment]); $response = $gateway->execute(new CreateChargeRequest($charge)); // The id of the charge is available as $response->charge_id, the id of the payment in $response->payments[0]->payment_id // These ids are in the form of ch- (or charge) or pt- (for payment), followed by a uuid v4. // To have the user complete the payment, redirect them to $response->payments[0]->payment_details->authentication_url
注意:每种支付方式都有自己的 {METHOD}Payment类。这些类的构造函数都强制执行所有必需的属性。例如,要创建一个新的信用卡支付,将new IdealPayment(...)
行替换为new CreditCardPayment(Money::EUR(500), ['VISA', 'MasterCard'], 'your-purchase-id', new \DateTime('+1 day'));
。
检索费用或支付
只需让 Gateway
执行带有正确id的 ViewChargeRequest
或 ViewPaymentRequest
,即可检索详细信息。
<?php use CMPayments\PaymentSdk\Requests\ViewChargeRequest; use CMPayments\PaymentSdk\Requests\ViewPaymentRequest; $response = $gateway->execute( new ViewChargeRequest('ch-fd0e1e2d-f994-4afc-a0b6-f7e76550fc31') ); $response = $gateway->execute( new ViewPaymentRequest('pt-297bba0f-5fae-4ec2-8c0f-dfbc0f62f6b0') );
处理错误
内部,使用Guzzle发送HTTP请求。当API响应4xx或5xx HTTP状态时,会抛出 ClientException
或 ServerException
。
如果出现 ServerException
,则CMPayments平台存在问题。稍后再次尝试请求。
对于 ClientException
,检查 $exception->getResponse()->getBody()->getContents()
来查看错误。
与Money
一起工作
使用的 Money
库要求任何金额都表示为最小单位(例如,分),因此EUR 5,-写作new Money(500, new Currency('EUR'))
或更短的形式为Money::EUR(500)
。
因为这可能有些麻烦,所以提供了MoneyConverter
类。它可以转换浮点数+货币到Money
对象,反之亦然。
<?php use \CMPayments\PaymentSdk\MoneyConverter; $converter = new MoneyConverter(); $money = $converter->fromAmountAndCurrency(5.00, 'EUR'); $amount = $converter->toFloat($money);