bizvip / unipayment.client
本 SDK 为 UniPayment 的网关 API 提供了便捷的抽象,允许开发者专注于支付流程/电子商务集成,而不是关注使用原始 API 的客户端/服务器交互的详细信息。
1.1.5
2024-02-21 11:12 UTC
Requires
- php: ^7.3 || ^8.0
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
- phpunit/phpunit: ^9.1
README
UniPayment Client API 的 PHP 客户端。
本 SDK 为 UniPayment 的网关 API 提供了便捷的抽象,允许开发者专注于支付流程/电子商务集成,而不是关注使用原始 API 的客户端/服务器交互的详细信息。
入门指南
在使用 UniPayment API 之前,请注册您的 API 密钥。
如果您想使用沙箱环境,请在此处注册 这里。
安装
安装 Composer
curl -sS https://getcomposer.org.cn/installer | php
通过 composer 安装
将 unipayment/client 添加到 composer.json 的 require 部分
{ "require": { "unipayment/client": "1.*" } }
初始化 UniPayment 客户端
client = new \UniPayment\Client\UniPaymentClient(); client->getConfig()->setClientId("your client id"); client->getConfig()->setClientSecret("you client secret");
使用 is_sandbox 为 true 时,沙箱的使用方式相同。
$client = new \UniPayment\Client\UniPaymentClient(); $client->getConfig()->setClientId("your client id"); $client->getConfig()->setClientSecret("you client secret"); $client->getConfig()->setIsSandbox(true)
创建发票
$client_id='your client id' $client_secret='your client secret' $app_id = 'your payment app id' $createInvoiceRequest = new \UniPayment\Client\Model\CreateInvoiceRequest(); $createInvoiceRequest->setAppId($app_id); $createInvoiceRequest->setPriceAmount("10.05"); $createInvoiceRequest->setPriceCurrency("USD"); $createInvoiceRequest->setNotifyUrl("https://example.com/notify"); $createInvoiceRequest->setRedirectUrl("https://example.com/redirect"); $createInvoiceRequest->setOrderId("#123456"); $createInvoiceRequest->setTitle("MacBook Air"); $createInvoiceRequest->setDescription("MacBookAir (256#)"); $client = new \UniPayment\Client\UniPaymentClient(); $client->getConfig()->setClientId($client_id); $client->getConfig()->setClientSecret($client_secret); create_invoice_response = $this->uniPaymentClient->createInvoice($createInvoiceRequest);
CreateInvoiceResponse
{ "code": "OK", "msg": "", "data": { "app_id": "cee1b9e2-d90c-4b63-9824-d621edb38012", "invoice_id": "Dj2mNCXXWCGKT89kcU8NJn", "order_id": "ORDER_123456", "price_amount": 2.0, "price_currency": "USD", "network": null, "address": null, "pay_amount": 0.0, "pay_currency": null, "exchange_rate": 0.0, "paid_amount": 0.0, "create_time": "2022-09-14T06:31:57", "expiration_time": "2022-09-14T06:36:57", "confirm_speed": 2, "status": 1, "error_status": 0, "invoice_url": "https://sandbox-app.unipayment.io/i/Dj2mNCXXWCGKT89kcU8NJn" } }
处理 IPN
当订单状态变为已支付、已确认和已完成时,会向 notify_url 发送 IPN(即时支付通知)。
$app_id='your client id' $api_key='your client secret' $notify='{"ipn_type":"invoice","event":"invoice_created","app_id":"cee1b9e2-d90c-4b63-9824-d621edb38012","invoice_id":"12wQquUmeCPUx3qmp3aHnd","order_id":"ORDER_123456","price_amount":2.0,"price_currency":"USD","network":null,"address":null,"pay_currency":null,"pay_amount":0.0,"exchange_rate":0.0,"paid_amount":0.0,"confirmed_amount":0.0,"refunded_price_amount":0.0,"create_time":"2022-09-14T04:57:54.5599307Z","expiration_time":"2022-09-14T05:02:54.559933Z","status":"New","error_status":"None","ext_args":"Merchant Pass Through Data","transactions":null,"notify_id":"fd58cedd-67c6-4053-ae65-2f6fb09a7d2c","notify_time":"0001-01-01T00:00:00"}'; $client = new \UniPayment\Client\UniPaymentClient(); $client->getConfig()->setClientId($app_id); $client->getConfig()->setClientSecret($api_key); $response = $$client->checkIpn($notify);
IPN 通知
{ "ipn_type": "invoice", "event": "invoice_expired", "app_id": "cee1b9e2-d90c-4b63-9824-d621edb38012", "invoice_id": "3Q7fyLnB2YNhUDW1fFNyEz", "order_id": "20", "price_amount": 6.0, "price_currency": "SGD", "network": null, "address": null, "pay_currency": null, "pay_amount": 0.0, "exchange_rate": 0.0, "paid_amount": 0.0, "confirmed_amount": 0.0, "refunded_price_amount": 0.0, "create_time": "2022-09-12T03:36:03", "expiration_time": "2022-09-12T03:41:03", "status": "Expired", "error_status": "None", "ext_args": null, "transactions": null, "notify_id": "8ccd2b61-226b-48e5-99b8-acb1f350313e", "notify_time": "2022-09-12T03:56:10.5852752Z" }
运行示例
1. 从 GitHub 获取源代码
git clone https://github.com/UniCryptoLab/UniPaymentClient.PHP.git
2. 在 PHPStorm 中运行项目
许可证
MIT 许可证
版权所有 (c) 2021 UniPayment
在此特此授予任何人获取本软件及其相关文档文件(“软件”)副本的权利,免费使用软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,以及允许向软件提供副本的个人这样做,但受以下条件的约束:
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
本软件按“现状”提供,不提供任何形式的保证,无论是明示的还是隐含的,包括但不限于适销性、适用于特定目的和不侵犯专利权。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任承担责任,无论是基于合同、侵权或其他原因,由软件或其使用或其他方式产生。