PHP code example of idarex / pingpp-yii2

1. Go to this page and download the library: Download idarex/pingpp-yii2 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/ */

    

idarex / pingpp-yii2 example snippets


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

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

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

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

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

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


/**
 * @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',
        ],
    ];
}



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