前提是申请好微信支付,同时配置好key,以及支付回调地址

1.composer

composer require yansongda/pay

2.引入

use Yansongda\Pay\Pay; // 引入支付

3.获取支付二维码字符串

/**
* @param $order_num
* @param $price
* @param string $pinfo
* @return object
*/
public function _get_wx_pay_code($order_num,$price,$pinfo = '')
{
$order_code_url = Cache::store('redis')->get($order_num);
if ($order_code_url){
return $order_code_url;
}
$config = [
'wechat' => [
'app_id' => config('wechat.app_id'),
'mch_id' => config('wechat.mch_id'),
'notify_url' => config('wechat.notice_uri'),
'key' => config('wechat.key'),
'cert_client' => './apiclient_cert.pem',
'cert_key' => './apiclient_key.pem',
],
]; $config_biz = [
'out_trade_no' => $order_num, // 订单号
'total_fee' => $price, // 订单金额,**单位:分**
'body' => $pinfo, // 订单描述
'spbill_create_ip' => Func::get_ip(), // 调用 API 服务器的 IP
'product_id' => 'pid', // 订单商品 ID
]; $pay = new Pay($config);
$code_url = $pay->driver('wechat')->gateway('scan')->pay($config_biz); // 扫码支付,返回支付码
Cache::store('redis')->set($order_num,$code_url,3600);
return $code_url;
}

4.回调出,进行订单处理

// 微信支付回调
public function index()
{
Clog::setLog('pay_success','wx_pay');
$str = $GLOBALS['HTTP_RAW_POST_DATA']; $arr = array();
$xmlTag = array(
'appid','bank_type','cash_fee','fee_type','is_subscribe','mch_id',
'nonce_str','openid','out_trade_no','result_code','return_code','sign',
'time_end','total_fee','trade_type','transaction_id'
);
foreach($xmlTag as $x){
preg_match_all("/<".$x.">.*<\/".$x.">/",$str,$temp);
$arr[$x] = $temp[0][0];
}
//去除XML标签并组装数据
$data = array();
foreach($arr as $key => &$value) {
if ($key == 'total_fee'){
$temp_a = explode('<'.$key.'>', $value);
$last_str = "</".$key.">";
$str_len = strlen($last_str);// 该字符串长度;?????????
$v = substr($temp_a[1],0,-$str_len);
$value = $v;
}else{
$temp_a = explode('<'.$key.'>'.'<![CDATA[', $value);
$last_str = "]]</".$key.">";
$str_len = strlen($last_str);// 该字符串长度;
$v = substr($temp_a[1],0,-$str_len-1);
$value = $v;
}
} Clog::setLog(var_export($arr, true),'wx_pay_arr'); $param = $arr; Clog::setLog($param);
Clog::setLog($param['appid']);
Clog::setLog($param['out_trade_no']);
Clog::setLog($param['return_code']); if(!$param['out_trade_no']){
echo "FAIL";
Clog::setLog('111111');
Clog::setLog($param['out_trade_no'].' 10001','wx_pay');
return;
} else {
Clog::setLog('22222');
if ($param['return_code'] == 'SUCCESS') { // 支付成功
$order_num = $param['out_trade_no'];
$transaction_id = $param['transaction_id']; Clog::setLog('33333');
// 处理订单状态
$order_info = Db::name('order')
->where('order_num',$order_num)
->find(); if ($order_info['status'] != self::ORDER_CREATE) {
Clog::setLog($param['out_trade_no'].' Has Payed','wx_pay');
return;
} $order_data['pay_time'] = time();
$order_data['status'] = self::ORDER_PAY;
$order_data['transaction_id'] = $transaction_id; Db::startTrans();
$error = 0; $r = Db::name('order')
->where('order_num',$order_num)
->data($order_data)
->update(); if (!$r && $r !==0) {
$error ++;
} // 处理优惠券
if ($order_info['user_coupon_id'] > 0) {
Clog::setLog('44444');
$coupon_data['is_use'] = 1;
$coupon_data['update_time'] = time();
$r = Db::name('user_coupon')
->where('id',$order_info['user_coupon_id'])
->data($order_data)
->update();
if (!$r && $r !==0) {
$error ++;
} $coupon_id = Db::name('user_coupon')
->where('id',$order_info['user_coupon_id'])
->value('coupon_id'); $r = Db::name('coupon')
->where('id',$coupon_id)
->inc('used_number',1)
->update(); if (!$r && $r !==0) {
$error ++;
} } if ($order_info['from'] == 1) { // 购物车
// 清空购物车
$r = Db::name('shopping_car')
->where('user_id', $order_info['user_id'])
->delete(); if (!$r && $r !==0) {
$error ++;
}
} if ($error == 0) {
Db::commit();
Clog::setLog($param['out_trade_no'].' 0','wx_pay');
echo "SUCCESS";
return;
} else {
Db::rollback();
Clog::setLog($param['out_trade_no'].' 10099','wx_pay');
return;
} } else {
Clog::setLog($param['out_trade_no'].' 10002','wx_pay');
return;
}
}
}

5.前端写个定时器,查看订单状态是否变更

public function check_pay(){
$order_id = trim($_REQUEST['order_id']);
$is_pay = Db::name('order')->where(['id' => $order_id,'status' => 2])->find();
if ($is_pay){
$this->json->setErr('0',lang('tips_is_pay'));
} else {
$this->json->setErr('10023',lang('tips_not_pay'));
}
$this->json->Send();
}

6.支付成功,进行跳转处理。

tp5 快速接入扫码支付的更多相关文章

  1. ASP.NET Core Web 支付功能接入 微信-扫码支付篇

    这篇文章将介绍ASP.NET Core中使用 开源项目 Payment,实现接入微信-扫码支付及异步通知功能. 开发环境:Win 10 x64.VS2017 15.6.4..NET Core SDK ...

  2. 【转载】ASP.NET Core Web 支付功能接入 微信-扫码支付篇

    转自:http://www.cnblogs.com/essenroc/p/8630730.html 这篇文章将介绍ASP.NET Core中使用 开源项目 Payment,实现接入微信-扫码支付及异步 ...

  3. 【移动支付】.NET微信扫码支付接入(模式二-NATIVE)

    一.前言       经过两三天的琢磨总算完成了微信扫码支付功能,不得不感叹几句: 微信提供的DEMO不错,直接复制粘贴就可以跑起来了: 微信的配置平台我真是服了.公众平台.商户平台.开放平台,一个平 ...

  4. ASP.NET Core Web 支付功能接入 微信-扫码支付篇(转)

    原文 https://www.cnblogs.com/essenroc/p/8630730.html // 随着版本更迭,新版本可能无法完全适用,请参考仓库内的示例. 这篇文章将介绍ASP.NET C ...

  5. 微信扫码支付PHP接入总结

    微信扫码支付分为两种模式, 模式一比较复杂,需要公众号配置回调地址. 模式二比较简单,只需要在代码中配置回调地址就可以了. 我这次使用的是模式二. 需要配置参数, const APPID = 'xxx ...

  6. ASP.NET Core 2.0 支付宝当面付之扫码支付

    前言 自从微软更换了CEO以后,微软的战略方向有了相当大的变化,不再是那么封闭,开源了许多东西,拥抱开源社区,.NET实现跨平台,收购xamarin并免费提供给开发者等等.我本人是很喜欢.net的,并 ...

  7. 快速接入PHP微信支付

    微信支付是微信开发中坑最多的一个功能,本文旨在帮助有开发基础的人快速接入微信支付,如果要详细了解微信支付,请看微信支付的开发文档. 再说把开发文档搬到这里来就没必要了.想要快速跑通微信支付的可以继续查 ...

  8. 170327、Java微信支付中的扫码支付

    微信支付现在已经变得越来越流行了,随之也出现了很多以可以快速接入微信支付为噱头的产品,不过方便之余也使得我们做东西慢慢依赖第三方,丧失了独立思考的能力,这次打算分享下我之前开发过的微信支付. 一 H5 ...

  9. 微信支付之扫码支付开发:我遇到的坑及解决办法(附:Ecshop 微信支付插件)

    前段时间帮一个朋友的基于ecshop开发的商城加入微信扫描支付功能,本以为是很简单的事儿——下载官方sdk或开发帮助文档,按着里面的做就ok了,谁知折腾了两三天的时间才算搞定,中间也带着疑问在网上找了 ...

随机推荐

  1. mysql create database and user 新建数据库并为其创建专用账号

    DROP DATABASE `wordpress`;------------------------------------------------------------------ CREATE ...

  2. ddt读取json文件测试用例的执行顺序

    一. 源码的说明 在源码中,ddt的file_data函数下有这样一段话 意思是说,如果json文件的内容是字典,字典的键名将会作为测试用例名的后缀,字典的值将会作为测试数据,如果这样的话,如果键名字 ...

  3. 五十 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现我的搜索以及热门搜索

    第三百七十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现我的搜索以及热门 我的搜素简单实现原理我们可以用js来实现,首先用js获取到 ...

  4. 【lightoj-1039】A Toy Company(BFS)

    The toy company "Babies Toys" has hired you to help develop educational toys. The current ...

  5. ARM汇编指令集4

    协处理器cp15操作指令: mcr & mrc •mrc用于读取CP15中的寄存器 •mcr用于写入CP15中的寄存器   什么是协处理器? •SoC内部另一处理核心,协助主CPU实现某些功能 ...

  6. Struts11---文件上传

    01.创建对应的上传页面 <body> <form action="user/upload" method="post" enctype=&q ...

  7. 《Java程序员职场全攻略 从小工到专家》 - 书摘精要

    (前言) 学习招式在次,提升内力才是最主要的: (P10) 选择一门编程语言,只是入门的途径.过分依赖编程语言,只会让自己成为代码高手,而不是开发大牛,要知道编程语言只是一种工具,更重要的是编程思想: ...

  8. React 实现 Table 的思考

    琼玖 1 年前 (写的零零散散, 包括github不怎么样) Table 是最常用展示数据的方式之一,可是一个产品中往往很多非常类似的 Table, 但是我们碰到的情况往往是 Table A 要排序, ...

  9. Happening in delphi world

    Happy New Year! Delphi XE5 Update 2 Recent VCL enhancements New product features for old product use ...

  10. 函数及参数http://www.cnblogs.com/Eva-J/p/7125925.html

    文件的修改操作.删除操作,with语句 函数: 函数的定义:def 函数名(形参1,形参2....): 函数的调用:函数名(实参1,实参2) 函数的返回值: 定义阶段:return 三种情况:没有返回 ...