PHP实现小程序微信支付v3版本退款,以及对退款订单进行查询
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
PS:本篇文章仅用作对小程序微信支付v3版本的退款流程以及对退款订单进行查询的流程展示,如要用于实际,还请自行修改代码
文章中调用的API_Connect.php 与API_v3Connect.php相对应的链接如下:
API_Connect: https://blog.csdn.net/DLH_C/article/details/125912347
API_v3Connect: https://blog.csdn.net/DLH_C/article/details/126008134
小程序端JS代码:
v3refund(){
wx.request({
url: 'http://127.0.0.1:2908/wxPayV3/refundOrder/refundAction.php', //此处修改为你的PHP文件url地址
method:'GET',
header:{
'content-type':'application/json'
},
data:{
'refund':'1', //此处写退款金额,单位为分
'total':'1', //此处写订单总金额,单位为分
'out_trade_no':'此处写你的商户订单号',
},
success(res){
console.log(res)
}
})
},
PHP类代码:
refundOrder.php
<?php
require_once ('../API_v3Connect.php');
require_once ('../../wxPayV2/API_Connect.php');
class refundOrder
{
/**
* 返回请求数据的json格式
* @param $out_trade_no :商户订单号
* @param $out_refund_no :商户退款订单号
* @param $refund :退款金额
* @param $total :原订单总金额
* @return false|string
*/
public function refundBody($out_trade_no,$out_refund_no,$refund,$total){
$data = array(
'out_trade_no' => $out_trade_no,
'out_refund_no' => $out_refund_no,
'notify_url' => '此处写你的回调地址url',
'amount' => [
'refund' => $refund,
'total' => $total,
'currency' => 'CNY'
]
);
return json_encode($data);
}
/**
* 返回商户退款订单号,商家自定义,此处使用日期+时间戳+4位随机数
* @return string
*/
public function getRefundOrder(): string
{
$basicData = '1234567890';
str_shuffle($basicData);
$disposeData = substr($basicData,0,4);
return date('YmdHis').time().$disposeData;
}
/**
* 返回32位随机字符串
* @return string
*/
public function getNonce_str(): string
{
$getData = new v2Connect;
return $getData -> nonce_str();
}
/**
* 返回参与签名数据的主体
* @param $nonce_str :32位随机字符串
* @param $requestBody :请求数据主体,json格式
* @return string
*/
public function getRefundSignBody($nonce_str,$requestBody): string
{
$disposeUrl = '/v3/refund/domestic/refunds';
$data = array(
'POST',$disposeUrl,time(),$nonce_str,$requestBody,''
);
return join("\n",$data);
}
/**
* 返回退款所需的签名值
* @param $data :参与签名的数据主体
* @return string
*/
public function getSignature($data): string
{
$getData = new API_v3Connect;
return $getData -> getSignature($data);
}
/**
* 返回curl请求接口所需的HTTPHEADER
* @param $nonce_str :32位随机字符串
* @param $signature :退款所需的签名值
* @return array
*/
public function getHeader($nonce_str,$signature): array
{
$getData = new API_v3Connect;
return $getData -> getHeader($nonce_str,$signature);
}
public function requestRefund($header,$data){
$getData = new API_v3Connect;
$url = 'https://api.mch.weixin.qq.com/v3/refund/domestic/refunds';
return $getData -> curlRequest($url,$header,'POST',$data);
}
}
PHP调用方法的代码:
refundAction.php
<?php
require_once ('./refundOrder.php');
$getData = new refundOrder;
$refund = $_GET['refund']; //接收小程序端发送的退款金额
$total = $_GET['total']; //接收小程序端发送的订单总金额
$out_trade_no = $_GET['out_trade_no']; //接收小程序端发送的商户订单号
$refund = (int)$refund; //将接收到的退款金额转换为int数据类型
$total = (int)$total; //将接受的微信订单金额转化为int数据类型
var_dump($refund);
var_dump($total);
$nonce_str = $getData -> getNonce_str(); //获取32位随机字符串
$out_refund_no = $getData -> getRefundOrder(); //获取商户退款订单号
$requestBody = $getData -> refundBody($out_trade_no,$out_refund_no,$refund,$total); //获取请求数据的主体
$signBody = $getData -> getRefundSignBody($nonce_str,$requestBody); //获取参与签名的数据的主体
$signature = $getData -> getSignature($signBody); //获取签名值
$header = $getData -> getHeader($nonce_str,$signature); //获取curl请求所需要的HTTPHEADER请求头
$resultData = $getData -> requestRefund($header,$requestBody); //获取微信退款订单返回的数据
echo $resultData;
file_put_contents('./refundReturnData.json',$resultData."\n\r",FILE_APPEND); //将微信退款订单返回的数据存入相应的文件中
以上是微信支付v3版本的退款流程,其中部分数据我输出到了相应的文件,是为了看一下返回的数据结构,请按照个人需求自行保留或者删除,其中退款成功时微信返回的JSON数据如下所示
{
"amount": {
"currency": "CNY",
"discount_refund": 0,
"from": [],
"payer_refund": 1,
"payer_total": 1,
"refund": 1,
"settlement_refund": 1,
"settlement_total": 1,
"total": 1
},
"channel": "ORIGINAL",
"create_time": "2022-07-27T19:10:19+08:00",
"funds_account": "AVAILABLE",
"out_refund_no": "2022072719101816589202181234",
"out_trade_no": "20220726165882643607854",
"promotion_detail": [],
"refund_id": "此处显示微信退款订单号",
"status": "PROCESSING",
"transaction_id": "4200001497202207269720733062",
"user_received_account": "支付用户零钱"
}
为了个人隐私,此处我将相关信息隐去,并不影响观看返回数据的数据结构
以下是对微信支付v3版本退款订单的查询
小程序端js代码:
queryRefund(){
wx.request({
url: 'http://127.0.0.1:2908/wxPayV3/refundOrder/queryRefundOrderAction.php',//此处填写你的PHP文件url地址
method:'POST',
header:{
'content-type':'application/x-www-form-urlencoded'
},
data:{
'out_refund_no':'商户退款单号'
},
success(res){
console.log(res)
}
})
},
PHP类代码
queryRefundOrder.php
<?php
require_once ('../../wxPayV2/API_Connect.php');
require_once ('../API_v3Connect.php');
class queryRefundOrder
{
/**
* 返回生成32位随机字符串
* @return string
*/
public function getNonce_str(): string
{
$getData = new v2Connect;
return $getData -> nonce_str();
}
/**
* @param $out_refund_no : 商户退款单号
* @param $nonce_str :32位随机字符串
* @return string
*/
public function getSignBody($out_refund_no,$nonce_str): string
{
$disposeUrl = "/v3/refund/domestic/refunds/$out_refund_no";
$data = array(
'GET',$disposeUrl,time(),$nonce_str,'',''
);
return join("\n",$data);
}
/**
* 返回查询退款订单所需的签名值
* @param $data :参与签名的数据主体,即方法getSignBody的返回值
* @return string
*/
public function getSign($data){
$getData = new API_v3Connect;
return $getData -> getSignature($data);
}
/**
* 返回请求接口所需的HTTPHEADER
* @param $nonce_str :32位随机字符串
* @param $signature :查询退款订单的签名值
* @return array
*/
public function getHeader($nonce_str,$signature): array
{
$getData = new API_v3Connect;
return $getData -> getHeader($nonce_str,$signature);
}
/**
* 返回curl请求接口微信返回的json数据
* @param $out_refund_no :商户退款订单号
* @param $header :curl请求接口所需要的HTTPHEADER
* @return bool|string
*/
public function curlRequest($out_refund_no,$header){
$getData = new API_v3Connect;
$url = "https://api.mch.weixin.qq.com/v3/refund/domestic/refunds/$out_refund_no";
return $getData -> curlRequest($url,$header);
}
}
PHP调用方法的相关代码:
queryRefundOrderAction.php
<?php
require_once ('./queryRefundOrder.php');
$out_refund_no = $_POST['out_refund_no']; //接收来自小程序端的商户退款单号
$getData = new QueryRefundOrder;
$nonce_str = $getData ->getNonce_str(); //获取32位随机字符串
$signBody = $getData -> getSignBody($out_refund_no,$nonce_str); //获取参与签名的数据主体
$signature = $getData -> getSign($signBody); //获取查询退款订单所需要的签名值
$header = $getData -> getHeader($nonce_str,$signature); //获取curl请求退款订单查询接口的HTTPHEADER
$result = $getData -> curlRequest($out_refund_no,$header); //获取curl请求接口后微信返回的数据
echo $result;
file_put_contents('./queryRefundReturn.json',$result."\n\r",FILE_APPEND); //将微信返回的数据存入向对应的文件
以上就是PHP实现查询小程序微信支付退款订单数据的相关代码
注:退款订单完成时微信就会返回相关的数据,这些数据与你后期通过退款订单查询接口查询到的数据是一样的,所以只要将微信返回的数据做好相关处理,是不需要再次对退款订单进行相关查询的(个人意见,仅供参考)
本文由CSDN用户: 缱绻淡蓝海 原创,代码具有时效性,作者会不定时对文章进行更新