PHP code example of royalcms / pay

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

    

royalcms / pay example snippets




namespace App\Http\Controllers;

use Royalcms\Component\Pay\Pay;
use Royalcms\Component\Pay\Log;

class PayController extends Controller
{
    protected $config = [
        'app_id' => '2018000000000001',
        'notify_url' => 'http://royalcms.cn/notify.php',
        'return_url' => 'http://royalcms.cn/return.php',
        'ali_public_key' => '',
        // 加密方式: **RSA2**  
        'private_key' => '',
        'log' => [ // optional
            'file' => './logs/alipay.log',
            'level' => 'debug'
        ],
        'mode' => 'dev', // optional,设置此参数,将进入沙箱模式
    ];

    public function index()
    {
        $order = [
            'out_trade_no' => time(),
            'total_amount' => '1',
            'subject' => 'test subject - 测试',
        ];

        $alipay = Pay::alipay($this->config)->web($order);

        return $alipay->send();// laravel 框架中请直接 `return $alipay`
    }

    public function returnCallback()
    {
        $data = Pay::alipay($this->config)->verify(); // 是的,验签就这么简单!

        // 订单号:$data->out_trade_no
        // 支付宝交易号:$data->trade_no
        // 订单总金额:$data->total_amount
    }

    public function notifyCallback()
    {
        $alipay = Pay::alipay($this->config);
    
        try{
            $data = $alipay->verify(); // 是的,验签就这么简单!

            // 请自行对 trade_status 进行判断及其它逻辑进行判断,在支付宝的业务通知中,只有交易通知状态为 TRADE_SUCCESS 或 TRADE_FINISHED 时,支付宝才会认定为买家付款成功。
            // 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号;
            // 2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额);
            // 3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email);
            // 4、验证app_id是否为该商户本身。
            // 5、其它业务逻辑情况

            Log::debug('Alipay notify', $data->all());
        } catch (Exception $e) {
            // $e->getMessage();
        }

        return $alipay->success()->send();// laravel 框架中请直接 `return $alipay->success()`
    }
}



namespace App\Http\Controllers;

use Royalcms\Component\Pay\Pay;
use Royalcms\Component\Pay\Log;

class PayController extends Controller
{
    protected $config = [
        'appid' => 'wxxxxxxxxxxxxxxx', // APP APPID
        'app_id' => 'wxxxxxxxxxxxxxxx', // 公众号 APPID
        'miniapp_id' => 'wxxxxxxxxxxxxxxx', // 小程序 APPID
        'mch_id' => '1xxxxxxxx',
        'key' => 'mxxxxxxxxxxxxxxxxxxxxxxxx',
        'notify_url' => 'http://royalcms.cn/notify.php',
        'cert_client' => './cert/apiclient_cert.pem', // optional,退款等情况时用到
        'cert_key' => './cert/apiclient_key.pem',// optional,退款等情况时用到
        'log' => [ // optional
            'file' => './logs/wechat.log',
            'level' => 'debug'
        ],
        'mode' => 'dev', // optional, dev/hk;当为 `hk` 时,为香港 gateway。
    ];

    public function index()
    {
        $order = [
            'out_trade_no' => time(),
            'total_fee' => '1', // **单位:分**
            'body' => 'test body - 测试',
            'openid' => 'oxxxxxxxxxxxxxxxxxxxxx',
        ];

        $pay = Pay::wechat($this->config)->mp($order);

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

    public function notify()
    {
        $pay = Pay::wechat($this->config);

        try{
            $data = $pay->verify(); // 是的,验签就这么简单!

            Log::debug('Wechat notify', $data->all());
        } catch (Exception $e) {
            // $e->getMessage();
        }
        
        return $pay->success()->send();// laravel 框架中请直接 `return $pay->success()`
    }
}