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 AlipayCN (Alipay China). If you release your mini program to Alipay+ partner apps, refer to the Alipay+ auto debit payment solution.

Before you start

  1. 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.
  1. Contact your solution architect for permission to call the my.getSiteInfo and my.signContract JSAPIs.

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 merchant server calls the authorization consult interface [1.1]. Note: For the authorization consult API of Alipay merchant service, see /v1/authorizations/consult. The key parameters that are passed in include:
      • customerBelongsTo: Its value is ALIPAY_CN.
      • authRedirectUrl: This URL is the redirect URL for authorization. Use a merchant H5 (HTML5) page as the redirect URL to catch authCode.
      • scopes: Its value is AGREEMENT_PAY.
      • terminalType: Its value is MINI_APP.
  1. Upon receiving the consult request, Alipay+ caches the merchant's authRedirectUrl for later use.
  2. The authorization consult interface returns authURL [1.1.1.1.1].
  3. The merchant server returns authURL to the merchant's mini program [1.2].

1.2 Invoke authorization page

  1. With authURL, the merchant's mini program invokes the my.signContract(authStr) JSAPI and sets the value of the authStr parameter to authURL [1.3]. To learn more, refer to my.signContract.
  2. Alipay China displays the authorization page to the user and the user confirms to accept the authorization [2].
  3. Alipay China redirects to the middle page of Alipay+ and returns agreement_no [2.1]. Alipay+ middle page caches agreement_no and generates authCode [2.1.1].
  4. Alipay+ middle page concatenates the cached authRedirectUrl with authCode and authState, then redirects to the merchant server [2.1.2].
  5. The merchant H5 page (authRedirectUrl) obtains authCode and transfers it to the merchant server [2.1.3].

1.3 Obtain accessToken

  1. At the backend, with authCode, the merchant server calls the applyToken API to get accessToken and userId [2.3.1.1]. Note: For the applyToken API of Alipay merchant service, see /v1/authorizations/applyToken.
  2. At the frontend, the merchant H5 page redirects the user to the mini program using the following path [2.1.3.1.4.1].
copy
alipays://platformapi/startapp?appId=[appId]&page=[pagePath]&query=[params]

Note: To learn how to use scheme to redirect the user to the mini program, refer to Mini program scheme introduction.

With the above steps, the authorization for auto debit payment is successful.

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 cancel authorization for auto debit payment.

Step 3: Revoke auto debit payment (optional)

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/authorizations/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.

More information

Auto Debit Introduction

Alipay+ auto debit payment solution

Alipay Developer Center

Manage Alipay+ mini programs in Alipay China