PHPackages                             idarex/pingpp-yii2 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. idarex/pingpp-yii2

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

idarex/pingpp-yii2
==================

ping plus plus yii2

v1.2(9y ago)202.4k7[3 issues](https://github.com/idarex/pingpp-yii2/issues)MITPHPPHP &gt;=5.4.0

Since Nov 28Pushed 9y ago4 watchersCompare

[ Source](https://github.com/idarex/pingpp-yii2)[ Packagist](https://packagist.org/packages/idarex/pingpp-yii2)[ RSS](/packages/idarex-pingpp-yii2/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (4)Dependencies (4)Versions (6)Used By (0)

Pingpp Extension for Yii2
=========================

[](#pingpp-extension-for-yii2)

基于 Ping++ 官方的 SDK 进行了简单的封装，用于 Yii2 框架。

[![Build Status](https://camo.githubusercontent.com/2176b7fa3999f9c9969cedb492d5c6a2b3f0ae072502604c801773496f4f2ac8/68747470733a2f2f7472617669732d63692e6f72672f6964617265782f70696e6770702d796969322e737667)](https://travis-ci.org/idarex/pingpp-yii2)[![Code Climate](https://camo.githubusercontent.com/73ac88805448ded8ee0b0924c2d7dcbcb943dd08de41514af6d0a51220abb488/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6964617265782f70696e6770702d796969322f6261646765732f6770612e737667)](https://codeclimate.com/github/idarex/pingpp-yii2)[![Issue Count](https://camo.githubusercontent.com/49f74c92df596beb05d3652445328b326f85aea05ed6d337a76fc22d51263468/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6964617265782f70696e6770702d796969322f6261646765732f69737375655f636f756e742e737667)](https://codeclimate.com/github/idarex/pingpp-yii2)[![Latest Stable Version](https://camo.githubusercontent.com/9a6646a4884c9b0799bfe64fd5d82cecbfe947be80528c7ee5d64f62e25fcea2/68747470733a2f2f706f7365722e707567782e6f72672f6964617265782f70696e6770702d796969322f76657273696f6e)](https://packagist.org/packages/idarex/pingpp-yii2)[![Latest Unstable Version](https://camo.githubusercontent.com/9456635efb85c07c0818e486bd22ef3497c6d19432aad6da36f627f2563075e4/68747470733a2f2f706f7365722e707567782e6f72672f6964617265782f70696e6770702d796969322f762f756e737461626c65)](//packagist.org/packages/idarex/pingpp-yii2)[![Total Downloads](https://camo.githubusercontent.com/9f5de8ceda8f7401691d584e0c53ad0af8e0c59e06f049a961711c24fd554e87/68747470733a2f2f706f7365722e707567782e6f72672f6964617265782f70696e6770702d796969322f646f776e6c6f616473)](https://packagist.org/packages/idarex/pingpp-yii2)[![License](https://camo.githubusercontent.com/6dd71ef03f8b1d7290cb559a1e77588660637f3b3c46553f30288e9094113540/68747470733a2f2f706f7365722e707567782e6f72672f6964617265782f70696e6770702d796969322f6c6963656e7365)](https://packagist.org/packages/idarex/pingpp-yii2)

[CHANGE LOG](CHANGELOG.md)

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

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

```

or add

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

to the `require` section of your composer.json.

Configuration
-------------

[](#configuration)

To use this extension, simply add the following code in your application configuration:

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

使用
--

[](#使用)

#### 支付

[](#支付)

##### 付款

[](#付款)

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

##### 查询

[](#查询)

查询单笔交易

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

查询交易列表

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

查询单笔退款

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

查询退款列表

```
$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](#configuration)

如果使用微信 JS-SDK 来调起支付，需要在创建 charge 后，获取签名（signature），传给 HTML5 SDK。

1. [创建Charge](#%E4%BB%98%E6%AC%BE)
2. 获取 Wechat 支付 Signature: ```
        $wechatSignature = $chargeForm->getWechatSignature();
        $charge = $chargeForm->getCharge(true);
    }```

    ```
3. 在 HTML5 SDK 里调用 `pingpp.createPayment(charge, callback, signature, false);`

#### Event 查询

[](#event-查询)

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

#### Event 列表查询

[](#event-列表查询)

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

#### 微信企业付款

[](#微信企业付款)

##### 付款

[](#付款-1)

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

##### 查询

[](#查询-1)

查询 Transfer 列表

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

查询指定 Transfer

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

#### 接收 Webhooks 通知

[](#接收-webhooks-通知)

##### Configuration

[](#configuration-1)

Modify your controler, add or change methode `actions()`

```
/**
 * @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 业务

[](#写自己的-webhook-业务)

\#file: common/components/PingppHooks.php

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

```
