snugcomponents/comgate
Composer 安装命令:
composer require snugcomponents/comgate
包简介
Library in Nette for basic usage of Comgate
README 文档
README
This extension provides basic interface for work with Comgate.
Instalation
Download
The best way to install snugcomponents/comgate, is using Composer:
$ composer require snugcomponents/comgate
Registering and setting
You can enable and configure the extension using your neon config:
extensions:
comgate: Snugcomponents\Comgate\SnugcomponentsComgateExtension
comgate:
eshopIdentifier: %comgate.eshopIdentifier%
password: %comgate.password%
cacheTime: '2 hours' # unrequired
Simply use
At first you need to create payment and send it to Comgate. Comgate then rerurns URL for redirect, or for iframe:
class BasePresenter extends Nette\Application\UI\Presenter
{
#[Inject] public Snugcomponents\Comgate\PaymentFactory $paymentFactory; // Required for creating payment
#[Inject] public Snugcomponents\Comgate\Client $comgateClient; // Required for sending payment to Comgate
public function actionComgateCreatePayment(): void
{
$payment = $this->paymentFactory->create( // For explanation of arguments see Comgate official documentation.
price: 5000,
curr: 'CZK',
label: 'Měsíční licence',
refId: date('Ym') . 'U' . $this->user->getId(),
email: $this->user->getIdentity()->getData()['email'],
prepareOnly: true,
method: 'ALL',
initRecurring: false,
test: !Debugger::$productionMode,
country: 'CZ',
//account: 'afes',
phone: $this->user->getIdentity()->getData()['phone'] ?? null,
name: 'Měsíční licence',
eetReport: false,
eetData: '{}',
preauth: false,
lang: 'cs',
verification: false,
embedded: false,
applePayPayload: null,
);
$redirectUrl = $this->comgateClient->createPayment($payment); // Creating payment. It sends payment to Comgate server and returns URL.
$this->redirectUrl($redirectUrl); // Redirect to received URL.
}
}
Then you need API endpoint, which is called by Comgate with payment result when payment is done:
class BasePresenter extends Nette\Application\UI\Presenter
{
#[Inject] public PaymentSaver $comgatePaymentSaver; // Required for processing comgate request for our API endpoint
public function actionPaymentConfirm(): void
{
try{
$this->comgatePaymentSaver->confirm(); // This will do all the stuff.
} catch (Exception $e) {
// Please log the error and request. May be hack.
$this->getHttpResponse()->setCode(IResponse::S400_BAD_REQUEST);
}
die();
}
}
That is not all. You need to implement some interfaces.
First of them is Snugcomponents\Comgate\Providers\PaymentResponseManipulator:
interface PaymentResponseManipulator
{
/**
* Is responsible for saving response from Comgate.
* When you implement the creating payment and Comgate returns result,
* then the result is forced to save via this method.
* You need to save it somewhere save.
*/
public function savePaymentResponse(PaymentResponse $paymentResponse): void;
/**
* This method is called automatically, when is triggered confirmation of payment
* by ComgatePaymentSaver. It is needed because of integrity of data.
* @param string $transId This is unique identifier of PaymentResponse.
*/
public function getPaymentResponse(string $transId): PaymentResponse;
}
and second is Snugcomponents\Comgate\Providers\ComgatePaymentConfirm:
interface ComgatePaymentConfirm
{
/**
* After successfully confirmed integrity of data from ComgatePaymentSaver, this method is called.
* Inside parameter you have all the data, which comgate provides and their integrity is confirmed.
* You can do with that data whatever you want,
* but we are recommended to save information about that the payment was proceeded.
*/
public function confirm(PaymentConfirmRequestDataProvider $paymentConfirmRequestDataProvider): void;
}
Recurring payments
When you need to use Recurring payments, then you need to implement another interface Snugcomponents\Comgate\Providers\RecurringPaymentResponseManipulator:
interface RecurringPaymentResponseManipulator
{
public function saveRecurringPaymentResponse(RecurringPaymentResponse $recurringPaymentResponse): void;
}
This interface is designed for saving information about recurring payment. The following steps are required for recurring payment to work: (The individual steps are described in detail below)
- User will create classic payment, but with
$initRecurringset to true. - Application saves the
$transIdof this payment. It is CRITICAL that this value is never overwritten, unless you cancel the recurring payment. - When you need to do recurring payment, then application or user will create instance of
Snugcomponents\Comgate\RecurringPayment. This class requires parametr$initRecurringIdwhich is the$transIdfrom previous step. - The application must send this instance to the Comgate server using the
Snugcomponents\Comgate\Client::doRecurringPaymentmethod. - Previous step will automatically call
RecurringPaymentResponseManipulator::saveRecurringPaymentResponsemethod. - If you want to cancel recurring payments, simply delete the
$transIdfrom step 2.
Steps 1 and 2 are described in the Simply use section and steps 5 and 6 are different for each implementation. Step 3 and 4 should look like this:
class BasePresenter extends Nette\Application\UI\Presenter
{
#[Inject] public Snugcomponents\Comgate\RecurringPaymentFactory $recurringPaymentFactory; // Required for creating payment
#[Inject] public Snugcomponents\Comgate\Client $comgateClient; // Required for sending payment to Comgate
public function actionComgateDoRecurringPayment(string $initRecurringId): void
{
$payment = $this->recurringPaymentFactory->create( // This is step 3. For explanation of arguments see Comgate official documentation.
price: 5000,
curr: 'CZK',
label: 'Měsíční licence',
refId: date('Ym') . 'U' . $this->user->getId(),
email: $this->user->getIdentity()->getData()['email'],
prepareOnly: true,
initRecurringId: $initRecurringId, // This variable can be loaded from database etc...
test: !Debugger::$productionMode,
country: 'CZ',
//account: 'afes',
phone: $this->user->getIdentity()->getData()['phone'] ?? null,
name: 'Měsíční licence',
eetReport: false,
eetData: '{}',
);
$this->comgateClient->doRecurringPayment($payment); // This is step 4. Do recurring payment. It sends payment to Comgate server and forces saving of response.
}
}
In recurring payment there is no redirection, so there is no confirm endpoint. The payment is made in the background, so the result of the payment is immediately forwarded in response from Comgate and forced save via step 5.
That's all. Enjoy.
Conclusion
This extension requires PHP8.1 and Nette3.1, and it is property of SnugDesign © 2021
snugcomponents/comgate 适用场景与选型建议
snugcomponents/comgate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 03 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 snugcomponents/comgate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 snugcomponents/comgate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2022-03-01