PHP code example of zafarjonovich / yii2-payment

1. Go to this page and download the library: Download zafarjonovich/yii2-payment library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zafarjonovich / yii2-payment example snippets




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);
    }
}




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();
    }
    
    ...
}

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