gridonic / swisscom-easypay
用于处理瑞士电信易付(Swisscom EasyPay)支付功能的PHP库
Requires
- php: >=7.0
- guzzlehttp/guzzle: ^6.3
Requires (Dev)
- phpunit/phpunit: ^6.5
This package is not auto-updated.
Last update: 2024-09-20 22:07:25 UTC
README
一个用于管理瑞士电信易付(Swisscom Easypay)支付的PHP库。
安装
使用composer安装库
composer require gridonic/swisscom-easypay
基本用法
注意:本指南仅涵盖如何使用此库的基本知识。有关Easypay的更多详细信息,请参阅官方文档。
环境
根据您的凭据创建一个新的STAGING
或PROD
环境
use Gridonic\EasyPay\Environment\Environment; $prodEnvironment = new Environment(Environment::ENV_PROD, 'my-merchant-id', 'my-secret-key') $stagingEnvironment = new Environment(Environment::ENV_STAGING, 'my-merchant-id', 'my-secret-key')
结账页面
将用户重定向到易付结账页面,在那里必须确认购买。
- 将用户的购物车映射到
CheckoutPageItem
。请注意,您必须提供重定向回您商店的成功/错误/取消URL。 - 对于周期性服务,请确保通过
setDuration()
和setDurationUnit()
将持续时间及其单位传递给结账页面项。 - 调用
CheckoutPageService::getCheckoutPageUrl()
以获取重定向URL。
use Gridonic\EasyPay\CheckoutPage\CheckoutPageItem; use Gridonic\EasyPay\CheckoutPage\CheckoutPageService; // Map the user's shopping cart to a CheckoutPageItem $checkoutPageItem = new CheckoutPageItem(); $checkoutPageItem ->setTitle('A mandatory title displayed on the checkout page') ->setDescription('A mandatory description displayed on the checkout page') ->setPaymentInfo('Mandatory payment information, visible on the invoice of the customer') ->setAmount('99.90') ->setSuccessUrl('https://myshop.com/return') ->setErrorUrl('https://myshop.com/return') ->setCancelUrl('https://myshop.com/cancel'); // Get the checkout page redirect URL $checkoutPageService = CheckoutPageService::create($environment); $redirectUrl = $checkoutPageService->getCheckoutPageUrl($checkoutPageItem);
处理结账页面响应
在结账页面上确认购买后,用户将重定向回商店。为了完成购买,必须通过Easypay的REST API提交付款。使用CheckoutPageResponseService
获取提交付款所需的payment-ID
或subscription-ID
。
use Gridonic\EasyPay\CheckoutPage\CheckoutPageResponse; // Create an instance from the available GET parameters $checkoutPageResponse = CheckoutPageResponse::createFromGet(); if ($checkoutPageResponse->isSuccess()) { $paymentId = $checkoutPageResponse->getPaymentId(); // or if the submitted CheckoutPageItem is a subscription (recurrent service) $authSubscriptionId = $checkoutPageResponse->getAuthSubscriptionId(); } else { print_r($checkoutPageResponse->getErrorCode()); }
提交付款
一次性(直接)付款需要通过Easypay的REST API提交。使用RestApiService
来完成此操作
use Gridonic\EasyPay\REST\RESTApiService; $restApiService = RESTApiService::create($environment); // Commit a direct payment $directPaymentResponse = $restApiService->directPayment('paymentId'); if ($directPaymentResponse->isSuccess()) { // Payment commited successfully } else { // A more detailed error is available as error message: $errorMessages = $directPaymentResponse->getErrorMessages(); $errorMessage = array_pop($errorMessages); $errorMessage->getMessage(); $errorMessage->getCode(); $errorMessage->getField(); $errorMessage->getRequestId(); }
对于服务订阅,程序类似
// Authorize a subscription $authSubscriptionResponse = $restApiService->authorizeSubscription('authSubscriptionId'); if ($authSubscriptionResponse->isSuccess()) { // Subscription authorized successfully } else { $errorMessages = $authSubscriptionResponse->getErrorMessages(); $errorMessage = array_pop($errorMessages); // ...accessing the error details is identical to the direct payment example above }
易付REST API
RestApiService
类提供了对Easypay REST API的抽象,以管理支付。
directPayment(string $paymentId, $operation = 'COMMIT') : DirectPaymentResponse
提交/拒绝或退款直接付款。
- 可用操作:
COMMIT
、REJECT
或REFUND
。
getDirectPayment(string $paymentId) : DirectPaymentResponse
获取有关直接付款的所有信息。
authorizeSubscription(string $authSubscriptionId, $operation = 'COMMIT') : AuthSubscriptionResponse
提交/拒绝/退款/续订或取消已授权的订阅。
- 可用操作:
COMMIT
、REJECT
、REFUND
、RENEW
或CANCEL
。
getAuthorizeSubscription(string $authSubscriptionId) : AuthSubscriptionResponse
获取有关已授权订阅的所有信息。
运行测试
确保已安装dev-dependencies
,然后从vendor
目录执行phpunit
vendor/bin/phpunit tests