一起源码网

  • www.171739.xyz
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 高级进阶 查看主题

小程序微信支付php案例

发布者: yun-fan | 发布时间: 2018-6-20 15:40| 查看数: 8450| 评论数: 1|帖子模式

本文方便使用php环境的会员增加一个参考对象

作者:未署名,来自原文地址

前端代码:

  1. wx.request({
  2. url: 'https://www.yourhost.com/weixin/WeiActivity/payJoinfee',//改成你自己的链接
  3. header: {
  4. 'Content-Type': 'application/x-www-form-urlencoded'
  5. },
  6. method:'POST',
  7. success: function(res) {
  8. console.log(res.data);
  9. console.log('调起支付');
  10. wx.requestPayment({
  11. 'timeStamp': res.data.timeStamp,
  12. 'nonceStr': res.data.nonceStr,
  13. 'package': res.data.package,
  14. 'signType': 'MD5',
  15. 'paySign': res.data.paySign,
  16. 'success':function(res){
  17. console.log('success');
  18. wx.showToast({
  19. title: '支付成功',
  20. icon: 'success',
  21. duration: 3000
  22. });
  23. },
  24. 'fail':function(res){
  25. console.log('fail');
  26. },
  27. 'complete':function(res){
  28. console.log('complete');
  29. }
  30. });
  31. },
  32. fail:function(res){
  33. console.log(res.data)
  34. }
  35. });

后端代码:

  1. //支付费用
  2. public function payJoinfee(){
  3. $appid='wx888888888';
  4. $openid='oCQwY0Q_pzrQpu8888888';
  5. $mch_id='141388888';
  6. $key='9A0A86888888888';
  7. import('Weixin.Lib.WeixinPay');
  8. $weixinpay = new \WeixinPay($appid,$openid,$mch_id,$key);
  9. $return=$weixinpay->pay();
  10. $this->response($return,'json');
  11. }

微信支付类

  1. <?php
  2. /*
  3. * 小程序微信支付
  4. */
  5. class WeixinPay{
  6. protected $appid;
  7. protected $mch_id;
  8. protected $key;
  9. protected $openid;
  10. function __construct($appid,$openid,$mch_id,$key){
  11. $this->appid=$appid;
  12. $this->openid=$openid;
  13. $this->mch_id=$mch_id;
  14. $this->key=$key;
  15. }
  16. public function pay(){
  17. //统一下单接口
  18. $return=$this->weixinapp();
  19. return $return;
  20. }
  21. //统一下单接口
  22. private function unifiedorder(){
  23. $url='https://api.mch.weixin.qq.com/pay/unifiedorder';
  24. $parameters=array(
  25. 'appid'=>$this->appid,//小程序ID
  26. 'mch_id'=>$this->mch_id,//商户号
  27. 'nonce_str'=>$this->createNoncestr(),//随机字符串
  28. 'body'=>'测试',//商品描述
  29. 'out_trade_no'=>'2015450806125346',//商户订单号
  30. 'total_fee'=>floatval(0.01*100),//总金额 单位 分
  31. 'spbill_create_ip'=>$_SERVER['REMOTE_ADDR'],//终端IP
  32. 'notify_url'=>'http://www.weixin.qq.com/wxpay/pay.php',//通知地址
  33. 'openid'=>$this->openid,//用户id
  34. 'trade_type'=>'JSAPI'//交易类型
  35. );
  36. //统一下单签名
  37. $parameters['sign']=$this->getSign($parameters);
  38. $xmlData=arrayToXml($parameters);
  39. $return=xmlToArray(postXmlSSLCurl($xmlData,$url,60));
  40. return $return;
  41. }
  42. //微信小程序接口
  43. private function weixinapp(){
  44. //统一下单接口
  45. $unifiedorder=$this->unifiedorder();
  46. $parameters=array(
  47. 'appId'=>$this->appid,//小程序ID
  48. 'timeStamp'=>''.time().'',//时间戳
  49. 'nonceStr'=>$this->createNoncestr(),//随机串
  50. 'package'=>'prepay_id='.$unifiedorder['prepay_id'],//数据包
  51. 'signType'=>'MD5'//签名方式
  52. );
  53. //签名
  54. $parameters['paySign']=$this->getSign($parameters);
  55. return $parameters;
  56. }
  57. //作用:产生随机字符串,不长于32位
  58. private function createNoncestr($length = 32 ){
  59. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  60. $str ="";
  61. for ( $i = 0; $i < $length; $i++ ) {
  62. $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
  63. }
  64. return $str;
  65. }
  66. //作用:生成签名
  67. private function getSign($Obj){
  68. foreach ($Obj as $k => $v){
  69. $Parameters[$k] = $v;
  70. }
  71. //签名步骤一:按字典序排序参数
  72. ksort($Parameters);
  73. $String = $this->formatBizQueryParaMap($Parameters, false);
  74. //签名步骤二:在string后加入KEY
  75. $String = $String."&key=".$this->key;
  76. //签名步骤三:MD5加密
  77. $String = md5($String);
  78. //签名步骤四:所有字符转为大写
  79. $result_ = strtoupper($String);
  80. return $result_;
  81. }
  82. ///作用:格式化参数,签名过程需要使用
  83. private function formatBizQueryParaMap($paraMap, $urlencode){
  84. $buff = "";
  85. ksort($paraMap);
  86. foreach ($paraMap as $k => $v){
  87. if($urlencode)
  88. {
  89. $v = urlencode($v);
  90. }
  91. $buff .= $k . "=" . $v . "&";
  92. }
  93. $reqPar;
  94. if (strlen($buff) > 0){
  95. $reqPar = substr($buff, 0, strlen($buff)-1);
  96. }
  97. return $reqPar;
  98. }
  99. }

最新评论

tz6688 发表于 2022-6-28 17:50
html5网站源码下载

浏览过的版块

一起源码让程序更轻更快

www.171739.xyz

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表