博客列表 >微信小程序支付(服务端)

微信小程序支付(服务端)

南鸢离梦的博客
南鸢离梦的博客原创
2019年11月12日 15:56:21999浏览

下单示例
<?php
namespace app\miniapp\controller;

class Api extends Controller {
public function pay()
{

  1. // 直接下单
  2. $g[0]['id'] = $gid;
  3. $g[0]['num'] = $num;
  4. $data['qid'] = $qid;
  5. $data['goods'] = json_encode($g);
  6. $data['money'] = $money;
  7. $address = MarketAddress::where('openid',$this->userinfo['openid'])->find();
  8. unset($address['id']); unset($address['qid']); unset($address['uid']);
  9. unset($address['openid']); unset($address['lat']); unset($address['lng']);
  10. $data['ag'] = $address['ag'];
  11. $data['address'] = json_encode($address);
  12. $data['order_number'] = date('YmdHis',time());
  13. $data['openid'] = $this->userinfo['openid'];
  14. $data['uid'] = $this->userId;
  15. $data['time'] = date('Y-m-d H:i:s',time());
  16. $res = Db::name('miniapp_order')->insert($data);
  17. if (!$res){
  18. return json(['code'=>0,'网络不佳,下单失败']);
  19. }
  20. $pay = new WxPay(); // 实例化并调用
  21. $result = $pay->wechatPay($this->userinfo['openid'],$data['money'],'乐尚客生活',$data['order_number']);
  22. return $result;
  23. }

}

封装好的微信支付类
<?php
namespace app\miniapp\controller;

class wxPay
{
//微信支付 微信用户的openid 要***的金额 支付的类型名称 商品名 下单订单号
public function wechatPay($user_openid = ‘’, $ro_pay_price = ‘’, $body_name = ‘’, $order_number = ‘’){

  1. $fee = $ro_pay_price; //举例充值0.01
  2. $appid = 'wxec5fff46b3259747';//如果是公众号 就是公众号的appid
  3. $body = $body_name; // 举例: 服务预约
  4. $mch_id = '123456789'; // 您的商户账号
  5. $nonce_str = $this->nonce_str(); //随机字符串
  6. $notify_url = 'http://xxx.com'; // 回调的url【自己填写,如若回调不成功请注意查看服务器是否开启防盗链,回调地址用http】
  7. $openid = $user_openid; // 用户openid 传参
  8. $out_trade_no = $order_number;//商户订单号
  9. $spbill_create_ip = '122.114.99.99'; // IP白名单
  10. $total_fee = $fee * 100; //因为充值金额最小是1 而且单位为分 如果是充值1元所以这里需要*100
  11. $trade_type = 'JSAPI'; //交易类型 默认
  12. //这里是按照顺序的 因为下面的签名是按照顺序 排序错误 肯定出错
  13. $post['appid'] = $appid;
  14. $post['body'] = $body;
  15. $post['mch_id'] = $mch_id;
  16. $post['nonce_str'] = $nonce_str;//随机字符串
  17. $post['notify_url'] = $notify_url;
  18. $post['openid'] = $openid;
  19. $post['out_trade_no'] = $out_trade_no;
  20. $post['spbill_create_ip'] = $spbill_create_ip;//终端的ip
  21. $post['total_fee'] = $total_fee;//总金额 最低为一块钱 必须是整数
  22. $post['trade_type'] = $trade_type;
  23. $sign = $this->sign($post);//签名
  24. $post_xml = '<xml>
  25. <appid>'.$appid.'</appid>
  26. <body>'.$body.'</body>
  27. <mch_id>'.$mch_id.'</mch_id>
  28. <nonce_str>'.$nonce_str.'</nonce_str>
  29. <notify_url>'.$notify_url.'</notify_url>
  30. <openid>'.$openid.'</openid>
  31. <out_trade_no>'.$out_trade_no.'</out_trade_no>
  32. <spbill_create_ip>'.$spbill_create_ip.'</spbill_create_ip>
  33. <total_fee>'.$total_fee.'</total_fee>
  34. <trade_type>'.$trade_type.'</trade_type>
  35. <sign>'.$sign.'</sign>
  36. </xml> ';
  37. //统一接口prepay_id
  38. $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
  39. $xml = $this->http_request($url, $post_xml);
  40. $array = $this->xml($xml); //全要大写
  41. if($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS'){
  42. $time = time();
  43. $tmp='';//临时数组用于签名
  44. $tmp['appId'] = $appid;
  45. $tmp['nonceStr'] = $nonce_str;
  46. $tmp['package'] = 'prepay_id='.$array['PREPAY_ID'];
  47. $tmp['signType'] = 'MD5';
  48. $tmp['timeStamp'] = "$time";
  49. $data['statu'] = 1;
  50. $data['timeStamp'] = "$time";//时间戳
  51. $data['nonceStr'] = $nonce_str;//随机字符串
  52. $data['signType'] = 'MD5';//签名算法,暂支持 MD5
  53. $data['package'] = 'prepay_id='.$array['PREPAY_ID'];//统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*
  54. $data['paySign'] = $this->sign($tmp);//签名,具体签名方案参见微信公众号支付帮助文档;
  55. $data['out_trade_no'] = $out_trade_no;
  56. }else{
  57. $data['statu'] = 0;
  58. $data['text'] = "错误";
  59. $data['RETURN_CODE'] = $array['RETURN_CODE'];
  60. $data['RETURN_MSG'] = $array['RETURN_MSG'];
  61. }
  62. return json_encode($data);
  63. }
  64. //签名 $data要先排好顺序
  65. private function sign($data)
  66. {
  67. $stringA = '';
  68. foreach ($data as $key => $value) {
  69. if (!$value) continue;
  70. if ($stringA) $stringA .= '&' . $key . "=" . $value;
  71. else $stringA = $key . "=" . $value;
  72. }
  73. $wx_key = 'abcdefghijklmnopqrstuvwxyz123456789';//申请支付后有给予一个商户账号和密码,登陆后自己设置key
  74. $stringSignTemp = $stringA . '&key=' . $wx_key;//申请支付后有给予一个商户账号和密码,登陆后自己设置key
  75. return strtoupper(md5($stringSignTemp));
  76. }
  77. //随机32位字符串
  78. private function nonce_str(){
  79. $result = '';
  80. $str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';
  81. for ($i=0;$i<32;$i++){
  82. $result .= $str[rand(0,48)];
  83. }
  84. return $result;
  85. }
  86. //curl请求啊
  87. function http_request($url,$data = null,$headers=array())
  88. {
  89. $curl = curl_init();
  90. if( count($headers) >= 1 ){
  91. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  92. }
  93. curl_setopt($curl, CURLOPT_URL, $url);
  94. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  95. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  96. if (!empty($data)){
  97. curl_setopt($curl, CURLOPT_POST, 1);
  98. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  99. }
  100. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  101. $output = curl_exec($curl);
  102. curl_close($curl);
  103. return $output;
  104. }
  105. //获取xml
  106. private function xml($xml){
  107. $p = xml_parser_create();
  108. xml_parse_into_struct($p, $xml, $vals, $index);
  109. xml_parser_free($p);
  110. $data = "";
  111. foreach ($index as $key=>$value) {
  112. if($key == 'xml' || $key == 'XML') continue;
  113. $tag = $vals[$value[0]]['tag'];
  114. $value = $vals[$value[0]]['value'];
  115. $data[$tag] = $value;
  116. }
  117. return $data;
  118. }

}

支付完回调

  1. <?php

namespace app\miniapp\controller;

class Notify extends Controller {
// 支付回调
public function notify(){
$testxml = file_get_contents(“php://input”);
$jsonxml = json_encode(simplexml_load_string($testxml, ‘SimpleXMLElement’, LIBXML_NOCDATA));

  1. $result = json_decode($jsonxml, true);//转成数组,
  2. if($result){
  3. //如果成功返回了
  4. if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
  5. // 这里写回调更新支付状态以及你的业务逻辑
  6. // 告知微信回调成功
  7. echo 'SUCCESS';
  8. }
  9. }
  10. }

}

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议