Home WeChat Applet Mini Program Development Asynchronous notification and verification order method of calling SDK after payment in WeChat applet

Asynchronous notification and verification order method of calling SDK after payment in WeChat applet

Jun 27, 2018 am 11:58 AM
sdk Asynchronous notification WeChat applet pay verify

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(&#39;WxPay.WxPay&#39;,EXTEND_PATH,&#39;.Api.php&#39;);

class WxNotify extends \WxPayNotify
{
  //继承了父类,所以直接重写方法
  public function NotifyProcess($data, &$msg)
  {
    //如果返回支付成功信息
    if($data[&#39;return_code&#39;] = &#39;SUCCESS&#39;){
      $orderNo = $data[&#39;out_trade_no&#39;];
      Db::startTrans();
      try
      {
        //判断订单状态
        //在这里可以进行对数据库进行所机制
        //简单说,当一个执行sql语句的事务想要操作表记录之前,先向数据库发出请求,对你访问的记录集加锁
        //在这个事务释放这个锁之前,其他事务不能对这些数据进行更新操作
        Order::where([&#39;order_no&#39;=>$orderNo])->lock(true)->find();
//        $order = Order::get([&#39;order_no&#39;=>$orderNo]);
        //如果未支付
        if ($order->status == 1) {
          $service = new \app\api\service\Order();
          //查询到该订单号的商品和库存量信息
          $stockStatus = $service->checkOrderStock($order->id);
          //如果库存量有,更新订单状态,以及减去商品库存
          if ($stockStatus[&#39;pass&#39;]) {
            $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[&#39;pStatusArray&#39;] as $v) {
      //使用tp5的setDec方法
      Product::where([&#39;id&#39;=>$v[&#39;id&#39;]])->setDec(&#39;stock&#39;,$v[&#39;count&#39;]);
    }
  }

  //success为真,则订单修改为PAID,如果为假则修改为已支付,但订单未处理的状态,对应枚举信息修改
  private function updateOrderStatus($orderID,$success)
  {
    $status = $success?OrderStatusEnum::PAID:OrderStatusEnum::PAID_BUT_OUT_OF;
    Order::where([&#39;id&#39;=>$orderID])->update([&#39;status&#39;=>$status]);
  }
}
Copy after login

The controller is as follows:

  public function receiveNotify()
  {
    //1:检查库存量防止超卖
    //2:更新status状态
    //3:减库存
    //如果成功处理,返回成功处理信息,否则返回没有成功处理信息
    $notify = new WxNotify();
    $notify->handle();
  }
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to pay for a taxi ride on Baidu Maps. Introduction to the payment steps for a taxi ride. How to pay for a taxi ride on Baidu Maps. Introduction to the payment steps for a taxi ride. Mar 13, 2024 am 10:04 AM

Baidu Map APP has now become the preferred travel navigation software for many users, so some of the functions here are comprehensive and can be selected and operated for free to solve some of the problems that you may encounter in daily travel. You can all check some of your own travel routes and plan some of your own travel plans. After checking the corresponding routes, you can choose appropriate travel methods according to your own needs. So whether you choose some public transportation, Cycling, walking or taking a taxi can all satisfy your needs. There are corresponding navigation routes that can successfully lead you to a certain place. Then everyone will feel more convenient if they choose to take a taxi. There are many drivers They are all able to take orders online, and taxi-hailing has become super

Xianyu WeChat mini program officially launched Xianyu WeChat mini program officially launched Feb 10, 2024 pm 10:39 PM

Xianyu's official WeChat mini program has quietly been launched. In the mini program, you can post private messages to communicate with buyers/sellers, view personal information and orders, search for items, etc. If you are curious about what the Xianyu WeChat mini program is called, take a look now. What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3. If you want to use it, you must activate WeChat payment before you can purchase it;

How to verify signature in PDF How to verify signature in PDF Feb 18, 2024 pm 05:33 PM

We usually receive PDF files from the government or other agencies, some with digital signatures. After verifying the signature, we see the SignatureValid message and a green check mark. If the signature is not verified, the validity is unknown. Verifying signatures is important, let’s see how to do it in PDF. How to Verify Signatures in PDF Verifying signatures in PDF format makes it more trustworthy and the document more likely to be accepted. You can verify signatures in PDF documents in the following ways. Open the PDF in Adobe Reader Right-click the signature and select Show Signature Properties Click the Show Signer Certificate button Add the signature to the Trusted Certificates list from the Trust tab Click Verify Signature to complete the verification Let

Detailed method to unblock using WeChat friend-assisted verification Detailed method to unblock using WeChat friend-assisted verification Mar 25, 2024 pm 01:26 PM

1. After opening WeChat, click the search icon, enter WeChat team, and click the service below to enter. 2. After entering, click the self-service tool option in the lower left corner. 3. After clicking, in the options above, click the option of unblocking/appealing for auxiliary verification.

WeChat applet implements image upload function WeChat applet implements image upload function Nov 21, 2023 am 09:08 AM

WeChat applet implements picture upload function With the development of mobile Internet, WeChat applet has become an indispensable part of people's lives. WeChat mini programs not only provide a wealth of application scenarios, but also support developer-defined functions, including image upload functions. This article will introduce how to implement the image upload function in the WeChat applet and provide specific code examples. 1. Preparatory work Before starting to write code, we need to download and install the WeChat developer tools and register as a WeChat developer. At the same time, you also need to understand WeChat

New features in PHP 8: Added verification and signing New features in PHP 8: Added verification and signing Mar 27, 2024 am 08:21 AM

PHP8 is the latest version of PHP, bringing more convenience and functionality to programmers. This version has a special focus on security and performance, and one of the noteworthy new features is the addition of verification and signing capabilities. In this article, we'll take a closer look at these new features and their uses. Verification and signing are very important security concepts in computer science. They are often used to ensure that the data transmitted is complete and authentic. Verification and signatures become even more important when dealing with online transactions and sensitive information because if someone is able to tamper with the data, it could potentially

Implement the drop-down menu effect in WeChat applet Implement the drop-down menu effect in WeChat applet Nov 21, 2023 pm 03:03 PM

To implement the drop-down menu effect in WeChat Mini Programs, specific code examples are required. With the popularity of mobile Internet, WeChat Mini Programs have become an important part of Internet development, and more and more people have begun to pay attention to and use WeChat Mini Programs. The development of WeChat mini programs is simpler and faster than traditional APP development, but it also requires mastering certain development skills. In the development of WeChat mini programs, drop-down menus are a common UI component, achieving a better user experience. This article will introduce in detail how to implement the drop-down menu effect in the WeChat applet and provide practical

Implement image filter effects in WeChat mini programs Implement image filter effects in WeChat mini programs Nov 21, 2023 pm 06:22 PM

Implementing picture filter effects in WeChat mini programs With the popularity of social media applications, people are increasingly fond of applying filter effects to photos to enhance the artistic effect and attractiveness of the photos. Picture filter effects can also be implemented in WeChat mini programs, providing users with more interesting and creative photo editing functions. This article will introduce how to implement image filter effects in WeChat mini programs and provide specific code examples. First, we need to use the canvas component in the WeChat applet to load and edit images. The canvas component can be used on the page

See all articles