2020年08月24日 下午15:06:07
原文链接:http://note.youdao.com/noteshare?id=315218dd673a75d8d378a50e5c1644a4&sub=367F0B5C0A684CB89997FE6FA3D4AB44
请求交易接口源码
$data = ArrayUtils::getRequestParams($data, [
// 订单号
'orderNo',
// 支付金额
'payAmount',
'openid',
// 商品标题或者商品名称
'productName',
// 支付时间
'payTime'
]);
extract($data);
// 支付回调地址
$notifyUrl = "https://www.siyuquanqiu.com/wxapp-api/common/wxpay-notify";
// 版本 1.0
$params["p0_Version"] = "1.0";
$params["p1_MerchantNo"] = '汇聚支付商户号';
// 订单号
$params["p2_OrderNo"] = $orderNo;
// 订单金额
$params["p3_Amount"] = $payAmount;
// 交易货币 1代表人民币
$params["p4_Cur"] = "1";
$params["p5_ProductName"] = $productName;
// 支付回调地址
$params["p9_NotifyUrl"] = $notifyUrl;
// 交易类型 微信小程序
$params["q1_FrpCode"] = "WEIXIN_XCX";
// 用户的openId
$params["q5_OpenId"] = $openid;
// 小程序appId
$params["q7_AppId"] = '';
// 汇聚支付报备商户号
$params['qa_TradeMerchantNo'] = '汇聚支付报备商户号';
// MD5加密
$hmacVal = urlencode($this->hmacRequest($params, '汇聚支付商户秘钥'));
$params['hmac'] = $hmacVal;
// curl 用 POST方法
$result = $this->app_curl("https://www.joinpay.com/trade/uniPayApi.action", $params);
$result = json_decode($result, true);
// 返回码 100 成功
if ($result['ra_Code'] != 100) {
$msg = $result['rb_CodeMsg'];
$logger->debug('error :'.$msg);
apiException("支付失败");
}
$data = json_decode($result['rc_Result'], true);
return $data;
curl网络请求方法
/**
* @param $url
* @param array $params
* @return mixed
*/
private function app_curl($url, array $params) {
// 初使化init方法
$ch = curl_init();
// 指定URL
curl_setopt($ch, CURLOPT_URL, $url);
// 设定请求后返回结果
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 声明使用POST方式来进行发送
curl_setopt($ch, CURLOPT_POST, 1);
// 发送什么数据呢
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
// 忽略证书
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// 忽略header头信息
curl_setopt($ch, CURLOPT_HEADER, 0);
// 设置超时时间
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// 发送请求
$output = curl_exec($ch);
// 关闭curl
curl_close($ch);
// 返回数据
return $output;
}
md5以及base64加密签名方法
/**
* @param $params
* @param $key
* @param string $encryptType
* @return string
*/
private function hmacRequest($params, $key, $encryptType = "1") {
if ("1" == $encryptType) {
return md5(implode("", $params) . $key);
} else {
$private_key = openssl_pkey_get_private($key);
$params = implode("", $params);
openssl_sign($params, $sign, $private_key, OPENSSL_ALGO_MD5);
openssl_free_key($private_key);
$sign = base64_encode($sign);
return $sign;
}
}
支付回调请求接口,判断订单交易状态
<?php
namespace app\controller\common;
use app\annotation\GetMapping;
use app\annotation\RequestParam;
use app\controller\BaseController;
use app\database\DB;
use app\http\response\HtmlResponse;
use app\logging\Log;
class HuiJuPayNotifyController extends BaseController {
/**
* @GetMapping("/common/wxpay-notify")
* @RequestParam(name="r6_Status", type="string")
* @RequestParam(name="r2_OrderNo", type="string")
* @RequestParam(name="r3_Amount", type="string")
* @RequestParam(name="r7_TrxNo", type="string")
* @RequestParam(name="ra_PayTime", type="string")
*
* @param string $ra_PayTime
* @param string $r7_TrxNo
* @param string $r3_Amount
* @param string $r2_OrderNo
* @param string $r6_Status
* @return HtmlResponse
*/
public function RequestXml($r6_Status, $r2_OrderNo, $r3_Amount, $r7_TrxNo, $ra_PayTime) {
// 支付订单回调错误日志开启
$logger = Log::channel('OrderNotify');
$logger->debug("支付订单回调开始");
if ($r6_Status == 100) {
$status = 1;
} else {
$status = 0;
}
$orderNo = $r2_OrderNo;
$tableName = "orders";
$orderInfo = DB::table($tableName)->where('orderNo', $orderNo)->limit(1)->first();
if (!is_array($orderInfo) || empty($orderInfo) || !empty($orderInfo['payTransId'])) {
return new HtmlResponse('success');
}
$payAmount = bcadd($r3_Amount, 0, 2);
$payTime = urldecode($ra_PayTime);
DB::table($tableName)->where('orderNo', $orderNo)->update([
'status' => $status,
'payTransId' => $r7_TrxNo,
'payAmount' => $payAmount,
'payTime' => $payTime
]);
DB::table('members')->where('id', $orderInfo['memberId'])->update([
'totalXfAmount' => DB::raw("totalXfAmount + {$payAmount}")
]);
return new HtmlResponse('success');
}
}