digitalvirgo / directpay
DirectPay 的官方库
1.0.1
2020-04-22 15:01 UTC
Requires
- php: >=5.6
- guzzlehttp/guzzle: 6.5.*
Requires (Dev)
- phpunit/phpunit: ^5.7
- slim/slim: ^3.12
This package is not auto-updated.
Last update: 2024-09-28 20:09:16 UTC
README
此库提供对 DirectPay Api 的集成访问。
安装
Composer
使用命令行界面
composer require digitalvirgo/directpay
或手动将以下内容添加到 composer.json 中的 "require" 部分
"require": { "digitalvirgo/directpay":"^1.0.0" }
入门
如果您使用 Composer,请使用自动加载功能
include "vendor/autoload.php";
用法
1. 配置
DirectPay 版本 2 的基本设置
use DigitalVirgo\DirectPay\Service\Client; $client = new Client();
第一个参数是 DirectPay url。默认为版本 2。这些 url 存储在常量中: Client::DP_V1_BASE_URL
和 Client::DP_V2_BASE_URL
。您可以将 Guzzle 客户端选项作为第二个参数传递。
use DigitalVirgo\DirectPay\Service\Client; $options = [ 'timeout' => 10, ] $client = new Client(Client::DP_V2_BASE_URL, $options);
在客户端中设置您的凭证
$client->setAuth($login, $password);
或者您可以直接在每次请求中传递凭证
$request = new \DigitalVirgo\DirectPay\Model\Request\PaymentPointInfoRequest([ 'product' => $product, 'login' => $login, 'password' => $password, ])
2. 获取支付点
在您能够生成新订单之前,您必须了解您的支付点
$paymentPointResponse = $client->paymentPointInfo([ 'product' => [ 'name' => 'product name', 'price' => [ // net price and currency is mandatory 'net' => '1,00', 'gross' => '1,23', 'tax' => '0,23', 'taxRate' => '23', 'currency' => 'PLN', ] ] ]); if ($paymentPointResponse->getError()) { throw new \Exception("Unable to get paymentPoints: {$paymentPointResponse->getError()} {$paymentPointResponse->getErrorDescription()}"); }
3. 选择您的支付点
$singlePaymentPoint = $paymentPointResponse->getProduct()->getPaymentPoints()->getPaymentPoint()[0]; // we use first given
4. 生成新订单
$orderNewResponse = $client->orderNewRequest([ 'order' => [ 'paymentPointId' => $singlePaymentPoint->getPaymentPointId(), 'orderDescription' => 'order_description', 'product' => [ 'name' => 'product name', 'price' => $singlePaymentPoint->getPrice(), ], 'notifyUrl' => 'https://partnerhost.com/notify', //setup your url's 'orderFailureUrl' => 'https://partnerhost.com/failure', 'orderCompleteUrl' => 'https://partnerhost.com/complete', ], ]);
5. 检查订单状态
$orderGetResponse = $client->orderGetRequest([ 'orderId' => $orderNewResponse->getOrderId() ]); if ($orderGetResponse->getError()) { throw new \Exception("Unable to get order: {$orderGetResponse->getError()} {$orderGetResponse->getErrorDescription()}"); } var_dump($orderGetResponse->getOrder()->getOrderId()); var_dump($orderGetResponse->getOrder()->getOrderStatus());
6. 接收通知
当您在 orderNewReqest
中设置 notifyUrl
时,DirectPay 将通知您。您可以将接收到的 xml 解析为 OrderNotifyRequest
。
$body = file_get_contents('php://input'); //if you can't receive this notification throw some error. // if everything is ok return status 200; $orderNotifyRequest = OrderNotifyRequest::fromXml($body); $response = new OrderNotifyResponse([ 'order' => $orderNotifyRequest->getOrder(), 'updateDate' => new DateTimeImmutable(), ]); print ($response->toXml());
所有步骤都在 example/index.php 和 example/notify.php 中。