tp5 快速接入扫码支付
前提是申请好微信支付,同时配置好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 快速接入扫码支付的更多相关文章
- ASP.NET Core Web 支付功能接入 微信-扫码支付篇
这篇文章将介绍ASP.NET Core中使用 开源项目 Payment,实现接入微信-扫码支付及异步通知功能. 开发环境:Win 10 x64.VS2017 15.6.4..NET Core SDK ...
- 【转载】ASP.NET Core Web 支付功能接入 微信-扫码支付篇
转自:http://www.cnblogs.com/essenroc/p/8630730.html 这篇文章将介绍ASP.NET Core中使用 开源项目 Payment,实现接入微信-扫码支付及异步 ...
- 【移动支付】.NET微信扫码支付接入(模式二-NATIVE)
一.前言 经过两三天的琢磨总算完成了微信扫码支付功能,不得不感叹几句: 微信提供的DEMO不错,直接复制粘贴就可以跑起来了: 微信的配置平台我真是服了.公众平台.商户平台.开放平台,一个平 ...
- ASP.NET Core Web 支付功能接入 微信-扫码支付篇(转)
原文 https://www.cnblogs.com/essenroc/p/8630730.html // 随着版本更迭,新版本可能无法完全适用,请参考仓库内的示例. 这篇文章将介绍ASP.NET C ...
- 微信扫码支付PHP接入总结
微信扫码支付分为两种模式, 模式一比较复杂,需要公众号配置回调地址. 模式二比较简单,只需要在代码中配置回调地址就可以了. 我这次使用的是模式二. 需要配置参数, const APPID = 'xxx ...
- ASP.NET Core 2.0 支付宝当面付之扫码支付
前言 自从微软更换了CEO以后,微软的战略方向有了相当大的变化,不再是那么封闭,开源了许多东西,拥抱开源社区,.NET实现跨平台,收购xamarin并免费提供给开发者等等.我本人是很喜欢.net的,并 ...
- 快速接入PHP微信支付
微信支付是微信开发中坑最多的一个功能,本文旨在帮助有开发基础的人快速接入微信支付,如果要详细了解微信支付,请看微信支付的开发文档. 再说把开发文档搬到这里来就没必要了.想要快速跑通微信支付的可以继续查 ...
- 170327、Java微信支付中的扫码支付
微信支付现在已经变得越来越流行了,随之也出现了很多以可以快速接入微信支付为噱头的产品,不过方便之余也使得我们做东西慢慢依赖第三方,丧失了独立思考的能力,这次打算分享下我之前开发过的微信支付. 一 H5 ...
- 微信支付之扫码支付开发:我遇到的坑及解决办法(附:Ecshop 微信支付插件)
前段时间帮一个朋友的基于ecshop开发的商城加入微信扫描支付功能,本以为是很简单的事儿——下载官方sdk或开发帮助文档,按着里面的做就ok了,谁知折腾了两三天的时间才算搞定,中间也带着疑问在网上找了 ...
随机推荐
- MVVM架构说明1
MVVM是Model-View-ViewModel的简写.微软的WPF带来了新的技术体验,如Sliverlight.音频.视频.3D.动画……,这导致了软件UI层更加细节化.可定制化.同时,在技术层面 ...
- idea maven打包 install 报错The packaging for this project did not assign a file to the build artifact
如题,这其实是个低级错误,这个错的意思是,找不到这个插件的包. 原因很简单,不是找不到这个打包插件,而是自己的项目没有从maven仓库里加载这个包到项目里,因此会找不到. 看一下问什么会报这个错: 大 ...
- 支付宝VIE的罪与罚
http://tech.ifeng.com/special/tusimple/alibaba/#_www_dt2 雅虎的杨致远.软银的孙正义,都曾是马云阿里巴巴创业路上的贵人,也都曾是相互信任的朋 ...
- CC攻击工具list
从论文里抠出来的工具列表如下,后面有黑产的工具以及网络上摘录的工具: 分类:(1)有僵尸网络(是否代理服务器)&没有的==>(2)单一url&混合url(多线程,压测为主,dem ...
- 面向对象设计原则-SOLID
SOLID的意思是: Single responsibility principle 单一职责原则 Open/close principle 开放/封闭原则 Liskov substitution p ...
- react-redux: counter
store: import {createStore,applyMiddleware, compose} from "redux"; import thunk from " ...
- 专业工具软件AutoCAD复习资料
专业工具软件AutoCAD复习资料 下载地址:http://download.csdn.net/detail/zhangrelay/9849503 这里给出了一些dwg格式的CAD资料,用于课后学习和 ...
- MySql 批量创建、导入实例
1.创建sql(例如,taobao,dangdang): DROP DATABASE IF EXISTS taobao; CREATE DATABASE taobao CHARSET=utf8; US ...
- 你所不知道的 Java 之 HashCode
之所以写HashCode,是因为平时我们总听到它.但你真的了解hashcode吗?它会在哪里使用?它应该怎样写? 相信阅读完本文,能让你看到不一样的hashcode. 使用hashcode的目的在于: ...
- C高级第一次作业附加
之前的作业链接:http://www.cnblogs.com/1204113692yang/p/8625650.html 过去两周学习了指针的概念.指针变量的定义.指针的基本运算.指针操作改变主调函数 ...