Auto debit payment

With this solution, you can learn how to implement auto debit payment in your mini program.

Auto debit payment is a payment solution that enables merchants to automatically make fund deductions from a user's account after obtaining the user's authorization. For more information, see Auto Debit Introduction.

Note: This solution is aimed at mini programs that are released to Alipay+ partner apps. If you release your mini program to Alipay China, refer to the Alipay China auto debit payment solution.

Before you start

Before you start, make sure you know who your acquiring partner is:

  • If you choose Alipay merchant service, refer to Alipay Developer Center to get API documentation.
  • If you choose other acquiring partners of Alipay+, contact your local acquiring partner to get API documentation.

Note: This solution is based on Alipay merchant service as the acquiring partner, which is also applicable to other acquiring partners of Alipay+.

Procedures

To implement auto debit payment, there are three steps you can expect:

Step 1: Obtain auto debit payment authorization

The following diagram walks you through how to obtain auto debit payment authorization by calling the my.signContract JSAPI.

image

As shown in the above diagram, the authorization process can further be divided into three steps below:

1.1 Prepare for authorization

  1. A user opens a mini program and invokes the authorization process for auto debit payment [1].
  2. The mini program invokes the my.getSiteInfo JSAPI to get the current app information [1.1]. Note: The current app information will be used in the customerBelongsTo parameter of the consult API.
  3. The merchant's mini program triggers the authorization process [1.2].
  4. The merchant server checks whether the current user authorized auto debit payment, which means to check whether there is an existing accessToken. [1.2.1].
  5. If the user did not authorize, the merchant server invokes the authorization consult API to obtain authURL for the authorization page [1.2.2]. Note: For the authorization consult API of Alipay merchant service, see /v1/authorizations/consult.
  6. Alipay Global redirects the request to Alipay+ and Alipay+ returns authURL [1.2.2.1.1].
  7. After acquiring authURL, the merchant server returns the value to the mini program [1.3].

1.2 Invoke authorization page

  1. With authURL, the mini program calls the my.signContract(authURL) JSAPI and passes authURL as a parameter to this JSAPI [1.4]. To learn more, see my.signContract.
  2. Then the super app displays the authorization page for the user to confirm [1.4.2].
  3. After the user agrees to the authorization, the super app sends the authorization result to the super app server for processing [2.1].
  4. The super app returns authCode to the mini program via a callback function [2.3]. Then the mini program sends authCode to the merchant server [2.3.1].

1.3 Obtain accessToken

  1. With authCode, the merchant server calls the applyToken API to get the accessToken and userId [2.3.1.1]. Note: For the applyToken API of Alipay merchant service, see /v1/authorizations/applyToken.
  2. The mini program obtains the accessToken and userId. And the authorization for auto debit payment is successful [2.3.2].

Step 2: Implement auto debit payment

The following diagram illustrates how to make a payment via the auto debit payment method.

image

The steps in the diagram are described as follows:

  1. The user that authorized auto debit payment initiates a request to make a payment [1].
  2. After creating an order, the mini program works to trigger the payment process and obtains the user's accessToken [1.2.1].
  3. With accessToken, the merchant server invokes the pay API to proceed with the payment process [1.2.2]. Note: For the pay API of Alipay merchant service, see /v1/payments/pay.
  4. Alipay Global performs the deduction to the user's account in the super app [1.2.2.1].
  5. The merchant server returns the payment result to the mini program. And the payment is successful [1.3].

With the above steps, you have now completed the implementation of auto debit payment. The following section introduces optional steps to revoke auto debit payment.

Step 3: (Optional) Revoke authorization

The following diagram illustrates how to cancel the authorization for auto debit payment.

image

The steps in the diagram are described as follows:

  1. The user or the merchant initiates a request to cancel the authorization for auto debit payment [1].
  2. The merchant server invokes the revoke API to send the cancellation request to Alipay Global [1.1.1]. Note: For the revoke API of Alipay merchant service, see /v1/authorizations/revoke.
  3. Alipay Global sends a request to the super app server through Alipay+ to revoke accessToken [1.1.1.1].
  4. The merchant server returns the revoke result to the mini program. And the request to cancel the authorization is successful [1.1.4].

API list

Refer to the following two tables for the APIs that are used in the above procedures:

JSAPI

API name

Description

my.getSiteInfo

Obtains the site information.

my.signContract

Redirects the user to the authorization page.

OpenAPI

API Name

Description

/v1/authorizations/consult

Initiates an authorization consult.

/v1/authorization/applyToken

Obtains the access token.

/v1/payments/pay

Initiates a payment.

/v1/authorizations/revoke

Cancels a user's authorization.

Note: The openAPIs listed above belong to Alipay merchant service. For other acquiring partners of Alipay+, contact your local acquiring partner to get API documentation.