zafarjonovich/yii2-payment

此包的最新版本(0.0.1.2)没有可用的许可证信息。

Yii2支付组件

0.0.1.2 2022-06-11 04:09 UTC

This package is auto-updated.

Last update: 2024-09-11 09:04:12 UTC


README

Assalomu aleykum, yaqinda bir ajoyib proektni qildim, unga PAYME to'lov tizimini ulashim keragidi, PAYME ga oldin ulanmaganman. Github ni qarasam yii2 uchun yaxshi paketni topa olmadim, shuning uchun o'zim va boshqalar ishlatishi uchun open source yozdim, bu paket siz uchun ham foydali bo'ladi degan umiddaman.

安装

安装包

通过 Composer 推荐安装

请运行以下命令

composer require zafarjonovich/yii2-payment

yoki composer.json ga quyidagini qo'shing

"zafarjonovich/yii2-payment": "*"

配置包

为使包工作,需要运行以下迁移

php yii migrate --migrationPath="@vendor/zafarjonovich/yii2-payment/src/migrations"

使用

此包中,与支付系统交互的业务逻辑完全与业务逻辑分离。

PAYME 集成

以下是一个示例,例如,您有一个名为 Invoice 的模型,我们的业务逻辑也与此模型相关,我们在项目的 API 模块中创建了一个名为 PaymeController 的控制器,并需要重写以下方法

  • getCredentials -> 返回用于连接 PAYME 的 Credentials 对象
  • getOwnerIdByAccount -> 根据 PAYME 返回的凭证返回 owner id 即模型 id
  • checkPerformTransaction -> 检查交易
  • performTransaction -> 交易成功
  • cancelTransaction -> 交易取消
<?php

namespace api\controllers;


use common\models\Invoice;
use zafarjonovich\Yii2Payment\gateways\payme\base\Credentials;
use zafarjonovich\Yii2Payment\gateways\payme\controllers\Controller;
use zafarjonovich\Yii2Payment\gateways\payme\exceptions\AccountNotFoundException;
use zafarjonovich\Yii2Payment\gateways\payme\exceptions\RequestParseException;

class PaymeController extends Controller
{
    public function getCredentials()
    {
        return new Credentials([
            'login' => env('PAYME_LOGIN'),
            'password' => env('PAYME_PASSWORD')
        ]);
    }

    public function getOwnerIdByAccount($account)
    {
        if (!isset($account['invoice_id'])) {
            throw new RequestParseException('invoice id not found in request');
        }

        $invoice = Invoice::findOne(['id' => $account['invoice_id']]);

        if (null === $invoice || $invoice->status != Invoice::STATUS_SENT) {
            throw new AccountNotFoundException('invoice not found');
        }

        return $invoice->id;
    }

    protected function checkPerformTransaction($ownerId, $amount)
    {
        $invoice = Invoice::findOne(['id' => $ownerId]);
        return $invoice->price * 100 == $amount;
    }

    protected function performTransaction($ownerId)
    {
        $invoice = Invoice::findOne(['id' => $ownerId]);
        $invoice->status = Invoice::STATUS_PAID;
        $invoice->save(false);
    }

    protected function cancelTransaction($ownerId)
    {
        $invoice = Invoice::findOne(['id' => $ownerId]);
        $invoice->status = Invoice::STATUS_CANCELED;
        $invoice->save(false);
    }
}

使用

PAYME 将所有请求发送到单个 URL。在我们的情况下,如果将请求发送到以下 URL,则可以这样做

https://api.domain.com/payme/hook

如果您想更改请求的名称,请执行以下操作

<?php

namespace api\controllers;


use common\models\Invoice;
use zafarjonovich\Yii2Payment\gateways\payme\base\Credentials;
use zafarjonovich\Yii2Payment\gateways\payme\controllers\Controller;
use zafarjonovich\Yii2Payment\gateways\payme\exceptions\AccountNotFoundException;
use zafarjonovich\Yii2Payment\gateways\payme\exceptions\RequestParseException;

class PaymeController extends Controller
{
    ...
    
    public function actionUpdate(){
        return parent::actionHook();
    }
    
    ...
}

然后请求可以这样处理

https://api.domain.com/payme/update