Alipay, China's leading third-party online payment solutionAlipay, China's leading third-party online payment solution

Service mocking

What is a mock service

The mock service provides simulations of different payment results to get you familiarized with the possible payment scenarios beforehand. With the simulated scenarios provided in a sandbox environment, you can test failed payment processes with different types of responses and learn to handle exception scenarios. The mock service aims to optimize your exception and error handling abilities and therefore, improve your Alipay integration quality with lower integration cost and better user experience.

Previously, many exception scenarios are difficult or even impossible to be simulated. With the mock service, you can run test cases for all the scenarios and see whether the response is as designed. Besides the default mock rules, you can also customize mock rules for exception scenarios based on your business requirements. By now, the mock service supports the process of payment, cancel, refund, and query.

How to use a mock service

Before using a mock service, you need to have an Alipay merchant account. Go to Alipay portal to sign up.

The following video explains how to use Mock services:

1、Log in your sandbox and go to the mock service part. By now, the mock service supports the following 4 APIs:

  • alipay.acquire.cancel
  • alipay.acquire.overseas.query

Service mocking

2、Configure mock rules by specifying mock API requests and corresponding responses.
Two types of mock rules exist for each API: default rules and custom rules.

  • Default rules apply for all the sandbox test accounts. You don't need to make any configuration to make the default rules work.
  • Custom rules are rules where you can specify conditions and expected responses based on your business requirements.

Service mocking

3、Send an API request that matches the specified mock rules. You will receive a corresponding mock response and then process it. For example, you can send an request with the following mock rule. See the below sample code and a corresponding mock response for details. The default mock rule:

Service mocking


Sample request¤cy=USD&sign=6030aea30c96e01c6e5c709c9f66a7df&agreement_info=%7B%22agreement_no%22%3A%22123%22%7D&trans_currency=USD&biz_product=OVERSEAS_MBARCODE_PAY&

Sample response

<?xml version="1.0" encoding="GBK"?>
        <param name="client_info">{"client_ip":""}</param>
        <param name="quantity">1</param>
        <param name="biz_product">OVERSEAS_MBARCODE_PAY</param>
        <param name="identity_code_type">barcode</param>
        <param name="sign">6030aea30c96e01c6e5c709c9f66a7df</param>
        <param name="memo">nomemo</param>
        <param name="trans_name">xxxzgl</param>
        <param name="trans_amount">9901</param>
        <param name="sendFormat">normal</param>
        <param name="partner_trans_id">8567745904202380</param>
        <param name="password">SJV88po0XvIptqWGM4rxP5EQ</param>
        <param name="format_type">xml</param>
        <param name="trans_currency">USD</param>
        <param name="partner">2088621880270354</param>
        <param name="agreement_info">{"agreement_no":"123"}</param>
        <param name="service"></param>
        <param name="currency">USD</param>
        <param name="sign_type">MD5</param>
        <param name="buyer_identity_code">288888888888888888</param>