thinkphp6调用微信商户支付-非工厂模式代码开发

33 篇文章 0 订阅
25 篇文章 0 订阅

通过前面10多篇的微信支付开发-支付工厂代码开发我们发现,不管是jsapi支付、app支付、h5支付、native支付或是小程序支付,固定参数基本不变,且又是一家开发。个人建议,最实用的代码输出方式还是采用工具集封装最简单、最直接、最实用。

一、支付产品流程图

二、微信支付工具集代码开发

<?php
/**
 * 微信支付工具集
 * User: 龙哥·三年风水
 * Date: 2024/9/27
 * Time: 9:10
 */
namespace app\service;
use app\BaseError;
class WechatPayment
{
    protected $merchantId = null;// 商户号
    protected $merchantSerialNumber = null;// 商户API证书序列号
    protected $appid = null;// 公众号ID
    protected $merchantPrivateKey = null;// 商户私钥

    /**
     * 初始化参数
     * WechatPayment constructor.
     */

    public function __construct(){
        $this->merchantId = config('pay.wechat.merchant_id');
        $this->appid = config('pay.wechat.app_id');
        $this->merchantSerialNumber = config('pay.wechat.merchant_serial_number');
        $file = file_get_contents('./wechatpay/apiclient_key.pem');
        $this->merchantPrivateKey = openssl_get_privatekey($file);// 读取商户秘钥
    }

    /**
     * 支付/下单
     * User: 龙哥·三年风水
     * Date: 2024/9/27
     * Time: 9:30
     * @ param $url
     * @ param $data
     * @ return mixed
     */

    public function pay($url,$data){
        $data['appid'] = $this->appid;
        $data['mchid'] = $this->merchantId;
        $authorization = $this->getSign($url,$data);
        $res = http_post($url, $authorization, $data);
        $arr = json_decode($res, true);
        if(isset($arr['code'])) {
            $error['code'] = $arr['code'];
            $error['message'] = $arr['message'];
            $error['timestamp'] = time();
            $error['ip'] = get_client_ip();
            file_put_contents('wechat_payment.txt', json_encode($error) . PHP_EOL, FILE_APPEND);
            throw new BaseError($arr['message'], 50000, 200);
        }
        return $arr;
    }

    /**
     * 订单查询
     * User: 龙哥·三年风水
     * Date: 2024/9/27
     * Time: 9:33
     * @ param $url 地址
     * @ return mixed 返回
     */

    public function transac($url){
        $data['appid'] = $this->appid;
        $data['mchid'] = $this->merchantId;
        $authorization = $this->getSign($url,'');
        $res = http_gets($url, $authorization, '');
        $arr = json_decode($res, true);
        if(isset($arr['code'])) {
            $error['code'] = $arr['code'];
            $error['message'] = $arr['message'];
            $error['timestamp'] = time();
            $error['ip'] = get_client_ip();
            file_put_contents('wechat_payment.txt', json_encode($error) . PHP_EOL, FILE_APPEND);
            throw new BaseError($arr['message'], 50000, 200);
        }
        return $arr;
    }

    /**
     * 订单退款
     * User: 龙哥·三年风水
     * Date: 2024/9/27
     * Time: 9:36
     * @ param $url
     * @ param $data
     * @ return mixed
     */

    public function refunds($url,$data){
        $data['appid'] = $this->appid;
        $data['mchid'] = $this->merchantId;
        $authorization = $this->getSign($url,$data);
        $res = http_post($url, $authorization, $data);
        $arr = json_decode($res, true);
        if(isset($arr['code'])) {
            $error['code'] = $arr['code'];
            $error['message'] = $arr['message'];
            $error['timestamp'] = time();
            $error['ip'] = get_client_ip();
            file_put_contents('wechat_payment.txt', json_encode($error) . PHP_EOL, FILE_APPEND);
            throw new BaseError($arr['message'], 50000, 200);
        }
        return $arr;
    }

    /**
     * 获取签名
     * User: 龙哥·三年风水
     * Date: 2024/9/27
     * Time: 9:23
     * @ param $url 地址
     * @ param $data 数据
     * @ return string 返回
     */

    private function getSign($url,$data){
        $timestamp = time();
        $nonce = alnum(12).date('YmdHis', $timestamp) . rand(1000, 9999);
        $url_parts = parse_url($url);
        $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
        $data = json_encode($data);
        $message = 'POST' . "\n" .
            $canonical_url . "\n" .
            $timestamp . "\n" .
            $nonce . "\n" .
            $data . "\n";
        openssl_sign($message, $signature, $this->merchantPrivateKey, "sha256WithRSAEncryption");
        $sign = base64_encode($signature);
        $schema = 'WECHATPAY2-SHA256-RSA2048';
        $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',  $this->merchantId, $nonce, $timestamp, $this->merchantSerialNumber, $sign);
        return "Authorization: " . $schema . " " . $token;
    }

    /**
     * 销毁参数
     */

    public function __destruct(){
        $this->merchantId = null;
        $this->appid = null;
        $this->merchantSerialNumber = null;
        $this->merchantPrivateKey = null;
    }
}

三、测试JSAPI支付订单调用

public function index(){
        //定义参数
        $data['description'] = '运费钱';
        $data['out_trade_no'] = create_order();
        $data['time_expire'] = date('Y-m-d\TH:i:s\+08:00',time()+3600);
        $data['notify_url'] = 'http://www.baidu.com';
        $data['amount'] = [
            'total'     =>  100,
            'currency'  =>  'CNY'
        ];
        $data['payer']  =   [
            'openid'    =>  'oUpF8uMuAJO_M2pxb1Q9zNjWeS6o'
        ];
        $wechatPayment = new WechatPayment();
        $res = $wechatPayment->pay('https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi',$data);
        var_dump($res);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙哥·三年风水

感觉写得好,给创作人一点赏赐呗

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值