Giter Site home page Giter Site logo

yii2-onpay's Introduction

Yii2 extension for onpay.ru service

Installation

Yii2-onpay can be installed using composer. Run following command to download and install Yii2-onpay:

composer require ejen/yii2-onpay ~1.0 

Configure

config/main.php

    ...
    'components' => [
        ...
        'onpay' => [
            'class' => 'ejen\payment\Onpay',
            'url_success' => 'http://path/to/success/page',
            'url_fail' => 'http://path/to/fail/page',
        ],
        ...
    ],
    ...

config/main-local.php

    ...
    'components' => [
        ...
        'onpay' => [
            'secret_key' => 'YOUR_SECRET_KEY',
            'username' => 'YOUR_USERNAME',
        ],
        ...
    ],
    ...

Api handler example.

This example can pass all OnPay api online tests.

<?php

namespace frontend\controllers;

use ejen\payment\onpay\ApiAction;

class OnpayController extends \yii\web\Controller
{
    public function actions()
    {
        return [
            'api' => [
                'class' => ApiAction::className(),
                'payCallback' => [$this, 'payCallback'],
                'checkCallback' => [$this, 'checkCallback'],
            ],
        ];
    }

    public function payCallback($request, $response)
    {
        $amount = 100;
        $currency = 'RUR';

        if ($request->balance_amount < $amount)
        {
            $response->code = 3;
            $response->comment = 'bad_pay';
            return false;
        }

        if ($request->balance_currency != $currency)
        {
            $response->code = 3;
            $response->comment = 'bad_pay';
            return false;
        }

        return true;
    }

    public function checkCallback($request, $response)
    {
        $amount = 500;
        $currency = 'RUR';

        if ($request->amount < $amount)
        {
            $response->code = 2;
            return false;
        }
        
        if ($request->amount > $amount || $request->order_currency != $currency)
        {
            $response->code = 3;
            return false;
        }

        return true;
    }
}

Payment example

<?php

namespace frontend\controllers;

use Yii;

use frontend\forms\PaymentForm;

class PaymentController extends \yii\web\Controller
{

    public function actionIndex()
    {
        $model = new PaymentForm;
        if ($model->load(Yii::$app->request->post()) && $model->validate())
        {
            $payment = Yii::$app->onpay->createPayment([
                'price' => $model->amount,
                'pay_for' => Yii::$app->user->id,
            ]);

            if ($payment->validate())
            {
                return $this->redirect($payment->getUrl());
            }
        }        

        return $this->render('index', [
            'model' => $model,
        ]);
    }
}

yii2-onpay's People

Contributors

alexeevdv avatar happyendik avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.