学习内容:
https://www.kancloud.cn/tpshop/thinkphp5/305718
来源:http://blog.csdn.net/phper8/article/details/76383415
这节课讲微信扫码支付。
初步简单的理解微信支付对接流程:
流程图:
* 什么是二维码?
打开下面url,将看到一个二维码。
demo5.tp-shop.cn/index.php?m=Home&c=index&a=qr_code&data=http%3A%2F%2Fwww.tp-shop.cn
查看项目中代码,qr_code方法:
public function qr_code(){ vendor('phpqrcode.phpqrcode'); error_reporting(E_ERROR); $url = urldecode($_GET["data"]); \QRcode::png($url);exit; }
附:
二维码解码器 在线识别系统
jiema.wwei.cn
== 下面开始讲 微信支付 ==
1.先申请微信支付商户号。。
2.后台填写,配置。
3.测试一条商品支付。
4.支付完成。
顺着支付订单的页面,看代码:
/application/home/controller/Cart.php
public function cart4(){ $order_id = I('order_id/d'); $order = M('Order')->where("order_id", $order_id)->find(); if($order['pay_status'] == 1){ $order_detail_url = U("Home/User/order_detail",array('id'=>$order_id)); header("Location: $order_detail_url"); exit; } if($order['pay_status'] == 2 && $order['order_prom_type'] == 4){ $pre_sell_info = M('goods_activity')->where(array('act_id'=>$order['order_prom_id']))->find(); $pre_sell_info = array_merge($pre_sell_info,unserialize($pre_sell_info['ext_info'])); if($pre_sell_info['retainage_start'] > time()){ $this->error('还未到支付尾款时间'.date('Y-m-d H:i:s',$pre_sell_info['retainage_start'])); } if($pre_sell_info['retainage_end'] < time()){ $this->error('对不起,该预售商品已过尾款支付时间'.date('Y-m-d H:i:s',$pre_sell_info['retainage_start'])); } } if($order['order_prom_type'] != 4){ $userlogic = new UsersLogic(); $res = $userlogic->abolishOrder($order['user_id'],$order['order_id'],$order['add_time']); if($res['status']==1) $this->error('订单超时未支付已自动取消',U("Home/User/order_detail",array('id'=>$order_id))); } $payment_where = array( 'type'=>'payment', 'status'=>1, 'scene'=>array('in',array(0,2)) ); $orderGoodsPromType = M('order_goods')->where(['order_id'=>$order['order_id']])->getField('prom_type',true); if($order['order_prom_type'] == 4 || in_array(1,$orderGoodsPromType)){ $payment_where['code'] = array('neq','cod'); } $paymentList = M('Plugin')->where($payment_where)->select(); $paymentList = convert_arr_key($paymentList, 'code'); foreach($paymentList as $key => $val) { $val['config_value'] = unserialize($val['config_value']); if($val['config_value']['is_bank'] == 2) { $bankCodeList[$val['code']] = unserialize($val['bank_code']); } } $bank_img = include APP_PATH.'home/bank.php'; $this->assign('paymentList',$paymentList); $this->assign('bank_img',$bank_img); $this->assign('order',$order); $this->assign('bankCodeList',$bankCodeList); $this->assign('pay_date',date('Y-m-d', strtotime("+1 day"))); return $this->fetch(); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
然后进入
/template/pc/cart/cart4.html
<form action="{:U('Home/Payment/getCode')}" method="post" name="cart4_form" id="cart4_form"></form>
再看:Home/Payment/getCode
其中:this−>payment−>getcode(order,$config_value);
这就要看__construct(),可知,指的是,plugins/payment/weixin/weixin.class.php
代码如下(代码中是以alipay举例)
class Payment extends Base { public $payment; public $pay_code; public function __construct() { parent::__construct(); $pay_radio = $_REQUEST['pay_radio']; if(!empty($pay_radio)) { $pay_radio = parse_url_param($pay_radio); $this->pay_code = $pay_radio['pay_code']; } else { $this->pay_code = I('get.pay_code'); unset($_GET['pay_code']); } $xml = $GLOBALS['HTTP_RAW_POST_DATA']; if(empty($this->pay_code)) exit('pay_code 不能为空'); include_once "plugins/payment/{$this->pay_code}/{$this->pay_code}.class.php"; $code = '\\'.$this->pay_code; $this->payment = new $code(); } ...略 public function getCode(){ header("Content-type:text/html;charset=utf-8"); $order_id = I('order_id/d'); session('order_id',$order_id); $payment_arr = M('Plugin')->where("`type` = 'payment'")->getField("code,name"); M('order')->where("order_id",$order_id)->save(array('pay_code'=>$this->pay_code,'pay_name'=>$payment_arr[$this->pay_code])); $order = M('order')->where("order_id", $order_id)->find(); if($order['pay_status'] == 1){ $this->error('此订单,已完成支付!'); } $pay_radio = $_REQUEST['pay_radio']; $config_value = parse_url_param($pay_radio); if($this->pay_code == 'weixin' && $_SESSION['openid'] && strstr($_SERVER['HTTP_USER_AGENT'],'MicroMessenger')){ $code_str = $this->payment->getJSAPI($order,$config_value); exit($code_str); }else{ $code_str = $this->payment->get_code($order,$config_value); } $this->assign('code_str', $code_str); $this->assign('order_id', $order_id); return $this->fetch('payment'); } ...略}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
注意:this−>payment−>getcode(order,$config_value);
是weixin.class.php中的get_code
function get_code($order, $config_value) { $notify_url = SITE_URL.'/index.php/Home/Payment/notifyUrl/pay_code/weixin'; $input = new WxPayUnifiedOrder(); $input->SetBody("TPshop商品"); $input->SetAttach("weixin"); $input->SetOut_trade_no($order['order_sn'].time()); $input->SetTotal_fee($order['order_amount']*100); $input->SetNotify_url($notify_url); $input->SetTrade_type("NATIVE"); $input->SetProduct_id("123456789"); $notify = new NativePay(); $result = $notify->GetPayUrl($input); $url2 = $result["code_url"]; return '<img alt="模式二扫码支付" src="/index.php?m=Home&c=Index&a=qr_code&data='.urlencode($url2).'" style="width:110px;height:110px;"/>'; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
这里就出来了,二维码的图片。主要就是这个get_code。
这里成功后,用户扫码。扫码成功后,返回到$notify_url的地址。
$notify_url = SITE_URL.'/index.php/Home/Payment/notifyUrl/pay_code/weixin';
/application/home/controller/Payment.php 看notifyUrl()
public function notifyUrl(){ $this->payment->response(); exit(); }
再看weixin.class.php中的response()
/** * 服务器点对点响应操作给支付接口方调用 * */ function response() { require_once("example/notify.php"); $notify = new PayNotifyCallBack(); $notify->Handle(false); }
主要是:plugins/payment/weixin/example/notify.php
public function NotifyProcess($data, &$msg) { Log::DEBUG("call back:" . json_encode($data)); $notfiyOutput = array(); if(!array_key_exists("transaction_id", $data)){ $msg = "输入参数不正确"; return false; } if(!$this->Queryorder($data["transaction_id"])){ $msg = "订单查询失败"; return false; } $appid = $data['appid']; $order_sn = $out_trade_no = $data['out_trade_no']; $attach = $data['attach']; if(strlen($order_sn) > 18){ $order_sn = substr($order_sn,0,18); } update_pay_status($order_sn); return true; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
update_pay_status($order_sn); // 修改订单支付状态
这个方法在common.php
function update_pay_status($order_sn,$ext=array()){ if(stripos($order_sn,'recharge') !== false){ $count = M('recharge')->where(['order_sn'=>$order_sn,'pay_status'=>0])->count(); if($count == 0) return false; $order = M('recharge')->where("order_sn", $order_sn)->find(); M('recharge')->where("order_sn",$order_sn)->save(array('pay_status'=>1,'pay_time'=>time())); accountLog($order['user_id'],$order['account'],0,'会员在线充值'); }else{ $count = M('order')->master()->where("order_sn = :order_sn and pay_status = 0 OR pay_status = 2")->bind(['order_sn'=>$order_sn])->count(); if($count == 0) return false; $order = M('order')->master()->where("order_sn",$order_sn)->find(); if ($order['order_prom_type'] == 4) { $orderGoodsArr = M('OrderGoods')->where(array('order_id'=>$order['order_id']))->find(); if($order['total_amount'] != $order['order_amount'] && $order['pay_status'] == 0){ M('order')->where("order_sn", $order_sn)->save(array('order_sn'=> date('YmdHis').mt_rand(1000,9999) ,'pay_status' => 2, 'pay_time' => time(),'paid_money'=>$order['order_amount'])); M('goods_activity')->where(array('act_id'=>$order['order_prom_id']))->setInc('act_count',$orderGoodsArr['goods_num']); }else{ M('order')->where("order_sn", $order_sn)->save(array('pay_status' => 1, 'pay_time' => time())); $pre_sell = M('goods_activity')->where(array('act_id'=>$order['order_prom_id']))->find(); $ext_info = unserialize($pre_sell['ext_info']); if(empty($ext_info['deposit'])){ M('goods_activity')->where(array('act_id'=>$order['order_prom_id']))->setInc('act_count',$orderGoodsArr['goods_num']); } } } else { M('order')->where("order_sn", $order_sn)->save(array('pay_status'=>1,'pay_time'=>time())); } minus_stock($order['order_id']); update_user_level($order['user_id']); if(array_key_exists('admin_id',$ext)){ logOrder($order['order_id'],$ext['note'],'付款成功',$ext['admin_id']); }else{ logOrder($order['order_id'],'订单付款成功','付款成功',$order['user_id']); } M('rebate_log')->where("order_id" ,$order['order_id'])->save(array('status'=>1)); $distribut_condition = tpCache('distribut.condition'); if($distribut_condition == 1) M('users')->where("user_id", $order['user_id'])->save(array('is_distribut'=>1)); $res = checkEnableSendSms("4"); if(!$res || $res['status'] !=1) return ; $sender = tpCache("shop_info.mobile"); if(empty($sender))return; $params = array('order_sn'=>$order_sn); sendSms("4", $sender, $params); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
Tpshop的微信支付就简单介绍到此,
需要我们再多研究,不是一下子就能理解和掌握。
加油!
学习改变命运,技术成就梦想!
坚持不懈的努力,其结果一定是成功!
回调业务处理逻辑: