首页 > 开发 > PHP > 正文

php实现小程序支付完整版

2024-05-04 22:42:04
字体:
来源:转载
供稿:网友

本文实例为大家分享了php实现小程序支付的具体代码,供大家参考,具体内容如下

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

Step1:  下载PHP 支付SDK(下载地址)  放到Library/Vendor下,取名Wxpay           

修改WxPay.Config.php 里的appid appsecret key MCHID

Step2: 小程序 js 代码:

 var url = getApp().globalData.httpServer + 'api/buy/pay';    var userId = getApp().globalData.userId;    var totalMoney = this.data.totalMoney;    var cart = this.data.goods;    var param = {      cart: JSON.stringify(cart),      cartamount: totalMoney,      userid: userId,      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);        } else {                // 微信支付          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);            }          })                    }       } else {        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');    $userId = $this->userid;     $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      $selectCount = $goods['selectcount'];  // price      $itemAmount = number_format($money * $selectCount, 2, '.', '');      $totalMoney += $itemAmount;    }    // 检查总金额是否一致    if ($totalMoney != $cartAmount) {      $result['msg'] = '总金额不匹配:' . $totalMoney;      $result['code'] = 0;      $this->ajaxReturn($result);    }     // 获取用户地址    $address = M('MemberAddress')->where('userid=' . $userId . " and id=" . $addressId)->find();    if (empty($address)) {      $result['msg'] = '用户地址不存在';      $result['code'] = 0;      $this->ajaxReturn($result);    }     // 用户信息    $user = M('Member')->where("id=" . $userId)->find();    if ($payment == 'balance') {      if ($user['amount'] < $cartAmount) {        $result['msg'] = '余额不足';        $result['code'] = 0;        $this->ajaxReturn($result);      }    }     // 生成订单    $order['ordersn'] = $this->genOrdersn($user['id']);    $order['price'] = $cartAmount;    $order['addressid'] = $address['id'];    $order['addressinfo'] = serialize($address); //json_encode($address);    $order['longitude'] = $address['longitude'];    $order['latitude'] = $address['latitude'];    $order['addtime'] = time();    $order['status'] = 0;    $order['userid'] = $user['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['selectcount'];      M("OrderGoods")->add($orderGoods);    }     if ($payment == 'balance') {      // 余额支付      $this->balancePay($cartAmount, $user['wxopenid'], $orderId);    } else if ($payment == 'weixin') {      // 微信支付      $this->weixinPay($cartAmount, $user['wxopenid'], $orderId, $order['ordersn']);    }  }            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表