首页 > 作文

PHP小程序支付功能完整版【基于thinkPHP】

更新时间:2023-04-07 09:43:54 阅读: 评论:0

本文实例讲述了php小程序支付功能。分享给大家供大家参考,具体如下:

环境: tp3.2 + 小程序 微信支付功能开通

step1: 下载php 支付sdk() 放到library\vendor下,取名wxpay

修改wxpay.config.php 里的appid appcret key mchid

step2: 小程序 js 代码:

var url = getapp().globaldata.httprver + 'api/buy/pay';var urid = getapp().globaldata.urid;var totalmoney = this.data.totalmoney;var cart = this.data.goods;var param = {  cart: json.stringify(cart),  cartamount: totalmoney,  urid: urid,  payment: this.data.payment,  addressid: defaultaddress.id};var that = this;util.http(url, param, function (ret) {  if (ret.data.code == 1) {    if (that.data.payment == 'balance') { // 余额支付      that.afterpaysuccess(ret.data.data);    } el {                // 微信支付      wx.requestpayment({        timestamp: ret.data.data.timestamp,        noncestr: ret.data.data.noncestr,        package: ret.data.data.package,    小额投资项目    signtype: ret.data.data.signtype,        paysign: ret.data.data.paysign,        'success': function (res) {          that.afterpaysuccess(ret.data.data.orderid);        },        'fail': function (res) {          console.log(res);        }      })    }  } el {    util.showtip(ret.data.msg, '提交订单失败');  }});/** * 网络请求 */function http(url, params, callback) {  wx.request({    url: url,    data: params,    success: function (res) {      callback(res);    },    fail: function (err) {      console.log(err);    }  });}

step3: 接口代码:

public function pay(){    $cart = i('cart', '', 'trim');    $cartamount = i('cartamount');    $addressid = i('addressid', 0, 'intval');    $payment = i('payment', '', 'trim');    $urid = $this->urid;    $cart = json_decode($cart, true);    if (empty($cart)) {      $result['msg'] = '购物车获取失败';      $result['code'] = 0;      $this->ajaxreturn($result);    }    $totalmoney = 0;    foreach ($cart as $goods) {      $money = $goods['price'];  // price      $lect南瓜藤count = $goods['lectcount'];  // price      $itemamount = number_format($money * $lectcount, 2, '.', '');      $totalmoney += $itemamount;    }    // 检查总金额是否一致    if ($totalmoney != $cartamount) {      $result['msg'] = '总金额不匹配:' . $totalmoney;      $result['code'] = 0;      $this->ajaxreturn($result);    }    // 获取用户地址    $address = m('memberaddress')->where('urid=' . $urid . " and id=" . $addressid)->find();    if (empty($address)) {      $result['msg'] = '用户地址不存在';      $result['code'] = 0;      $this->ajaxreturn(期中反思$result);    }    // 用户信息    $ur = m('member')->where("id=" . $urid)->find();    if ($payment == 'balance') {      if ($ur['amount'] < $cartamount) {        $result['msg'] = '余额不足';        $result['code'] = 0;        $this->ajaxreturn($result);      }    }    // 生成订单    $order['ordersn'] = $this->长沙辣椒炒肉;genordersn($ur['id']);    $order['price'] = $cartamount;    $order['addressid'] = $address['id'];    $order['addressinfo'] = rialize($address); //json_encode($address);    $order['longitude'] = $address['longitude'];    $order['latitude'] = $address['latitude'];    $order['addtime'] = time();    $order['status'] = 0;    $order['urid'] = $ur['id'];    $order['paytype'] = $payment;    $order['paysn'] = '';    $order['paytime'] = time();    $orderid = m("order")->add($order);    if ($orderid == 0) {      $result['msg'] = '创建订单失败';      $result['code'] = 0;      $this->ajaxreturn($result);    }    foreach ($cart as $goods) {      $ordergoods['orderid'] = $orderid;      $ordergoods['goodsid'] = $goods['id'];      $ordergoods['title'] = $goods['title'];      $ordergoods['price'] = $goods['price'];      $ordergoods['attr'] = $goods['attr'];      $ordergoods['pic'] = $goods['pic'];      $ordergoods['num'] = $goods['lectcount'];      m("ordergoods")->add($ordergoods);    }    if ($payment == 'balance') {      // 余额支付      $this->balancepay($cartamount, $ur['wxopenid'], $orderid);    } el if ($payment == 'weixin') {      // 微信支付      $this->weixinpay($cartamount, $ur['wxopenid'], $orderid, $order['ordersn']);    }}/*** 微信支付* @author 大脸猫脸大* @param $cart* @param $cartamount* @param $address* @param $ur*/private function weixinpay($cartamount, $openid, $orderid, $ordersn){    import("vendor.wxpay.lib.wxpay#api", "", ".php");    //订单号    $money = $cartamount * 100;    $openid = $openid;    $input approximately= new \wxpayunifiedorder();    $input->tbody("迪克-商品");    $input->tout_trade_no("$ordersn");    $input->ttotal_fee("$money");    $input->tnotify_url("https://" . $_rver['http_host'] . "/api/buy/paynotify");    $input->ttrade_type("jsapi");    $input->topenid($openid);    $unifiedorder = \wxpayapi::unifiedorder($input);    if ($unifiedorder['result_code'] == 'success' && $unifiedorder['return_code'] == 'success') {      $time = time();      $data['timestamp'] = "$time";              //时间戳      $data['noncestr'] = $unifiedorder['nonce_str'];     //随机字符串      $data['signtype'] = 'md5';                //签名算法,暂支持 md5      $data['package'] = 'prepay_id=' . $unifiedorder['prepay_id'];  //统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*      $data['paysign'] = $this->genpaysign($unifiedorder, $time);// 之前以为是$unifiedorder['sign']; 后来发现是调用的这种方法. 签名方案参见微信公众号支付帮助文档;      $data['out_trade_no'] = $ordersn;      $data['orderid'] = $orderid;      $return['code'] = 1;      $return['data'] = $data;    } el {        log::write(var_export($unifiedorder, true), log::err, '', c('log_path')."wx_pay_".date('y_m_d').'.log');        $return['code'] = 0;        $return['msg'] = '微信支付失败';// $unifiedorder['return_msg'];    }    $this->ajaxreturn($return);}/* 生成支付签名*/private function genpaysign($unifiedorder, $time){    $appid = \wxpayconfig::appid;    $noncestr = $unifiedorder['nonce_str'];    $package = 'prepay_id=' . $unifiedorder['prepay_id'];    $signtype = "md5";    $timestamp = $time;    $key = \wxpayconfig::key;    $sign = md5(sprintf("appid=%s&noncestr=%s&package=%s&signtype=%s&timestamp=%s&key=%s", $appid, $noncestr, $package, $signtype, $timestamp, $key));    return $sign;}/*** 支付回调* @author:大脸猫脸大*/public function paynotify(){    import("vendor.wxpay.lib.wxpay#data", "", ".php");    $xml = $globals['http_raw_post_data'];    $val = \wxpayresults::init($xml);    if ($val['result_code'] == 'success' && $val['return_code'] == 'success') {      $ordersn = $val['out_trade_no'];      $transactionid = $val['transaction_id'];      $data = array('paytype' => 'weixin', 'status' => '1', 'paytime' => time(), 'paysn' => $transactionid);      m("order")->where("ordersn='$ordersn'")->tfield($data);      exit('<xml><return_code><![cdata[success]]></return_code><return_msg><![cdata[ok]]></return_msg></xml>');    }el {      log::write(var_export($val, true), log::err, '', c('log_path')."wx_pay_notify_".date('y_m_d').'.log');      exit('<xml><return_code><![cdata[fail]]></return_code><return_msg><![cdata[ok]]></return_msg></xml>');    }}

paynotify 回调方法里一定要注意返回

<xml><return_code><![cdata[success]]></return_code><return_msg><![cdata[ok]]></return_msg></xml>

如果不处理,你会发现paynotify 会被执行很多次参见:官方文档

总结一下: 注意二点, 1.签名的问题 2. 回调方法的返回处理。

欢迎大家指正。

更多关于php相关内容感兴趣的读者可查看本站专题:《php微信开发技巧汇总》、《php curl用法总结》、《php网络编程技巧总结》、《php字符串(string)用法总结》、《php中json格式数据操作技巧汇总》及《php针对xml文件操作技巧总结》

希望本文所述对大家php程序设计有所帮助。

本文发布于:2023-04-07 09:43:52,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/76496ede2c03322a68269da448755155.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PHP小程序支付功能完整版【基于thinkPHP】.doc

本文 PDF 下载地址:PHP小程序支付功能完整版【基于thinkPHP】.pdf

标签:余额   回调   订单   程序
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图