Yii pay – Alipay and wechat payment extension package for Yii

Time:2020-6-3

Guanguans / Yii pay – payment extension package developed based on yansongda / pay, which is suitable for Alipay and wechat of Yii.

Environmental requirements

  • Yii >= 2.0

install

$ composer require guanguans/yii-pay -v

to configure

Yii2 profileconfig/main.phpAdd:

'components' => [
    // ...
    'pay' => [
        'class' => 'Guanguans\YiiPay\Pay',
        'wechat' => [
            'appid' => 'wxb3fxxxxxxxxxxx', // APP APPID
            'app_ Id'= >'wxb3fxxxxxxxxxxx', // official account APPID
            'miniapp_ ID '= > wxb3fxxxxxxxxxx', // applet appid
            'mch_id' => '14577xxxx',
            'key' => 'mF2suE9sU6Mk1Cxxxxxxxxxxx',
            'notify_url' => 'http://xxxxxx.cn/notify.php',
            'cert_ client' => './cert/apiclient_ cert.pem '// used for optional, refund, etc
            'cert_ key' => './cert/apiclient_ key.pem '// used for optional, refund, etc
            'log' => [ // optional
                'file' => './logs/wechat.log',
                'level' = > Info ', // it is recommended to adjust the production environment level to info and the development environment to debug
                'type' = > single ', // optional, daily is optional
                'max_ File '= > 30, // optional, valid when type is daily, 30 days by default
            ],
            'http' => [ // optional
                'timeout' => 5.0,
                'connect_timeout' => 5.0,
                //For more configuration items, please refer to [guzzle]( https://guzzle-cn.readthedocs.io/zh_ CN/latest/request- options.html )
            ],
            //'mode' = > dev ', // optional, dev / HK; when it is' HK', it is Hong Kong gateway.
        ],
        'alipay' => [
            'app_id' => '2016082000295641',
            'notify_url' => 'http://xxxxxx.cn/notify.php',
            'return_url' => 'http://xxxxxx.cn/return.php',
            'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuWJKrQ6SWvS6niI+4vEVZiYfjkCfLQfoFI2nCp9ZLDS42QtiL4Ccyx8scgc3nhVwmVRte8f57TFvGhvJD0upT4O5O/lRxmTjechXAorirVdAODpOu0mFfQV9y/T9o9hHnU+VmO5spoVb3umqpq6D/Pt8p25Yk852/w01VTIczrXC4QlrbOEe3sr1E9auoC7rgYjjCO6lZUIDjX/oBmNXZxhRDrYx4Yf5X7y8FRBFvygIE2FgxV4Yw+SL3QAa2m5MLcbusJpxOml9YVQfP8iSurx41PvvXUMo49JG3BDVernaCYXQCoUJv9fJwbnfZd7J5YByC+5KM4sblJTq7bXZWQIDAQAB',
            //Encryption method: * * RSA2**  
            'private_key' => 'MIIEpAIBAAKCAQEAs6+F2leOgOrvj9jTeDhb5q46GewOjqLBlGSs/bVL4Z3fMr3p+Q1Tux/6uogeVi/eHd84xvQdfpZ87A1SfoWnEGH5z15yorccxSOwWUI+q8gz51IWqjgZxhWKe31BxNZ+prnQpyeMBtE25fXp5nQZ/pftgePyUUvUZRcAUisswntobDQKbwx28VCXw5XB2A+lvYEvxmMv/QexYjwKK4M54j435TuC3UctZbnuynSPpOmCu45ZhEYXd4YMsGMdZE5/077ZU1aU7wx/gk07PiHImEOCDkzqsFo0Buc/knGcdOiUDvm2hn2y1XvwjyFOThsqCsQYi4JmwZdRa8kvOf57nwIDAQABAoIBAQCw5QCqln4VTrTvcW+msB1ReX57nJgsNfDLbV2dG8mLYQemBa9833DqDK6iynTLNq69y88ylose33o2TVtEccGp8Dqluv6yUAED14G6LexS43KtrXPgugAtsXE253ZDGUNwUggnN1i0MW2RcMqHdQ9ORDWvJUCeZj/AEafgPN8AyiLrZeL07jJz/uaRfAuNqkImCVIarKUX3HBCjl9TpuoMjcMhz/MsOmQ0agtCatO1eoH1sqv5Odvxb1i59c8Hvq/mGEXyRuoiDo05SE6IyXYXr84/Nf2xvVNHNQA6kTckj8shSi+HGM4mO1Y4Pbb7XcnxNkT0Inn6oJMSiy56P+CpAoGBAO1O+5FE1ZuVGuLb48cY+0lHCD+nhSBd66B5FrxgPYCkFOQWR7pWyfNDBlmO3SSooQ8TQXA25blrkDxzOAEGX57EPiipXr/hy5e+WNoukpy09rsO1TMsvC+v0FXLvZ+TIAkqfnYBgaT56ku7yZ8aFGMwdCPL7WJYAwUIcZX8wZ3dAoGBAMHWplAqhe4bfkGOEEpfs6VvEQxCqYMYVyR65K0rI1LiDZn6Ij8fdVtwMjGKFSZZTspmsqnbbuCE/VTyDzF4NpAxdm3cBtZACv1Lpu2Om+aTzhK2PI6WTDVTKAJBYegXaahBCqVbSxieR62IWtmOMjggTtAKWZ1P5LQcRwdkaB2rAoGAWnAPT318Kp7YcDx8whOzMGnxqtCc24jvk2iSUZgb2Dqv+3zCOTF6JUsV0Guxu5bISoZ8GdfSFKf5gBAo97sGFeuUBMsHYPkcLehM1FmLZk1Q+ljcx3P1A/ds3kWXLolTXCrlpvNMBSN5NwOKAyhdPK/qkvnUrfX8sJ5XK2H4J8ECgYAGIZ0HIiE0Y+g9eJnpUFelXvsCEUW9YNK4065SD/BBGedmPHRC3OLgbo8X5A9BNEf6vP7fwpIiRfKhcjqqzOuk6fueA/yvYD04v+Da2MzzoS8+hkcqF3T3pta4I4tORRdRfCUzD80zTSZlRc/h286Y2eTETd+By1onnFFe2X01mwKBgQDaxo4PBcLL2OyVT5DoXiIdTCJ8KNZL9+kV1aiBuOWxnRgkDjPngslzNa1bK+klGgJNYDbQqohKNn1HeFX3mYNfCUpuSnD2Yag53Dd/1DLO+NxzwvTu4D6DCUnMMMBVaF42ig31Bs0jI3JQZVqeeFzSET8fkoFopJf3G6UXlrIEAQ==',
            //To use public key certificate mode, please configure the following two parameters and modify Ali at the same time_ public_ Key is the Alipay public key certificate path ending with.Crt, such as (./cert/alipayCertPublicKey_ RSA2.crt)
            // 'app_ cert_ public_ key' => './cert/ appCertPublicKey.crt '// application public key certificate path
            // 'alipay_ root_ cert' => './cert/ alipayRootCert.crt // Alipay certificate path
            'log' => [ // optional
                'file' => './logs/alipay.log',
                'level' = > Info ', // it is recommended to adjust the production environment level to info and the development environment to debug
                'type' = > single ', // optional, daily is optional
                'max_ File '= > 30, // optional, valid when type is daily, 30 days by default
            ],
            'http' => [ // optional
                'timeout' => 5.0,
                'connect_timeout' => 5.0,
                //For more configuration items, please refer to [guzzle]( https://guzzle-cn.readthedocs.io/zh_ CN/latest/request- options.html )
            ],
            //'mode' = > dev ', // optional. Set this parameter to enter sandbox mode
        ],
    ],
    // ...
]

use

Get Alipay instance

Yii::$app->pay->alipay();

Alipay uses examples, for more detailed documentation, please refer to yansongda/pay

<?php

namespace frontend\controllers;

use Yii;

class PayController extends Controller
{
    public function actionIndex()
    {
        $order = [
            'out_trade_no' => time(),
            'total_amount' => '1',
            'subject' = > test subject - Test ',
        ];

        $Alipay = Yii:: $app - > pay - > alipay() - > Web ($order); // computer payment
        //$Alipay = Yii:: $app - > pay - > alipay() - > WAP ($order); // Mobile Website Payment
        //$Alipay = Yii:: $app - > pay - > alipay() - > app ($order); // APP payment
        //$Alipay = Yii:: $app - > pay - > alipay() - > POS ($order); // pay by credit card
        //$alipay = Yii:: $app->pay->alipay () ->scan ($order); // code scanning payment
        //$Alipay = Yii:: $app - > pay - > alipay() - > transfer ($order); // account transfer
        //$Alipay = Yii:: $app - > pay - > alipay() - > mini ($order); // applet payment

        return $alipay->send();
    }

    public function actionReturn()
    {
        $data = Yii::$app->pay->alipay()->verify();

        //Order number: $data - > out_ trade_ No
        // Alipay transaction No.: $data->trade_ No
        //Total order amount: $data - > total_ amount
    }

    public function actionNotify()
    {
        $alipay = Yii::$app->pay->alipay();
    
        try{
            $data = $alipay->verify();

            //Please check the trade_ Status judge and judge other logic. In Alipay's business notification, only transaction notification state is TRADE_. Success or trade_ At FINISHED, Alipay will decide to pay the buyer successfully.
            //1. The merchant needs to verify the out in the notice data_ trade_ Whether no is the order number created in the merchant system;
            //2. Judge total_ Whether the amount is the actual amount of the order (i.e. the amount when the merchant order is created);
            //3. Seller in the verification notice_ ID (or seller_ Email) is out_ trade_ No the corresponding operator of this document (sometimes, a merchant may have multiple sellers_ id/seller_ email);
            //4. Verify app_ Whether the ID is the merchant itself.
            //5. Other business logic

        } catch (\Exception $e) {
            // $e->getMessage();
        }

        return $alipay->success()->send();
    }
}

Get wechat instance

Yii::$app->pay->wechat();

Wechat use example, please refer to yansongda / pay for more detailed documents

<?php

namespace frontend\controllers;

use Yii;

class PayController extends Controller
{
    public function actionIndex()
    {
        $order = [
            'out_trade_no' => time(),
            'total_ Fee '= > 1', // * * unit: minute**
            'body' = > test body - Test ',
            'openid' => 'onkVf1FjWS5SBIixxxxxxx',
        ];

        $pay = Yii:: $app->pay->wechat () ->mp ($order); // official account payment
        //$pay = Yii:: $app - > pay - > wechat() - > miniapp ($order); // applet payment
        //$pay = Yii:: $app - > pay - > wechat() - > WAP ($order); // H5 payment
        //$pay = Yii:: $app - > pay - > wechat() - > scan ($order); // scan code to pay
        //$pay = Yii:: $app - > pay - > wechat() - > POS ($order); // pay by credit card
        //$pay = Yii:: $app - > pay - > wechat() - > app ($order); // APP payment
        //$pay = Yii:: $app - > pay - > wechat() - > transfer ($order); // enterprise payment
        //$pay = Yii:: $app - > pay - > wechat() - > Redpack ($order); // ordinary red packet
        //$pay = Yii:: $app - > pay - > wechat() - > groupredpack ($order); // split red packets

        // $pay->appId
        // $pay->timeStamp
        // $pay->nonceStr
        // $pay->package
        // $pay->signType
    }

    public function actionNotify()
    {
        $pay = Yii::$app->pay->wechat();

        try{
            $data = $pay->verify();
        } catch (\Exception $e) {
            // $e->getMessage();
        }
        
        return $pay->success()->send();
    }
}

test

$ composer test

Related links

  • https://github.com/yansongda/pay,yansongda

License

MIT