话不多说直接上代码:

// app\member\controller\pay.php

<?php

namespace app\controller;

use EasyWeChat\Kernel\Exceptions\Exception;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use support\Request;
use EasyWeChat\Pay\Application;
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

class Index
{
    private $config = [
        'mch_id' => 'xxxx', //根据自已实际的填写
        // 商户证书
        'private_key' => '/xxx/apiclient_key.pem',//根据自已实际的填写,绝对路径
        'certificate' => '/xxx/apiclient_cert.pem', //根据自已实际的填写,绝对路径
        // v3 API 秘钥
        'secret_key' => 'xxxx', //根据自已实际的填写
        // v2 API 秘钥
        'v2_secret_key' => 'xxx', //根据自已实际的填写
        'platform_certs' => [
            '/xxx/wechatpay_public.pem', //根据自已实际的填写,获取办法见下面。
        ],
        'http' => [
            'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启
            'timeout' => 5.0,
            // 'base_uri' => 'https://api.mch.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri
        ],
    ];

    //二维码生成展示页
    public function index(Request $request)
    {
        try {
            $app = new Application($this->config);
            $response = $app->getClient()->post('/v3/pay/transactions/native', [
                'json' => [
                    'mchid' => (string)$app->getMerchant()->getMerchantId(),
                    'out_trade_no' => 'SN_'.time(),
                    'appid' => 'xxxx', //根据自已实际的填写
                    'description' => '测试内容',
                    'notify_url' => 'https://www.xxxx.com/member/wx_native_notify', //注意是ssl协议
                    'amount' => [
                        'total' => 2,
                        'currency' => 'CNY',
                    ]
                ]
            ]);
        } catch (InvalidArgumentException $e) {
            //
        }
        return view('index/index', ['code_url' => $response->toArray()]);
    }


    //回调
    public function verify(Request $request)
    {
        try {
           //下面四行很关键
            $symfony_request = new SymfonyRequest($request->get(), $request->post(), [], $request->cookie(), [], [], $request->rawBody());
            $symfony_request->headers = new HeaderBag($request->header());
            $app = new Application($this->config);
            $app->setRequestFromSymfonyRequest($symfony_request);

            // $app 为你实例化的支付对象,此处省略实例化步骤
            $server = $app->getServer();
            
            // 处理支付结果事件
            $server->handlePaid(function ($message) {
                // $message 为微信推送的通知结果,详看微信官方文档
                file_put_contents(__DIR__ . '/notify_result.txt',
                    date('Y-m-d H:i:s') . ':' . var_export($message, true));
                
                // 微信支付订单号 $message['transaction_id']
                // 商户订单号 $message['out_trade_no']
                // 商户号 $message['mchid']
                // 具体看微信官方文档...
                // 进行业务处理,如存数据库等...

            });
            //这一步特别要注意,必须这么写。
            return $server->serve()->getBody()->getContents();

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

}

“平台证书”申请办法:http://www.liziyu.com/archives/309/

标签: none

添加新评论