mludvik/yii2-gopay 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mludvik/yii2-gopay

Composer 安装命令:

composer require mludvik/yii2-gopay

包简介

Yii2 integration for official [GoPay's PHP SDK for Payments REST API](https://github.com/gopaycommunity/gopay-php-api).

README 文档

README

Yii2 integration for official GoPay's PHP SDK for Payments REST API.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require mludvik/yii2-gopay:"~1.0"

or add

"mludvik/yii2-gopay": "~1.0"

to the require section of your composer.json.

Usage

Before you start using this extension, it is recommended to read GoPay REST API documentation.

GoPayComponent

GoPayComponent is thin wrapper over GoPay PHP SDK.

Enable it by adding respective component configuration.

'components' => [

  ...

  'goPay' => [
    'class' => 'mludvik\gopay\GoPayComponent',
    'config' => [
      'goid' => '...',
      'clientId' => '...',
      'clientSecret' => '...',
      'isProductionMode' => YII_ENV_PROD,
      'scope' => TokenScope::ALL,
      'language' => Language::ENGLISH,
      'timeout' => 30,
    ],
  ],

  ...
]

Then you can call any method you would call on Payments object.

use Yii;

Yii::$app->goPay->createPayment([...]);
Yii::$app->goPay->getStatus(...);
...

GoPayAsset

GoPayAsset imports front-end GoPay dependencies to your pages. It is sensitive to YII_EVN constant — loads production version when YII_ENV === 'prod', test version otherwise. There are multiple ways how to use it.

  1. To load it on all pages, register it in AppAsset.
class AppAsset extends AssetBundle {

  ...

  public $depends = [
    ...
    'mludvik\gopay\GoPayAsset',
  ];
}
  1. To load it on specific page, register it in respective view.
use mludvik\gopay\GoPayAsset;

GoPayAsset::register($this);
  1. When using GoPayForm described below, you do not need to register GoPayAsset at all. It will be registered automatically on pages where GoPayForm is rendered.

GoPayForm

GoPayForm is widget created to help you build checkout page. Use it if you like or develop your own widget with the help of GoPayComponent and GoPayAsset.

GoPayForm is basically ActiveForm with 2 differences.

  • It uses asynchronous POST request to submit data to controller and then redirects user to payment gateway.
  • It has static method response, which can be used in controller to build expected response to form submission.

Example

First of all, you will need form to collect user payment preferences (eg. preferred payment method). It is also a good place to put some business logic in.

namespace app\models;

use Yii;
use yii\base\Model;

class CheckoutForm extends Model {

  public $paymentMethod;

  ...

  public function createPayment() {
    // Build request based on form's data.
    $request = [...];

    // Establish payment on GoPay side.
    return Yii::$app->goPay->createPayment($request);
  }
}

Then create checkout action.

namespace app\controllers;

use Yii;
use yii\web\Controller;
use mludvik\gopay\GoPayForm;
use app\models\CheckoutForm;

class PaymentController extends Controller {

  ...

  public function actionCheckout() {
    $model = CheckoutForm;

    if(Yii::$app->request->isAjax) {
      if($model->load(Yii::$app->request->post()) && $model->validate()) {
        $response = $model->createPayment();
        return GoPayForm::response($response);
      } else {
        return GoPayForm::response(null);
      }
    }

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

Finally, create the view.

<?php use mludvik\gopay\GoPayForm; ?>

<?php $form = GoPayForm::begin(); ?>

  ...

  <?= Html::submitButton('Pay') ?>
<?php GoPayForm::end(); ?>

统计信息

  • 总下载量: 395
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-08-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固