首页
友链
壁纸
留言
今日热榜
更多
关于
时光
推荐
精品流量卡
Search
1
都二十多年了,你的梦为什么还没碎!
57,100 阅读
2
2022年5个好用的 BT/ 磁力链接下载工具推荐 |Windows 、安卓系统
37,478 阅读
3
nps内网穿透实现外网访问树莓派
32,440 阅读
4
实践利用宝塔建emlog个人博客-超详细【原创】
26,143 阅读
5
Typecho-Joe-Theme主题帮助文档
24,680 阅读
闲杂乱码
Python
网站源码
微信小程序
娱乐分享
Is相册
软件工具
登录
Search
标签搜索
PHP
HTML
API
Javascript
源码
JS
Vue
Github
CloudFlare
接口
函数
SQL
ASP.NET
MVC
EF
T4模板
后台管理
CDN
微信小程序
MAC
韩小韩
累计撰写
262
篇文章
累计收到
1,307
条评论
首页
栏目
闲杂乱码
Python
网站源码
微信小程序
娱乐分享
Is相册
软件工具
页面
友链
壁纸
留言
今日热榜
关于
时光
推荐
精品流量卡
搜索到
1
篇与
微信支付
的结果
2021-01-15
PHP微信小程序支付工具类封装
微信小程序支付流程小程序登录,获取openid后端调用微信支付统一下单得到预支付交易会话标识prepay_id组装小程序支付所需的几个参数,并进行签名支付成功,主动查询订单支付状态接收微信支付回调通知微信小程序支付<?php /** * 微信小程序支付 * @author Stephen */ namespace App\Helper\Pay; use GuzzleHttp\Client; class WxApp { private $app_id; private $secret; private $mch_id; private $wx_pay_url = 'https://api.mch.weixin.qq.com'; private $pay_uri = '/pay/unifiedorder'; private $order_query_uri = '/pay/orderquery'; public function __construct() { $this->app_id = 'app_id'; $this->secret = 'secret'; $this->mch_id = 'mch_id'; } /** * 下单 * @param float $totalFee * @param string $orderNo * @param string $orderName * @param string $notifyUrl * @param string $openId * @return array * @throws \GuzzleHttp\Exception\GuzzleException|\Exception */ public function pay(float $totalFee, string $orderNo, string $orderName, string $notifyUrl, string $openId) : array { $data = [ 'appid' => $this->app_id, 'mch_id' => $this->mch_id, 'nonce_str' => $this->generateNonceStr(), 'sign_type' => 'MD5', 'body' => $orderName, 'out_trade_no' => $orderNo, 'fee_type' => 'CNY', 'total_fee' => intval($totalFee * 100), // 单位:分 'spbill_create_ip' => request()->getClientIp(), 'time_start' => date('YmdHis'), 'notify_url' => $notifyUrl, 'trade_type' => 'JSAPI', 'openid' => $openId ]; $data['sign'] = $this->generateSignature($data); $payXmlStr = $this->arrayToXml($data); $unifiedOrder = $this->request($this->pay_uri, $payXmlStr); if (!$unifiedOrder) { throw new \Exception('支付请求失败'); } if ($unifiedOrder['return_code'] != 'SUCCESS') { throw new \Exception($unifiedOrder['return_msg'] ?? '支付请求失败'); } if ($unifiedOrder['result_code'] != 'SUCCESS') { throw new \Exception($unifiedOrder['err_code'] ?? '支付请求失败'); } return $this->getWxPayData($unifiedOrder); } /** * 回调 * @return string * @throws \Exception */ public function notify($notify) { if (!$notify) { throw new \Exception('Parse XML Error'); } if ($notify['return_code'] != 'SUCCESS') { throw new \Exception($notify['return_msg'])); } if ($notify['result_code'] != 'SUCCESS') { throw new \Exception($notify['err_code'])); } //签名验证 $signature = $notify['sign']; $array = (array)$notify; unset($array['sign']); if ($signature != $this->generateSignature($array)) { throw new \Exception('签名验证失败'); } return '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>'; } /** * 查询订单 * @param string $orderNo * @param string $wxOrderNo * @return \SimpleXMLElement * @throws \GuzzleHttp\Exception\GuzzleException */ public function query(string $orderNo, ?string $wxOrderNo='') { $data = [ 'appid' => $this->app_id, 'mch_id' => $this->mch_id, 'out_trade_no' => $orderNo, 'nonce_str' => $this->generateNonceStr(), 'sign_type' => 'MD5' ]; $wxOrderNo && $data['transaction_id'] = $wxOrderNo; $data['sign'] = $this->generateSignature($data); $payXmlStr = $this->arrayToXml($data); $unifiedOrder = $this->request($this->order_query_uri, $payXmlStr); if (!$unifiedOrder) { throw new \Exception('订单查询失败'); } if ($unifiedOrder['return_code'] != 'SUCCESS') { throw new \Exception($unifiedOrder['return_msg'] ?? '订单查询失败'); } if ($unifiedOrder['result_code'] != 'SUCCESS') { throw new \Exception($unifiedOrder['err_code'] ?? '订单查询失败'); } return $unifiedOrder; } /** * 生成随机字符串 * @param int|null $length * @return string */ private function generateNonceStr(?int $length = 16) { $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $str = ''; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } /** * 生成签名 * @param array $params * @return string */ private function generateSignature(array $params) { ksort($params, SORT_STRING); $formatStr = $this->formatQueryStr($params); return strtoupper(md5($formatStr)); } private function formatQueryStr(array $params) { $formatStr = ""; foreach ($params as $key => $val) { $formatStr .= "{$key}={$val}&"; } return $formatStr."key=".$this->secret; } private function arrayToXml(array $params) { $xml = "<xml>"; foreach ($params as $key => $val) { if (is_numeric($val)) { $xml .= "<" . $key . ">" . $val . "</" . $key . ">"; } else $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">"; } $xml .= "</xml>"; return $xml; } /** * 微信小程序调用微信支付的参数 * @param $unifiedOrder * @return array */ private function getWxPayData($unifiedOrder) : array { $data = [ 'appId' => $this->app_id, 'timeStamp' => time(), 'nonceStr' => $this->generateNonceStr(), 'package' => "prepay_id=" . $unifiedOrder['prepay_id'], 'signType' => 'MD5' ]; $data['paySign'] = $this->generateSignature($data); return $data; } /** * 微信支付请求 * @param string $url * @param string|null $method * @param string|null $body * @return null|\SimpleXMLElement * @throws \GuzzleHttp\Exception\GuzzleException */ private function request(string $url, string $params) { $url = $this->wx_pay_url . $url; $client = new Client(['verify' => false]); $response = $client->request('POST', $url, [ 'body' => $body ]); $code = $response->getStatusCode(); if ($code === 200) { $content = $response->getBody()->getContents(); return simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA); } return null; } }发起支付请求$totalPrice = 100; //总金额,单位:元 $orderNo = date('Ymd') . rand(10000, 99999); // 订单号 $proName = '产品名称'; $callback = 'https://xx.com/pay/wxapp/callback'; // 支付成功后回调Url $openId = ''; // OPEN ID $wxAppPay = new App\Helper\Pay\WxApp(); // 将支付请求参数直接返回给前端,前端再到微信发起支付请求 return $wxAppPay->pay($totalPrice, $orderNo, $proName, $callback, $openId);支付完成,主动查询订单支付状态$orderNo = $_POST['order_no']; $wxAppPay = new App\Helper\Pay\WxApp(); $response = $wxAppPay->query($orderNo); if ($response['trade_state'] === 'SUCCESS') { // todo 支付成功,修改订单状态 }支付回调$post = file_get_contents('php://input'); $notify = simplexml_load_string($post, 'SimpleXMLElement', LIBXML_NOCDATA); $totalFee = $notify['total_fee']; // 总金额 $cashFee = $notify['cash_fee']; // 支付金额 $outOrderId = $notify['transaction_id']; // 微信订单号 $orderId = $notify['out_trade_no']; // 订单号 $paymentTime = $notify['time_end']; // 完成支付时间 // 回调验签 $wxAppPay = new App\Helper\Pay\WxApp(); $response = $wxAppPay->notify($notify); // todo 完成支付,处理订单
2021年01月15日
1,097 阅读
0 评论
1 点赞