


Asynchronous notification and verification order method of calling SDK after payment in WeChat applet
The following is a WeChat applet that uses the SDK’s asynchronous notification and verification method to process orders after payment. It has a good reference value and I hope it will be helpful to everyone
Asynchronous verification and order processing after the applet
<?php /** * Created by YuanPan. * User: YuanPan * Date: 2017/12/21 * Time: 15:15 */ namespace app\api\service; use app\api\model\Product; use app\lib\enum\OrderStatusEnum; use think\Db; use think\Exception; use think\Loader; use app\api\model\Order; //在这里依然引入WxPay.API.php即可完成引入 Loader::import('WxPay.WxPay',EXTEND_PATH,'.Api.php'); class WxNotify extends \WxPayNotify { //继承了父类,所以直接重写方法 public function NotifyProcess($data, &$msg) { //如果返回支付成功信息 if($data['return_code'] = 'SUCCESS'){ $orderNo = $data['out_trade_no']; Db::startTrans(); try { //判断订单状态 //在这里可以进行对数据库进行所机制 //简单说,当一个执行sql语句的事务想要操作表记录之前,先向数据库发出请求,对你访问的记录集加锁 //在这个事务释放这个锁之前,其他事务不能对这些数据进行更新操作 Order::where(['order_no'=>$orderNo])->lock(true)->find(); // $order = Order::get(['order_no'=>$orderNo]); //如果未支付 if ($order->status == 1) { $service = new \app\api\service\Order(); //查询到该订单号的商品和库存量信息 $stockStatus = $service->checkOrderStock($order->id); //如果库存量有,更新订单状态,以及减去商品库存 if ($stockStatus['pass']) { $this->updateOrderStatus($order->id, true); $this->reduceStock($stockStatus); //如果没有库存,订单状态修改为已支付,但订单未处理 } else { $this->updateOrderStatus($order->id, false); } } Db::commit(); //返回给微信 return true; }catch (Exception $e){ Db::rollback(); return fasle; } }else{ //如果微信返回失败的处理信息,那我们这里依然返回true,向微信表明态度 return true; } } //支付成功,减去商品库存 private function reduceStock($stockStatus) { //遍历减去 foreach ($stockStatus['pStatusArray'] as $v) { //使用tp5的setDec方法 Product::where(['id'=>$v['id']])->setDec('stock',$v['count']); } } //success为真,则订单修改为PAID,如果为假则修改为已支付,但订单未处理的状态,对应枚举信息修改 private function updateOrderStatus($orderID,$success) { $status = $success?OrderStatusEnum::PAID:OrderStatusEnum::PAID_BUT_OUT_OF; Order::where(['id'=>$orderID])->update(['status'=>$status]); } }
The controller is as follows:
public function receiveNotify() { //1:检查库存量防止超卖 //2:更新status状态 //3:减库存 //如果成功处理,返回成功处理信息,否则返回没有成功处理信息 $notify = new WxNotify(); $notify->handle(); }
The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Implementation of shopping cart function in WeChat mini program
Communication between pages in WeChat mini program Method
Introduction to defining global data and function reuse and templates in WeChat mini-program
The above is the detailed content of Asynchronous notification and verification order method of calling SDK after payment in WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
