This paper mainly explains the process of docking alipay sandbox environment and generating payment TWO-DIMENSIONAL code. My level is limited, if there is any improper place, please point out, thank you

I. Enter the development platform of Alipay

1. Search the development of Alipay

2, login



3. Enter the sandbox environment



Sandbox environment Main information

You can just click on it and get a sense of what it’s got, and you can use it later

The sandbox tool contains the Sandbox version of Alipay software, which can be downloaded to your mobile phone. The account number and password are in the sandbox account

Set the public key

Set the application public key



If not, can click pay treasure to explain, have detailed step

4. Download demo

Click on the Developer Center overview



SDK download on the right



Alipay provides a lot of test Demo, take to change can be used directly

Find pay in person



download



2. Obtain the QR code of payment

1. Open the test Demo



2. Modify the configuration file

There is a zfbinfo.properties configuration file in the project, which is some important configuration information in the interconnection

Only the necessary fields are picked up here

# dev = dev
open_api_domain=https://openapi.alipaydev.com/gateway.do
In the sandbox account, the UID of the merchant, which is the ID of the merchant receiving the money
pid = 2088102178032752
# Sandbox app, appID
appid = 2016100100635726

# RSA private key, public key and Alipay public keyPublic_key = PkCS8 version of the private key generated by the Alipay public key software# Other fields are modified according to personal needsCopy the code

3, the Main class

The main method uses main.test_trade_precreate(); Method to generate two-dimensional code

We can change main.test_trade_precreate(); The content in the method is derived from the definition information

1) The path in line 435 can be set as the local path to generate the path for the picture

String filePath = String.format("C:/Users/bai/Desktop/qr-%s.png",response.getOutTradeNo()); GetQRCodeImge (Response.getQrCode (), 256, filePath);Copy the code

2) For the URL of 423 callback interface, Alipay will have a callback respectively after the user scans the code and after the payment is successful. The two callbacks use this interface, which can be judged according to the status field and has field explanation in the JSON message

.setNotifyUrl("Alipay callback URL accessible to external network")Copy the code

3) Other information can be customized according to annotations and personal needs

After modifying the information, directly run main to generate two-dimensional code pictures in the specified path

4. Four JSON packets

Get JSON from qr code:

Json {// Mandatory, order number, up to 64 characters, alphanumeric underscore"out_trade_no": "tradeprecreate15647279763334241735"."seller_id": "", // Collect alipay ID. If it is empty, use it according to the PID in alipay development application configuration"total_amount": "100", // Required, total amount (before discount)"undiscountable_amount": "0", // No discount amount (actual payment amount), optional, default is the total amount - discount amount"subject": "Face to face scanning code consumption in XXX brand XXX store", // Required, order title"body": "Purchase 3 items for a total of 20.00 yuan", // Order description // Product information"goods_detail": [{
		"goods_id": "goods_id001"."goods_name": "XXX rolls"."quantity": 1,
		"price": "10"
	}, {
		"goods_id": "goods_id002", // Product ID"goods_name": Toothbrush "XXX", // Product name"quantity": 2, // quantity"price": "5"/ / unit price}],"operator_id": "test_operator_id"// Merchant operator id, which can be used for sales statistics, optional"store_id": "test_store_id", // Mandatory, merchant store id // custom parameter, callback will be returned, currently only support sys_service_PROVIDer_ID"extend_params": {
		"sys_service_provider_id": "2088100200300400500"}, // Expiration time"timeout_express": "120m"
}Copy the code

Get the JSON of the qr code response:

// Alipay response response {"alipay_trade_precreate_response": {
		"code": "10000", // For other incorrect status codes, you can go to alipay's official website to check"msg": "Success"."out_trade_no": "tradeprecreate15647279763334241735", / / picture"qr_code": "https:\/\/qr.alipay.com\/bax03381xcn7dw89lcih007e"
	},
	"sign": "hFNh5JIlUR9rY42ModDoCj/7CA/05cHUq5fI/XaWGRpVAxR3i8tGjTa7XQpvNZEpGd04D/vI5KF/9QfDV7nH5MGNAR/JQA2fkHrevvbfiBV1O+aKzBIZel4 BRTNDTrTH1k+DxWE7nn2Ei54DDwnuk47l8bKhMM4jA6fFCcu/oimcybvMdQV/HyHqssiIph/0OiorFMrOebIKPieAGF7+XksIa1OrSSRCCsjYxh2c7vIHFLC DU1rlSpL8e8USl0bkMy5XX+qCn967aQVet+ejy4eMSYZ2PFNGY3HlgAESX2PEhfDd8opmED3xWSzSzLJUW2RRisjwkR0Pjz8AXe0fGA=="
}
Copy the code

User scan callback JSON:

// after scanning {"gmt_create": ["The 2019-08-02 14:39:53"], // Order creation time"charset": ["utf-8"]."seller_email": ["[email protected]"], // Shop assistant mailbox, sandbox account information"notify_time": ["The 2019-08-02 14:39:54"], // Notification time"subject": ["Face to face scanning code consumption in XXX brand XXX store"], // title // signature"sign": ["OiXtXYPpQx2siDhBJiKYyEpYYd1NULnt1pKAP+MGJHaya8bIFzHcx3GdQLMmj3MPL+jKimMd2+lMVNCfvxjXvYLLfyHe3TrxECI1vGuS3ROREz0xwl6vQem ZnBVgphGGulAVYsmzVyBq3L0mfGwPgFgAVnU63iK874l1C57hlsGZTetkyEKcBWPgOchNKciHDTLgzuaEQnhn37JYJbAm1SZB5hGc4HxHsKWEvdmfu+dzU7u E0WdBTs76V1xG8lyMeKjcUjah4NAsxbp0HRvli5lWiCEXt30eIJZJAEcgraL/lrpmb0p7AabLixbsT+P/ibF4pEWByXX7lzkYa/+aGA=="]."body": ["Purchase 3 items for a total of 20.00 yuan"], // Order details"buyer_id": ["2088102178090170"], // Buyer ID"version": ["1.0"], / / version"notify_id": ["2019080200222143954090171000498739"], // Notification ID"notify_type": ["trade_status_sync"], // Notification type"out_trade_no": ["tradeprecreate15647279763334241735"], // Order number"total_amount": ["100.00"], // Total price"trade_status": ["WAIT_BUYER_PAY"], // Transaction status"trade_no": ["2019080222001490171000065035"], // Transaction id"auth_app_id": ["2016100100635726"],  //app ID
	"buyer_logon_id": ["kdl***@sandbox.com"], // Buyer login ID"app_id": ["2016100100635726"], / / app - ID"sign_type": ["RSA2"], // Signature type"seller_id": ["2088102178032752"] // Merchant UID, alipay development information}Copy the code

User payment callback JSON:

// After successful payment {"gmt_create": ["The 2019-08-02 14:39:53"]."charset": ["utf-8"]."seller_email": ["[email protected]"]."subject": ["Face to face scanning code consumption in XXX brand XXX store"]."sign": ["UBHEmHSVGHz3e/sPYx0wEe7n2jRKVBFiraQdfwp49ElD3XqWemOtk6xFpj1wlVzElOCov9V/RWJr+Xhw98A9yxybtWboFBOCg3oyUXdftLDhUD3gFkUVoGy B70LvPeIG5qzo84/zcrr/F/JqTuiFXzuG7CYpzg1g/H6K2Be7X+cT+9xsvT4llkZB2mso0pkWTXFoR0kG8OrjuYdaBo0yKIFWIn6JYGxsyrX1jn/hj/v6Vth KIf17u/bSVxZUcZO/asgjqd2v1wweELJsyvSz7pwW0R+bqKRvZ+wLGsV9r3LQC6TQecQIP4adFlfdVAWPJ981einnk+F2+0u38K3WCg=="]."body": ["Purchase 3 items for a total of 20.00 yuan"]."buyer_id": ["2088102178090170"]."notify_id": ["2019080200222144027090171000500200"]."total_amount": ["100.00"]."app_id": ["2016100100635726"]."sign_type": ["RSA2"]."buyer_logon_id": ["kdl***@sandbox.com"]."seller_id": ["2088102178032752"]."version": ["1.0"]."out_trade_no": ["tradeprecreate15647279763334241735"]."trade_no": ["2019080222001490171000065035"]."auth_app_id": ["2016100100635726"], //---------------"notify_time": ["The 2019-08-02 14:40:27"], // Notification time"gmt_payment": ["The 2019-08-02 14:40:26"], // Payment time"trade_status": ["TRADE_SUCCESS"], // Payment status, success"buyer_pay_amount": ["100.00"], // The amount spent by the buyer"invoice_amount": ["100.00"], // Invoice value"fund_bill_list": ["[{\" amount \ ": \" 100.00 \ ", \ "fundChannel \" : \ "ALIPAYACCOUNT \}"]"]."receipt_amount": ["100.00"]."point_amount": ["0.00"}}Copy the code

5. Callback interface

The callback interface must be accessible from the Internet and can be used for Intranet penetration

To build a service that can be accessed from the Internet, I use SpringBoot to build it quickly here, or I can run it directly in Tomcat using Web projects

@RestControllerpublic 
class WebController {    
    @PostMapping("/")    
    public String receive(ZfbRequestBean zfbRequestBean) {
        System.out.println(zfbRequestBean);        
        return "hello"; }}Copy the code

ZfbRequestBean is used to receive the requested data, with all the attributes corresponding to the ABOVE JSON message

The @data annotation is a Lombok plugin for IDEA. It automatically generates getters, setters, and toString methods. If you don’t use them, you can add getters and setters

@Data
public class ZfbRequestBean {
    private String gmt_create;
    private String charset;
    private String seller_email;
    private String subject;
    private String sign;
    private String body;
    private String buyer_id;
    private BigDecimal invoice_amount;
    private String notify_id;
    private String fund_bill_list;
    private String notify_type;
    private String trade_status;
    private BigDecimal receipt_amount;
    private BigDecimal buyer_pay_amount;
    private String app_id;
    private String sign_type;
    private String seller_id;
    private String gmt_payment;
    private String notify_time;
    private String version;
    private String out_trade_no;
    private BigDecimal total_amount;
    private String trade_no;
    private String auth_app_id;
    private String buyer_logon_id;
    private BigDecimal point_amount;
}Copy the code

This interface will be called by Alipay when the user scans the code and makes payment

The fund_bill_list field is a special JSON string, which is not converted here. It can be converted by itself

Third, add

1. Web Demo

There is webApp directory under the test project, Tomcat can be configured by itself, index. HTML can be run, and page version can be connected well

We need to make a few changes

Copy the zfbinfo.properties configuration file into the WEB-INF folder

Change line 31 of the trade_precreate.jsp file to configs.init (“.. /zfbinfo.properties”);

If you need to test other interfaces, the paths need to be changed

Here is a personal solution, if there is a better, feel free to discuss in the comments section

2. Other interfaces

Main method and other WebAPP interface docking, you can try, are similar