An explanation of the asynchronous notification and verification of the processing order method of the SDK after payment of the WeChat applet

Source: Internet
Author: User
The following small series for you to share a small program to call the SDK after the asynchronous notification and verification process order method, has a good reference value, I hope to help you

Asynchronous validation and order processing after a small program

<?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\Or Derstatusenum;use think\db;use think\exception;use think\loader;use app\api\model\order;// The introduction of WxPay.API.php is still introduced here to complete the introduction of Loader::import (' Wxpay.wxpay ', Extend_path, '. Api.php '); class Wxnotify extends \wxpaynotify{//inherits the parent class, so direct override method public function notifyprocess ($data, & $msg) {//      If the payment success information is returned if ($data [' return_code '] = ' SUCCESS ') {$orderNo = $data [' Out_trade_no '];      Db::starttrans (); try {//To determine the order status//Here you can make a mechanism for the database//simply say, when a transaction that executes the SQL statement wants to manipulate the table record, first make a request to the database that you access the recordset locking//In this Before the transaction releases this lock, other transactions cannot perform an update operation on the data order::where ([' order_no ' = = $orderNo])->lock (true)->find ();//$order = Or        Der::get ([' order_no ' = $orderNo]);          If not paid if ($order->status = = 1) {$service = new \app\api\service\order (); Check the product and stock information to the order number $stockStatus = $service->checkorderstock ($order->id);             If stock is available, update order status, and subtract commodity stock if ($stockStatus [' Pass ']) {$this->updateorderstatus ($order->id, true);            $this->reducestock ($stockStatus);          If there is no inventory, the order status is modified to paid, but the order is not processed} else {$this->updateorderstatus ($order->id, false);        }} db::commit ();      Returns true to return;        }catch (Exception $e) {db::rollback ();      return fasle;    }}else{//If the failed processing information is returned, then we still return true here, to indicate the attitude return true; }}//payment succeeded, minus commodity stock private function Reducestock ($stockStatus) {//Traverse minus foreach ($stockStatus [' Pstatusarray '] as $    V) {//Use TP5 's Setdec method Product::where ([' id ' = ' + $v [' id ']])->setdec (' Stock ', $v [' count ']); }}//success is true, the order is modified to paid, if False then modified to paid, but the order is not processed, the corresponding enumeration information modifies the private function updateorderstatus ($orderID, $success) {$ Status = $success?    Orderstatusenum::P aid:orderstatusenum::P aid_but_out_of; Order::where ([' ID '+ $orderID])->update ([' status ' = $status]); }}

The controller is as follows:

  Public Function receivenotify ()  {    //1: Check inventory to prevent oversold    //2: Update status Status    //3: Reduce inventory    //If successful processing, return successful processing of information, Otherwise, the message is not processed successfully    $notify = new Wxnotify ();    $notify->handle ();  }

After the payment of this small program to call the SDK asynchronous notification and verification processing order method is small to share all the content of everyone, hope to give you a reference, but also hope that we support PHP Chinese web.

Articles you may be interested in:

PHP uses Echarts to generate data statistics report implementation

PHP based on mobile phone number judge operator

Laravel5.2 using CAPTCHA to generate a verification code for login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.