2017 © Pedro Peláez
 

library pingpp-yii2

ping plus plus yii2

image

idarex/pingpp-yii2

ping plus plus yii2

  • Friday, May 13, 2016
  • by bobchengbin
  • Repository
  • 4 Watchers
  • 21 Stars
  • 2,353 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 7 Forks
  • 3 Open issues
  • 6 Versions
  • 1 % Grown

The README.md

Pingpp Extension for Yii2

基于 Ping++ 官方的 SDK 进行了简单的封装,用于 Yii2 框架。, (*1)

Build Status Code Climate Issue Count Latest Stable Version Latest Unstable Version Total Downloads License, (*2)

CHANGE LOG, (*3)

Installation

The preferred way to install this extension is through composer., (*4)

Either run, (*5)

composer require --prefer-dist idarex/pingpp-yii2

or add, (*6)

"idarex/pingpp-yii2": "dev-master"

to the require section of your composer.json., (*7)

Configuration

To use this extension, simply add the following code in your application configuration:, (*8)

return [
    //....
    'components' => [
        'pingpp' => [
            'class' => '\idarex\pingppyii2\PingppComponent',
            'apiKey' => '<YOUR_API_KEY>',
            'appId' => '<YOUR_APP_ID>',
            // !important 微信公众号付款须设置 wxAppId 和 wxAppSecret
            // 'wxAppId' => '<YOUR_WX_APP_ID>',
            // 'wxAppSecret' => '<YOUR_WX_APP_SECRET>',
            // 'privateKeyPath' => '<YOUR_RSA_PRIVATE_KEY_PATH>', // 设置这个了就不用设置 privateKey 了
            // 'privateKey' => '<YOUR_RSA_PRIVATE_KEY_CONTENT>',
        ],
    ],
];

使用

支付

付款
use Yii;
use yii\web\ServerErrorHttpException;
use idarex\pingppyii2\Channel;
use idarex\pingppyii2\ChargeForm;

$chargeForm = new ChargeForm();
$chargeForm->order_no = '123456789';
$chargeForm->amount = '100';
/**
 * @see Channel
 */
$chargeForm->channel = Channel::WX;
$chargeForm->currency = 'cny';
$chargeForm->client_ip = Yii::$app->getRequest()->userIP;
$chargeForm->subject = 'Your Subject';
$chargeForm->body = 'Your body';

if ($chargeForm->create()) {
    return $chargeForm->getCharge(true);
} elseif ($chargeForm->hasErrors()) {
    var_dump($chargeForm->getErrors());
} else {
    throw new ServerErrorHttpException();
}
退款
\Yii::$app->pingpp->refunds($chId, $amount, $description);
查询

查询单笔交易, (*9)

\Yii::$app->pingpp->retrieve($chId);

查询交易列表, (*10)

$params = ['limit' => 1,];
\Yii::$app->pingpp->chargeList($params);

查询单笔退款, (*11)

\Yii::$app->pingpp->refundRetrieve($chId, $refundId);

查询退款列表, (*12)

$params = ['limit' => 1];
\Yii::$app->pingpp->refundRetrieveList($chId, $params);

红包

发送红包
use yii\web\ServerErrorHttpException;
use idarex\pingppyii2\RedEnvelopeForm;

$postData = [
    'order_no' => '2022222222016',
    'amount' => 200,
    'channel' => 'wx',
    'currency' => 'cny',
    'subject' => 'idarex pingpp-yii2 tests',
    'body' => 'idarex pingpp-yii2 tests body',
    'nickname' => 'bob',
    'sendName' => 'bob',
    'recipient' => 'bobchengbin',
];

$form = new RedEnvelopeForm();
$form->load($postData, '');

if ($form->create()) {
    return $form->getData(true);
} elseif ($form->hasErrors()) {
    var_dump($form->getErrors());
} else {
    throw new ServerErrorHttpException();
}
查询指定微信红包
\Yii::$app->pingpp->redEnvelopeRetrieve($redId);
查询微信红包列表
$params = ['limit' => 1,];
\Yii::$app->pingpp->redEnvelopeList($params);
微信公众号签名获取

配置微信公众号 AppId 和 AppSecret, (*13)

如果使用微信 JS-SDK 来调起支付,需要在创建 charge 后,获取签名(signature),传给 HTML5 SDK。, (*14)

  1. 创建Charge
  2. 获取 Wechat 支付 Signature: if ($chargeForm->create()) { $wechatSignature = $chargeForm->getWechatSignature(); $charge = $chargeForm->getCharge(true); }
  3. 在 HTML5 SDK 里调用 pingpp.createPayment(charge, callback, signature, false);

Event 查询

\Yii::$app->pingpp->eventRetrieve($eventId);

Event 列表查询

$params = ['type' => 'charge.succeeded'];
\Yii::$app->pingpp->eventList($params);

微信企业付款

付款
use yii\web\ServerErrorHttpException;
use idarex\pingppyii2\TransferForm;

$postData = [
    'amount' => 100,
    'order_no' => '20160419',
    'currency' => 'cny',
    'channel' => 'wx_pub',
    'type' => 'b2c',
    'recipient' => 'o9zpMs9jIaLynQY9N6yxcZ',
    'description' => 'testing',
    'user_name' => 'User Name',
    'force_check' => true,
];

$form = new TransferForm();
$form->load($postData, '');

if ($form->create()) {
    return $form->getData(true);
} elseif ($form->hasErrors()) {
    var_dump($form->getErrors());
} else {
    throw new ServerErrorHttpException();
}
查询

查询 Transfer 列表, (*15)

$params = ['limit' => 1];
\Yii::$app->pingpp->transferList($params);

查询指定 Transfer, (*16)

\Yii::$app->pingpp->transferRetrieve($transferId);

接收 Webhooks 通知

Configuration

Modify your controler, add or change methode actions(), (*17)


/** * @inheritdoc */ public function beforeAction($action) { if ($action->id == 'pingpp-hooks') { // 当用户完成交易后 Ping++ 会以 POST 方式把数据发送到你的 hook 地址 // 所以这时候需要临时关闭掉 Yii 的 CSRF 验证 Yii::$app->controller->enableCsrfValidation = false; } return parent::beforeAction($action); } public function actions() { return [ // ... 'pingpp-hooks' => [ 'class' => '\idarex\pingppyii2\HooksAction', 'pingppHooksComponentClass' => 'common\components\PingppHooks', 'publicKeyPath' => '@common/config/pingpp_rsa_public_key.pem', ], ]; }
写自己的 Webhook 业务

#file: common/components/PingppHooks.php, (*18)

  • 使用 $this->event 来访问 Ping++ 提交过来的数据
  • Yii::$app->getResponse()->data = ''; 来给返回值赋值。
  • 方法最后调用 Yii::$app->end(); 来结束请求。
<?php

namespace common\components;

use idarex\pingppyii2\Hooks;
use idarex\pingppyii2\HooksInterface;
use Yii;

class PingppHooks extends Hooks implements HooksInterface
{
    /**
     * @inheritdoc
     */
    public function onAvailableDailySummary()
    {
        Yii::$app->end();
    }

    /**
     * @inheritdoc
     */
    public function onAvailableWeeklySummary()
    {
        Yii::$app->end();
    }

    /**
     * @inheritdoc
     */
    public function onAvailableMonthlySummary()
    {
        Yii::$app->end();
    }

    /**
     * @inheritdoc
     */
    public function onSucceededCharge()
    {
        $orderId = $this->event->data->object->order_no;
        Yii::$app->getResponse()->data = 'finished job';
        Yii::$app->end();
    }

    /**
     * @inheritdoc
     */
    public function onSucceededRefund()
    {
        Yii::$app->end();
    }

    /**
     * @inheritdoc
     */
    public function onSucceededTransfer()
    {
        Yii::$app->end();
    }

    /**
     * @inheritdoc
     */
    public function onSentRedEnvelope()
    {
        Yii::$app->end();
    }

    /**
     * @inheritdoc
     */
    public function onReceivedRedEnvelope()
    {
        Yii::$app->end();
    }
}

Tricks

  • 给配置的组件加 IDE 自动补全 IDE autocompletion for custom components
  • 手动标记一个测试环境的订单为已支付,使用 GET 请求 https://api.pingxx.com/notify/charges/CHARGE_ID?livemode=false
  • 调用组件的相应方法后,会有对该接口返回值的对象属性自动补全功能

The Versions

13/05 2016

dev-master

9999999-dev

ping plus plus yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

13/05 2016

dev-tryToFixTravis

dev-tryToFixTravis

ping plus plus yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

13/05 2016

v1.2

1.2.0.0

ping plus plus yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

30/04 2016

v1.2-alpha

1.2.0.0-alpha

ping plus plus yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

19/04 2016

v1.1

1.1.0.0

ping plus plus yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

28/11 2015

v1.0

1.0.0.0

ping plus plus yii2

  Sources   Download

MIT

The Requires

 

The Development Requires