PHP小程序支付功能完整版【基于thinkPHP】
程序员文章站
2022-06-03 12:02:17
本文实例讲述了php小程序支付功能。分享给大家供大家参考,具体如下:
环境: tp3.2 + 小程序 微信支付功能开通
step1: 下载php...
本文实例讲述了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']); } } /** * 微信支付 * @author 大脸猫脸大 * @param $cart * @param $cartamount * @param $address * @param $user */ private function weixinpay($cartamount, $openid, $orderid, $ordersn) { import("vendor.wxpay.lib.wxpay#api", "", ".php"); //订单号 $money = $cartamount * 100; $openid = $openid; $input = new \wxpayunifiedorder(); $input->setbody("迪克-商品"); $input->setout_trade_no("$ordersn"); $input->settotal_fee("$money"); $input->setnotify_url("https://" . $_server['http_host'] . "/api/buy/paynotify"); $input->settrade_type("jsapi"); $input->setopenid($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; } else { 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×tamp=%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'")->setfield($data); exit('<xml><return_code><![cdata[success]]></return_code><return_msg><![cdata[ok]]></return_msg></xml>'); }else { 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程序设计有所帮助。
上一篇: 她说我不爱她。