zgabievi / laravel-ipay
Laravel 的 iPay 集成
0.2.4
2021-07-30 06:44 UTC
Requires
- php: ^7.2
- bensampo/laravel-enum: ^1.38|^3.1
- illuminate/config: ^6.0|^7.0|^8.0
- illuminate/database: ^6.0|^7.0|^8.0
- illuminate/support: ^6.0|^7.0|^8.0
README
目录
安装
要开始,您需要安装此包
composer require zgabievi/laravel-ipay
如果您的 Laravel 版本低于 5.5,则将以下内容添加到 config/app.php 中的服务提供者
'providers' => [ ... Zorb\IPay\IPayServiceProvider::class, ... ];
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="Zorb\IPay\IPayServiceProvider"
此命令将为您复制配置文件
使用
所有响应都是 stdClasses。错误由 laravel 的 abort 辅助程序处理。如果您想处理它们,请自行捕获异常。
以下是此包提供的方法
- 支付流程 - 支付流程初始化
- 周期性付款 - 使用保存的订单 ID 重复支付流程
- 退款 - 交易的撤销
- 订单详情 - 检查订单详情
- 订单状态 - 检查订单状态
- 支付详情 - 检查支付详情
- 完成预授权 - 完成预授权订单
- 辅助方法 - 生成结账的辅助方法
支付流程
生成令牌
此步骤是可选的,如果您不提供令牌给下一个请求,它将自动获取令牌。
use Zorb\IPay\Facades\IPay; class PaymentController { public function __invoke() { $response = IPay::token(); } }
示例 $response:
{ "access_token": "eyJraWQiOiIxMDA2IiwiY3R5IjoiYXBwbGljYXRpb25cL2pzb24iLCJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJQdWJsaWMgcGF5bWVudCBBUEkgVjEiLCJhdWQiOiJpUGF5IERlbW8iLCJpc3M", "token_type": "Bearer", "app_id": "1A2019", "expires_in": 1605623557393 }
结账
在 iPay 系统中生成订单并获取订单详情和重定向 URL。
use Zorb\IPay\Facades\IPay; use Zorb\IPay\Enums\Intent; class PaymentController { public function __invoke() { $order_id = 1; $units = [ IPay::purchaseUnit(10), // read more about purchaseUnit bellow ]; $items = [ IPay::purchaseItem(1, 10, 1, 'Item #1'), // read more about purchaseItem bellow IPay::purchaseItem(2, 10, 1, 'Item #2'), // read more about purchaseItem bellow ]; // string $intent - 'CAPTURE', 'AUTHORIZE', 'LOAN' // int $order_id - Your order id // array $units - Purchase units // array $items = [] - (optional) Purchase items // string $token = null - (optional) JWT Token // string $capture_method = 'AUTOMATIC' - (optional) 'AUTOMATIC', 'MANUAL' // string $transaction_id = '' - (optional) Transaction id for recurring $response = IPay::checkout(Intent::Capture, $order_id, $units, $items); } }
您可以分别设置参数
use Zorb\IPay\Facades\IPay; use Zorb\IPay\Enums\Intent; use Zorb\IPay\Enums\CaptureMethod; class PaymentController { public function __invoke() { $order_id = 1; $response = IPay::setIntent(Intent::Capture) ->setShopOrder($order_id) ->setPurchaseUnits([ IPay::purchaseUnit(10) ]) ->setItems([ IPay::purchaseItem(1, 10, 1, 'Item #1'), IPay::purchaseItem(2, 10, 1, 'Item #2'), ]) ->setToken('IPAY_JWT_TOKEN') ->setCaptureMethod(CaptureMethod::Manual) ->setTransaction('IPAY_TRANSACTION_ID') ->checkout(); } }
示例 $response
{ "status": "CREATED", "payment_hash": "d7936f718c2b0ec2517a28c9de76966bcbecfe29", "links": [ { "href": "https://ipay.ge/opay/api/v1/checkout/orders/899318b1ce0d5885cb7405fe86e3930178ff90be", "rel": "self", "method": "GET" }, { "href": "https://ipay.ge/?order_id=899318b1ce0d5885cb7405fe86e3930178ff90be&locale=ka", "rel": "approve", "method": "REDIRECT" } ], "order_id": "899318b1ce0d5885cb7405fe86e3930178ff90be" }
重定向
重定向到结账方法提供的支付页面。
use Zorb\IPay\Facades\IPay; use Zorb\IPay\Enums\Intent; use Zorb\IPay\Enums\CheckoutStatus; class PaymentController { public function __invoke() { $order_id = 1; $units = [ IPay::purchaseUnit(10), // read more about purchaseUnit bellow ]; $items = [ IPay::purchaseItem(1, 10, 1, 'Item #1'), // read more about purchaseItem bellow IPay::purchaseItem(2, 10, 1, 'Item #2'), // read more about purchaseItem bellow ]; $response = IPay::checkout(Intent::Capture, $order_id, $units, $items); if (isset($response->status) && $response->status === CheckoutStatus::Created) { return IPay::redirect($response); // IPay::redirectUrl($response); - will be used in some cases, like InertiaJS } } }
您可以分别设置参数
use Zorb\IPay\Facades\IPay; use Zorb\IPay\Enums\Intent; use Zorb\IPay\Enums\CaptureMethod; use Zorb\IPay\Enums\CheckoutStatus; class PaymentController { public function __invoke() { $order_id = 1; $response = IPay::setIntent(Intent::Capture) ->setShopOrder($order_id) ->setPurchaseUnits([ IPay::purchaseUnit(10) ]) ->setItems([ IPay::purchaseItem(1, 10, 1, 'Item #1'), IPay::purchaseItem(2, 10, 1, 'Item #2'), ]) ->setToken('IPAY_JWT_TOKEN') ->setCaptureMethod(CaptureMethod::Manual) ->setTransaction('IPAY_TRANSACTION_ID') ->checkout(); if (isset($response->status) && $response->status === CheckoutStatus::Created) { return IPay::setResponse($response)->redirect(); // IPay::setResponse($response)->redirectUrl(); } } }
重定向方法将找到支付重定向链接并将用户重定向到该页面。
周期性付款
周期性付款流程与结账流程相同。您只需要提供要用于周期性付款的交易 ID。
use Zorb\IPay\Facades\IPay; use Zorb\IPay\Enums\Intent; use Zorb\IPay\Enums\CheckoutStatus; class PaymentController { public function __invoke(string $trx_id) { $order_id = 1; $transaction_id = '899318B1CE0D5885CB7'; // Transaction id was provided in you callback url $units = [ IPay::purchaseUnit(10), // read more about purchaseUnit bellow ]; $items = [ IPay::purchaseItem(1, 10, 1, 'Item #1'), // read more about purchaseItem bellow IPay::purchaseItem(2, 10, 1, 'Item #2'), // read more about purchaseItem bellow ]; $response = IPay::repeat($transaction_id, Intent::Capture, $order_id, $units, $items); if (isset($response->status) && $response->status === CheckoutStatus::Created) { return IPay::redirect($response); } } }
您可以分别设置参数
use Zorb\IPay\Facades\IPay; use Zorb\IPay\Enums\Intent; use Zorb\IPay\Enums\CheckoutStatus; class PaymentController { public function __invoke() { $order_id = 1; $transaction_id = '899318B1CE0D5885CB7'; $response = IPay::setIntent(Intent::Capture) ->setShopOrder($order_id) ->setPurchaseUnits([ IPay::purchaseUnit(10) ]) ->setItems([ IPay::purchaseItem(1, 10, 1, 'Item #1'), IPay::purchaseItem(2, 10, 1, 'Item #2'), ]) ->setTransaction($transaction_id) ->repeat(); if (isset($response->status) && $response->status === CheckoutStatus::Created) { return IPay::setResponse($response)->redirect(); } } }
退款
为了退款,您需要支付订单的 order_id。
use Zorb\IPay\Facades\IPay; class PaymentController { public function __invoke() { $order_id = '899318b1ce0d5885cb7405fe86e3930178ff90be'; // string $order_id - Order id provided by checkout process // int $amount - Amount you want to refund (in cents) // string $token = null - (optional) JWT Token $response = IPay::refund($order_id, 10); } }
您可以分别设置参数
use Zorb\IPay\Facades\IPay; class PaymentController { public function __invoke() { $order_id = '899318b1ce0d5885cb7405fe86e3930178ff90be'; $response = IPay::setOrder($order_id) ->setAmount(10) ->refund(); } }
如果响应是 OK,则表示退款流程成功。
订单详情
为了获取订单详情,您需要支付订单的 order_id。
use Zorb\IPay\Facades\IPay; class PaymentController { public function __invoke() { $order_id = '899318b1ce0d5885cb7405fe86e3930178ff90be'; // string $order_id - Order id provided by checkout process // string $token = null - (optional) JWT Token $response = IPay::orderDetails($order_id); } }
您可以分别设置参数
use Zorb\IPay\Facades\IPay; class PaymentController { public function __invoke() { $order_id = '899318b1ce0d5885cb7405fe86e3930178ff90be'; $response = IPay::setOrder($order_id) ->orderDetails(); } }
示例 $response
{ "id": "6ed105e54e703fb6d2e5b7f68a0face71fea2cc6", "status": "PERFORMED", "intent": "CAPTURE", "payer": { "name": null, "email_address": null, "payer_id": null }, "purchaseUnit": { "amount": { "value": "0.10", "currency_code": "GEL" }, "payee": { "addres": "Shartava str., 77", "contact": "0322444444", "email_address": "support@ipay.ge" }, "payments": [ { "captures": [ { "id": "1", "status": "PERFORMED", "amount": { "value": "0.10", "currency_code": "GEL" }, "final_capture": "true", "create_time": "Tue Nov 17 19:04:29 GET 2020", "update_time": "Tue Nov 17 19:04:29 GET 2020" }, { "id": "2", "status": "PERFORMED", "amount": { "value": "0.10", "currency_code": "GEL" }, "final_capture": "true", "create_time": "Tue Nov 17 19:04:29 GET 2020", "update_time": "Tue Nov 17 19:04:29 GET 2020" } ] } ], "shop_order_id": "1" }, "createTime": null, "updateTime": null, "errorHistory": [] }
订单状态
为了获取订单状态,您需要支付订单的 order_id。
use Zorb\IPay\Facades\IPay; class PaymentController { public function __invoke() { $order_id = '899318b1ce0d5885cb7405fe86e3930178ff90be'; // string $order_id - Order id provided by checkout process // string $token = null - (optional) JWT Token $response = IPay::orderStatus($order_id); } }
您可以分别设置参数
use Zorb\IPay\Facades\IPay; class PaymentController { public function __invoke() { $order_id = '899318b1ce0d5885cb7405fe86e3930178ff90be'; $response = IPay::setOrder($order_id) ->orderStatus(); } }
示例 $response
{ "status": "REJECTED" }
支付详情
为了获取支付详情,您需要支付订单的 order_id。
use Zorb\IPay\Facades\IPay; class PaymentController { public function __invoke() { $order_id = '899318b1ce0d5885cb7405fe86e3930178ff90be'; // string $order_id - Order id provided by checkout process // string $token = null - (optional) JWT Token $response = IPay::paymentDetails($order_id); } }
您可以分别设置参数
use Zorb\IPay\Facades\IPay; class PaymentController { public function __invoke() { $order_id = '899318b1ce0d5885cb7405fe86e3930178ff90be'; $response = IPay::setOrder($order_id) ->paymentDetails(); } }
示例 $response
{ "status": "error", "pan": null, "order_id": "899318b1ce0d5885cb7405fe86e3930178ff90be", "pre_auth_status": null, "payment_hash": "d7936f718c2b0ec2517a28c9de76966bcbecfe29", "ipay_payment_id": "18625", "status_description": "REJECTED", "shop_order_id": "1", "payment_method": "UNKNOWN", "card_type": "UNKNOWN", "transaction_id": null }
完成预授权
为了获取完整预授权订单,您需要支付订单的 order_id。
use Zorb\IPay\Facades\IPay; class PaymentController { public function __invoke() { $order_id = '899318b1ce0d5885cb7405fe86e3930178ff90be'; // string $order_id - Order id provided by checkout process // string $token = null - (optional) JWT Token $response = IPay::completePreAuth($order_id); } }
您可以分别设置参数
use Zorb\IPay\Facades\IPay; class PaymentController { public function __invoke() { $order_id = '899318b1ce0d5885cb7405fe86e3930178ff90be'; $response = IPay::setOrder($order_id) ->completePreAuth(); } }