comgate / sdk
Comgate PHP SDK
v1.0.18
2024-09-16 06:49 UTC
Requires
- php: ^7.3 || ^8.0
- psr/http-message: ^1.0.1|^2
- psr/log: ^1.1|^2|^3
Requires (Dev)
- codeception/codeception: ^4.2 || ^5.0
- codeception/module-asserts: ^1.3 || ^2.0 || ^3.0
- codeception/module-phpbrowser: ^1.0 || ^2.0
- codeception/stub: ^3.0 || ^4.0
- friendsofphp/php-cs-fixer: ^3.0
- mockery/mockery: ^1.5
- phpstan/phpstan: ^1.11
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-strict-rules: ^1.5
- squizlabs/php_codesniffer: ^3.7
- vlucas/phpdotenv: ^5.5
- v1.0.18
- v1.0.17
- v1.0.16
- v1.0.15
- v1.0.14
- v1.0.13
- v1.0.12
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-master / 0.2.x-dev
- dev-dependabot/composer/codeception/module-phpbrowser-tw-1.0or-tw-2.0or-tw-3.0
- dev-compatibility-fixes-phpstan
- dev-null-as-standelone-type
- dev-detached
- dev-empty-status-email
This package is auto-updated.
Last update: 2024-09-16 06:50:32 UTC
README
💰 用于与 Comgate 进行通信的 PHP 库。
入门指南
安装
要安装最新版本的 comgate/sdk
,请使用 Composer。
composer require comgate/sdk
您必须在您的 PHP 服务器上安装 php-xml 扩展(例如:Ubuntu:apt-get install php-xml)。
文档
注册
- 首先在我们的网站上注册您的账户 comgate.cz。
- 您将获得 商户标识符 和 密钥。
- 在 portal.comgate.cz 上允许您的电商服务器 IPv4 地址。
- 在 portal.comgate.cz 上设置 PAID、CANCELLED、PENDING 和 STATUS URL。
使用方法
设置客户端
use Comgate\SDK\Comgate; $client = Comgate::defaults() ->setMerchant('123456') // get on portal.comgate.cz ->setSecret('foobarbaz') // get on portal.comgate.cz ->createClient();
创建支付
use Comgate\SDK\Entity\Codes\CategoryCode; use Comgate\SDK\Entity\Codes\CurrencyCode; use Comgate\SDK\Entity\Codes\DeliveryCode; use Comgate\SDK\Entity\Codes\PaymentMethodCode; use Comgate\SDK\Entity\Money; use Comgate\SDK\Entity\Payment; use Comgate\SDK\Utils\Helpers; use Comgate\SDK\Entity\Codes\RequestCode; use Comgate\SDK\Exception\ApiException; $payment = new Payment(); $payment ->setPrice(Money::ofInt(50)) // 50 CZK ->setPrice(Money::ofFloat(50.25)) // 50,25 CZK ->setPrice(Money::ofCents(5025)) // 50,25 CZK // ----- ->setCurrency(CurrencyCode::CZK) ->setLabel('Test item') // or ->setParam('label', 'Test item') // you can pass all params like this ->setReferenceId('test001') ->setEmail('foo@bar.tld') ->addMethod(PaymentMethodCode::ALL) //->setRedirect() //->setIframe() ->setTest(false) ->setFullName('Jan Novák') ->setCategory(CategoryCode::PHYSICAL_GOODS_ONLY) ->setDelivery(DeliveryCode::HOME_DELIVERY); try { $createPaymentResponse = $client->createPayment($payment); if ($createPaymentResponse->getCode() === RequestCode::OK) { // Redirect the payer to Comgate payment gateway (use proper method of your framework) Helpers::redirect($createPaymentResponse->getRedirect()); } else { var_dump($createPaymentResponse->getMessage()); } } catch (ApiException $e) { var_dump($e->getMessage()); }
$client->createPayment
的成功响应示例。
$transactionId = $createPaymentResponse->getTransId(); // XXXX-YYYY-ZZZZ $code = $createPaymentResponse->getCode(); // 0 $message = $createPaymentResponse->getMessage(); // OK $redirect = $createPaymentResponse->getRedirect(); // https://payments.comgate.cz/client/instructions/index?id=XXXX-YYYY-ZZZZ
$client->createPayment
的错误响应示例。
$code = $e->getCode(); // 1109 $message = $e->getMessage(); // Invalid payment method [fake]
获取方法
use Comgate\SDK\Exception\ApiException; try { $methodsResponse = $client->getMethods(); foreach ($methodsResponse->getMethodsList() as $method) { var_dump([ $method->getId(), $method->getName(), $method->getDescription(), $method->getLogo(), ]); } } catch (ApiException $e) { var_dump($e->getMessage()); }
完成订单并向返回的付款人显示支付状态
- 示例 PAID URL: https://your-eshop.tld/order-finish.php?id=${id}&refId=${refId}&status=PAID
- 示例 PAID CANCELLED: https://your-eshop.tld/order-finish.php?id=${id}&refId=${refId}&status=CANCELLED
- 示例 PAID PENDING: https://your-eshop.tld/order-finish.php?id=${id}&refId=${refId}&status=PENDING
use Comgate\SDK\Entity\Payment; use Comgate\SDK\Entity\PaymentNotification; use Comgate\SDK\Entity\Codes\PaymentStatusCode; $transactionId = $_GET['id']; // XXXX-YYYY-ZZZZ $refId = $_GET['refId']; // your order number try { $paymentStatusResponse = $client->getStatus($transactionId); switch ($paymentStatusResponse->getStatus()){ case PaymentStatusCode::PAID: // Your code (set order as paid) echo "Your payment was PAID successfully."; break; case PaymentStatusCode::CANCELLED: // Your code (set order as cancelled) echo "Your order was CANCELLED."; break; case PaymentStatusCode::PENDING: // Your code (order is still pending) echo "We are waiting for the payment."; break; case PaymentStatusCode::AUTHORIZED: // Your code (set order as authorized) echo "Payment was authorized successfully."; break; } } catch (ApiException $e) { var_dump($e->getMessage()); }
接收支付通知(服务器到服务器)
示例 URL: https://your-eshop.tld/notify.php
use Comgate\SDK\Entity\PaymentNotification; use Comgate\SDK\Entity\Codes\PaymentStatusCode; use Comgate\SDK\Exception\ApiException; // Create from $_POST global variable // $notification = PaymentNotification::createFromGlobals(); // Create from your framework $data = $framework->getHttpRequest()->getPostData(); $notification = PaymentNotification::createFrom($data); $transactionId = $notification->getTransactionId(); try { // it's important to check the status from API $paymentStatusResponse = $client->getStatus($transactionId); switch ($paymentStatusResponse->getStatus()){ case PaymentStatusCode::PAID: // Your code (set order as paid) break; case PaymentStatusCode::CANCELLED: // Your code (set order as cancelled) break; case PaymentStatusCode::AUTHORIZED: // Your code (set order as authorized) break; // PaymentStatusCode::PENDING - is NOT send via push notification } echo "OK"; // important response with HTTP code 200 } catch (ApiException $e) { var_dump($e->getMessage()); }
创建退款
use Comgate\SDK\Entity\Refund; use Comgate\SDK\Exception\ApiException; use Comgate\SDK\Entity\Money; use Comgate\SDK\Entity\Codes\RequestCode; $refund = new Refund(); $refund->setTransId('XXXX-YYYY-ZZZZ') ->setAmount(Money::ofCents(100)) ->setRefId('11bb22'); try{ $refundResult = $client->refundPayment($refund); if($refundResult->getCode() == RequestCode::OK) { // refund created successfully } } catch (ApiException $e){ var_dump($e->getMessage()); }
调试
日志记录
我们使用 PSR-3 进行日志记录。
use Comgate\SDK\Comgate; use Comgate\SDK\Logging\FileLogger; use Comgate\SDK\Logging\StdoutLogger; $client = Comgate::defaults() ->setLogger(new FileLogger(__DIR__ . '/comgate.log')) ->createClient();
查看我们的 测试 以了解日志记录器格式。
维护
如果您发现一个错误,请直接在 Github 中提交问题。
感谢您使用我们的 Comgate 支付。
许可证
版权所有 (c) 2021 Comgatea a.s. MIT 许可。