本文实例为大家分享了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[&#39;amount&#39;] <$cartamount) {
$result[&#39;msg&#39;] &#61; &#39;余额不足&#39;;
$result[&#39;code&#39;] &#61; 0;
$this->ajaxreturn($result);
}
}
// 生成订单
$order[&#39;ordersn&#39;] &#61; $this->genordersn($user[&#39;id&#39;]);
$order[&#39;price&#39;] &#61; $cartamount;
$order[&#39;addressid&#39;] &#61; $address[&#39;id&#39;];
$order[&#39;addressinfo&#39;] &#61; serialize($address); //json_encode($address);
$order[&#39;longitude&#39;] &#61; $address[&#39;longitude&#39;];
$order[&#39;latitude&#39;] &#61; $address[&#39;latitude&#39;];
$order[&#39;addtime&#39;] &#61; time();
$order[&#39;status&#39;] &#61; 0;
$order[&#39;userid&#39;] &#61; $user[&#39;id&#39;];
$order[&#39;paytype&#39;] &#61; $payment;
$order[&#39;paysn&#39;] &#61; &#39;&#39;;
$order[&#39;paytime&#39;] &#61; time();
$orderid &#61; m("order")->add($order);
if ($orderid &#61;&#61; 0) {
$result[&#39;msg&#39;] &#61; &#39;创建订单失败&#39;;
$result[&#39;code&#39;] &#61; 0;
$this->ajaxreturn($result);
}
foreach ($cart as $goods) {
$ordergoods[&#39;orderid&#39;] &#61; $orderid;
$ordergoods[&#39;goodsid&#39;] &#61; $goods[&#39;id&#39;];
$ordergoods[&#39;title&#39;] &#61; $goods[&#39;title&#39;];
$ordergoods[&#39;price&#39;] &#61; $goods[&#39;price&#39;];
$ordergoods[&#39;attr&#39;] &#61; $goods[&#39;attr&#39;];
$ordergoods[&#39;pic&#39;] &#61; $goods[&#39;pic&#39;];
$ordergoods[&#39;num&#39;] &#61; $goods[&#39;selectcount&#39;];
m("ordergoods")->add($ordergoods);
}
if ($payment &#61;&#61; &#39;balance&#39;) {
// 余额支付
$this->balancepay($cartamount, $user[&#39;wxopenid&#39;], $orderid);
} else if ($payment &#61;&#61; &#39;weixin&#39;) {
// 微信支付
$this->weixinpay($cartamount, $user[&#39;wxopenid&#39;], $orderid, $order[&#39;ordersn&#39;]);
}
}
/**
* 微信支付
* &#64;author 大脸猫脸大
* &#64;param $cart
* &#64;param $cartamount
* &#64;param $address
* &#64;param $user
*/
private function weixinpay($cartamount, $openid, $orderid, $ordersn)
{
import("vendor.wxpay.lib.wxpay#api", "", ".php");
//订单号
$money &#61; $cartamount * 100;
$openid &#61; $openid;
$input &#61; new \wxpayunifiedorder();
$input->setbody("迪克-商品");
$input->setout_trade_no("$ordersn");
$input->settotal_fee("$money");
$input->setnotify_url("https://" . $_server[&#39;http_host&#39;] . "/api/buy/paynotify");
$input->settrade_type("jsapi");
$input->setopenid($openid);
$unifiedorder &#61; \wxpayapi::unifiedorder($input);
if ($unifiedorder[&#39;result_code&#39;] &#61;&#61; &#39;success&#39; && $unifiedorder[&#39;return_code&#39;] &#61;&#61; &#39;success&#39;) {
$time &#61; time();
$data[&#39;timestamp&#39;] &#61; "$time"; //时间戳
$data[&#39;noncestr&#39;] &#61; $unifiedorder[&#39;nonce_str&#39;]; //随机字符串
$data[&#39;signtype&#39;] &#61; &#39;md5&#39;; //签名算法&#xff0c;暂支持 md5
$data[&#39;package&#39;] &#61; &#39;prepay_id&#61;&#39; . $unifiedorder[&#39;prepay_id&#39;]; //统一下单接口返回的 prepay_id 参数值&#xff0c;提交格式如&#xff1a;prepay_id&#61;*
$data[&#39;paysign&#39;] &#61; $this->genpaysign($unifiedorder, $time);// 之前以为是$unifiedorder[&#39;sign&#39;]; 后来发现是调用的这种方法. 签名方案参见微信公众号支付帮助文档;
$data[&#39;out_trade_no&#39;] &#61; $ordersn;
$data[&#39;orderid&#39;] &#61; $orderid;
$return[&#39;code&#39;] &#61; 1;
$return[&#39;data&#39;] &#61; $data;
} else {
log::write(var_export($unifiedorder, true), log::err, &#39;&#39;, c(&#39;log_path&#39;)."wx_pay_".date(&#39;y_m_d&#39;).&#39;.log&#39;);
$return[&#39;code&#39;] &#61; 0;
$return[&#39;msg&#39;] &#61; &#39;微信支付失败&#39;;// $unifiedorder[&#39;return_msg&#39;];
}
$this->ajaxreturn($return);
}
/* 生成支付签名*/
private function genpaysign($unifiedorder, $time)
{
$appid &#61; \wxpayconfig::appid;
$noncestr &#61; $unifiedorder[&#39;nonce_str&#39;];
$package &#61; &#39;prepay_id&#61;&#39; . $unifiedorder[&#39;prepay_id&#39;];
$signtype &#61; "md5";
$timestamp &#61; $time;
$key &#61; \wxpayconfig::key;
$sign &#61; md5(sprintf("appid&#61;%s&noncestr&#61;%s&package&#61;%s&signtype&#61;%s×tamp&#61;%s&key&#61;%s", $appid, $noncestr, $package, $signtype, $timestamp, $key));
}
/**
* 支付回调
* &#64;author:大脸猫脸大
*/
public function paynotify()
{
import("vendor.wxpay.lib.wxpay#data", "", ".php");
$xml &#61; $globals[&#39;http_raw_post_data&#39;];
$val &#61; \wxpayresults::init($xml);
if ($val[&#39;result_code&#39;] &#61;&#61; &#39;success&#39; && $val[&#39;return_code&#39;] &#61;&#61; &#39;success&#39;) {
$ordersn &#61; $val[&#39;out_trade_no&#39;];
$transactionid &#61; $val[&#39;transaction_id&#39;];
$data &#61; array(&#39;paytype&#39; &#61;> &#39;weixin&#39;, &#39;status&#39; &#61;> &#39;1&#39;, &#39;paytime&#39; &#61;> time(), &#39;paysn&#39; &#61;> $transactionid);
m("order")->where("ordersn&#61;&#39;$ordersn&#39;")->setfield($data);
exit(&#39;&#39;);
}else {
log::write(var_export($val, true), log::err, &#39;&#39;, c(&#39;log_path&#39;)."wx_pay_notify_".date(&#39;y_m_d&#39;).&#39;.log&#39;);
exit(&#39;&#39;);
}
}
paynotify 回调方法里一定要注意返回
如果不处理&#xff0c;你会发现paynotify 会被执行很多次参见&#xff1a;官方文档
总结一下&#xff1a;
注意二点&#xff0c;1.签名的问题 2. 回调方法的返回处理。
欢迎大家指正。
以上就是本文的全部内容&#xff0c;希望对大家的学习有所帮助&#xff0c;也希望大家多多支持萬仟网。
希望与广大网友互动&#xff1f;&#xff1f;
点此进行留言吧&#xff01;