This is the sixth day of my participation in the August Text Challenge.More challenges in August

Front listing

  • Official id: APPID
  • Merchant Number: MCHID
  • Security operation certificate: APiclient_cert
  • API key: apiclient_key
  • APIv3 key: the key set on the merchant platform
  • Domain name: HTTPS protocol (443)

Wechat platform configuration

Merchant platform

  • Payment catalog Authorization (authorized access catalog) : Merchant Platform > Product Center > Development Configuration > Payment Configuration > JSAPI Payment > Add Catalog

Public account Platform

  • Web Authorized domain name (obtain user OpenID) : Public account Settings > Function Settings > Web Authorized domain name > Settings
  • Authorization Callback page domain name (limited callback page) : Settings and Development > Development > Interface Authorization > Web Services > Web Authorization > Modify > Web Authorization Domain Name > Settings

Related configuration details can be set to view the website: pay.weixin.qq.com/wiki/doc/ap…

Into the development

Business flow chart

The front end

  1. Basic steps for pulling user information

    • Obtain the code through user authorization
      • Direct followers to open links: https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOP E&state=STATE#wechat_redire

      • APPID: unique identifier of the public account

      • REDIRECT_URI: indicates the address of the callback link that is redirected after authorization. Use urlEncode to process the link

      • SCOPE:

        • Snsapi_base: silent authorization. Only user Openids can be obtained
        • Snsapi_userino: popup authorization to obtain basic user information
      • STATE: The redirection will carry the STATE parameter. The developer can fill in the parameter value of A-ZA-Z0-9, which is 128 bytes at most

    • Exchange web page authorization access_token with code
      • Requested address: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
    • Pull user information
      • Request address: https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

  2. The JSAPI order

    • Invokes the back-end interface to generate a prepaid order, and getsprepay_id(Pre-paid transaction session id. For use in subsequent interface calls, valid for 2 hours).
    • Sample request
    { 
        "mchid": "1900006XXX"."out_trade_no": "1217752501201407033233368318"."appid": "wxdace645e0bc2cXXX"."description": "Image Shop - Shenzhen Tengda -QQ dolls"."notify_url": "https://weixin.qq.com/"."amount": { "total": 1."currency": "CNY" },
        "payer": { "openid": "o4GgauInH_RCEdvrrNGrntXDuXXX"}}Copy the code
  3. JSAPI invokes payment

    • Success withprepay_idAfter that, the cashier desk of wechat Pay needs to be activated again through JSAPI (this operation uses the built-in object of wechat browser, which can only be triggered by wechat browser).

The back-end

  1. Add the dependent

    • gradle
    implementation 'com. Making. Wechatpay - apiv3: wechatpay -- apache httpclient: 0.2.2'
    Copy the code
    • maven
    <dependency> <groupId>com.github.wechatpay-apiv3</groupId> <artifactId>wechatpay-apache-httpclient</artifactId> The < version > 0.2.2 < / version > < / dependency >Copy the code
  2. Initialize the httpClient

// load merchant privateKey] (privateKey: privateKey string)
PrivateKey merchantPrivateKey = PemUtil .loadPrivateKey(
new ByteArrayInputStream(privateKey.getBytes("utf-8"))); 

// Load platform certificate (McHId: merchant number, McHSerialNo: merchant certificate serial number,apiV3Key: V3 key)
AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials(mchId, 
new PrivateKeySigner(mchSerialNo, merchantPrivateKey)),apiV3Key.getBytes("utf-8")); 

// Initialize the httpClient
httpClient = WechatPayHttpClientBuilder.create()
.withMerchant(mchId, mchSerialNo, merchantPrivateKey)
.withValidator(new WechatPay2Validator(verifier)).build();
Copy the code
  1. Receive order request and generate advance order

    • Requested address:https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi
    • Related parameters and error codes:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml
  2. Receive notification of payment result

    • Requested address:https://api.mch.weixin.qq.com/v3/pay/transactions/id/{transaction_id}
    • Related parameters and error codes:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_2.shtml
  3. Close the order

    • Requested address:https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{out_trade_no}/close
    • Related parameters and error codes:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_3.shtml

summary

In the front end, the user needs to be redirected through the link provided by wechat, get the code, then get the access_koken through the code, and finally get the user OpenID and prepay_id. At this point, wechpay can be awakened through the built-in object of wechat browser, so as to send a payment request.

The back end is going to provide

  • Get the prepaid receipt for the front endprepay_idThe interface of the
  • Payment received successfully (notify_url) notification interface
  • Query order status
  • Close the order

Other payment methods such as such as sweep yards way is similar to the JSAPI and instructions to see related parameters to modify pay.weixin.qq.com/wiki/doc/ap…