通过前面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);
}
标签:arr,code,商户,url,微信,thinkphp6,error,message,data
From: https://blog.csdn.net/m0_63603104/article/details/142586063