Contents
3. Pull Up Invoice List (in Weixin)
4. Pull Up Invoice List (Third-party Apps)
5. Query Invoice Information and Obtain PDF File
# 1. Development Preparation
Before starting development, ensure that you have completed the following preparation:
(1) You have a Weixin Service Account that has passed verification.
(2) You have a printer that can access the Internet.
(3) You have a cloud-based backend that can send commands to the printer.
# 2. Workflow
# 3. Pull Up Invoice List (in Weixin)
# 3.1 API Description
This API is used to pull up the invoice card list in Weixin (see the figure below). After the user selects the invoice to print, the developer obtains identification information for the selected invoice.
The call of this API must follow the JS-SDK call method. Before development, read the JS-SDK Documentation to get familiar with the development terminology and basic API calls.
# 3.2 Request Method
Refer to the sample code and provide the following parameters to pull up the invoice list page.
Parameter | Type | Required | Description |
---|---|---|---|
timestamp | string(32) | Yes | Signature generation time, expressed as a 10-digit Unix timestamp. |
nonceStr | string(32) | Yes | The random string used to generate the signature. See 6.3 for the signature generation method. |
signType | string(32) | Yes | Signature type. See 6.3 for the signature generation method. |
cardSign | string(64) | Yes | The invoice signature, which is the signature generated according to the documentation. For details, see 6.3. |
# 3.3 Response
After the user taps OK or Cancel, a result containing the following fields is returned.
Response Field | Type | Value | Description |
---|---|---|---|
err_msg choose_invoice | string | ok | Invoice selected successfully. |
fail | Invoice selection failed. | ||
cancel | Selection canceled. | ||
choose_invoice_info | object | Object list | The list of invoices selected by the user. |
The choose_invoice_info object structure is as follows:
{ card_id = @"", encrypt_code = @"" app_id=@"" }
Each field is described below:
Field | Type | Description |
---|---|---|
card_id | string | The card_id of the selected invoice card. |
encrypt_code | string | The encryption code of the selected invoice card. The printing service provider can use the card_id and encrypt_code to obtain the information and PDF file of the invoice the user wants to print. |
app_id | string | The appid of the invoice issuer. |
After obtaining the card_id and encrypt_code, the developer can obtain the structured information and PDF file for each invoice by referring to the "Query Invoice Information and Obtain PDF File" section.
# 3.4 Sample Code
wx.config({
beta: true,
debug: false,
appId: "wx00000000000000",
timestamp: 1489030247,
nonceStr: "(9J4YRV[#@",
signature: "f027317f8910000000000000000000",
jsApiList: ['chooseInvoice']
});
wx.ready(function () {
wx.invoke('chooseInvoice', {
'timestamp': 1489030247, // The card signature timestamp.
'nonceStr': "p(6N&7WOAF", // The random string for card signature.
'signType': 'SHA1', //The signature method. Default value: 'SHA1'.
'cardSign': "a72043eed36c74300000000000000000" // The card signature.
}, function(res) {
alert(JSON.stringify(res));
}
});
});
# 4. Pull Up Invoice List (third-party apps)
# 4.1 API Description
Weixin also provides an API that allows third-party apps to pull up the invoice list. Just like the API to pull up the invoice list in Weixin, the developer can obtain invoice identification data after a user selects an invoice and taps OK.
The call of this API must follow the JS-SDK calling method and certain conditions must be met. Before development, make sure the following conditions are met:
You have a verified Weixin Open Platform account.
You have created an app on the Weixin Open Platform and it has passed the verification. Developers of unregistered apps can register their apps on the app registration page.
You have downloaded the Weixin e-invoice reimbursement SDK (the iOS and Android download addresses are provided in the resource download section of the e-invoice documentation).
You have read the basic SDK usage methods in the Weixin Open Platform Resource Center and the SDKs are correctly used in your app.
# 4.2 Request Method
iOS apps
Refer to the OpenSDK1.7.7 file in the code package. During use, call the WXChooseInvoice class. Refer to the JS-SDK call method for information about the required signature.
Android apps
Refer to the JAR file in the code package. During use, call the WXChooseCard class and set the CardType to "INVOICE". Refer to the JS-SDK call method for information about the required signature.
# 4.3 Response
The response is identical to the response to the Pull Up Invoice List (in Weixin) API. See section 3.3.
# 5. Query Invoice Information and Obtain PDF File
# 5.1 API Description
After the reimburser has obtained the identification parameters for the electronic invoices selected by the user as described above, this API can be used to query the structured information of the electronic invoices and obtain the invoice PDF files.
# 5.2 Request Method
Request URL: https://api.weixin.qq.com/card/invoice/reimburse/getinvoiceinfo?access_token={access_token}
Request method: POST
Request parameters:
The request parameters use JSON format. Included parameters are as below:
Parameter | Type | Required | Description |
---|---|---|---|
card_id | string | Yes | The card_id of the invoice card. |
encrypt_code | string | Yes | The encrypted code of the invoice card. encrypt_code and card_id are used together to construct the unique identifier of an invoice card. |
# 5.3 Response
Data format: JSON
Parameter | Type | Required | Description |
---|---|---|---|
errcode | Int | Yes | Error code |
errmsg | String | Yes | Error message |
If the error code is 0, the following information is returned:
Parameter | Type | Required | Description |
---|---|---|---|
card_id | String | Yes | Invoice ID. |
begin_time | Int | Yes | Start time of the invoice validity period |
end_time | Int | Yes | End time of the invoice validity period |
openid | String | Yes | User ID |
type | String | Yes | Invoice type, such as Guangdong VAT invoice. |
payee | String | Yes | Invoice payee |
detail | String | Yes | Invoice details |
user_info | Object | Yes | The primary information on an invoice |
user_info contains the following information:
Parameter | Type | Required | Description |
---|---|---|---|
fee | Int | Yes | Tax-inclusive amount of the invoice (in cents) |
title | String | Yes | The invoice title |
billing_time | Int | Yes | The invoicing time, expressed as a 10-digit timestamp (utc+8). |
billing_no | String | Yes | The invoice number |
billing_code | String | Yes | The invoice code |
info | List | No | The product information structure, explained below. |
fee_without_tax | Int | Yes | Tax-exclusive amount (in cents) |
tax | Int | Yes | The tax amount (in cents) |
pdf_url | String | Yes | The PDF_URL corresponding to the invoice |
trip_pdf_url | String | Yes | URLs of other proofs of purchase, such as itineraries and water bills. |
reimburse_status | String | Yes | The invoice reimbursement status. See Notes 6.2. |
check_code | String | Yes | The verification code (required) |
buyer_number | String | No | The buyer's taxpayer ID (optional) |
buyer_address_and_phone | String | No | The buyer's address and phone number (optional) |
buyer_bank_account | String | No | The buyer's bank and bank account (optional) |
seller_number | String | No | The seller's taxpayer ID (optional) |
seller_address_and_phone | String | No | The seller's address and phone number (optional) |
seller_bank_account | String | No | The seller's bank and bank account (optional) |
remarks | String | No | Remarks. |
cashier | String | No | The recipient, in the lower left corner of the invoice (optional) |
maker | String | No | The invoice issuer, in the lower right corner of the invoice (optional) |
info provides the information of invoice items. The data is expressed as an Object list, and each Object in the list includes the following information:
Parameter | Type | Required | Description |
---|---|---|---|
name | String | Yes | Item (product) name |
num | Int | Yes | Item quantity. |
unit | String | Yes | Item unit. |
price | Int | Yes | Unit price (in cents) |
# 6. Notes
# 6.1 Error Codes
The error codes used in the Weixin e-invoice reimbursement solution are defined as below:
Error Code | Description |
---|---|
0 | Successful |
72015 | You do not have the permission to perform invoice operations. |
72017 | Inconsistent invoice title |
72023 | The invoice has been locked by another Official Account. |
72024 | Incorrect invoice status |
# 6.2 Invoice Statuses
The invoice status codes used when obtaining invoice information via the API are defined as below:
Status | Description |
---|---|
INVOICE_REIMBURSE_INIT | Initial invoice status. The invoice is not locked and can be submitted for reimbursement. |
INVOICE_REIMBURSE_LOCK | Invoice locked. The invoice cannot be submitted for reimbursement for multiple times. |
INVOICE_REIMBURSE_CLOSURE | Invoice written off. The invoice has been removed from the user's Cards & Offers. |
# 6.3 Obtain api_ticket
<span id = "21>
# 6.3.1 Request Method
Request URL: https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=wx_card
Request method: GET
# 6.3.2 Response
Response format: JSON
Parameter | Type | Required | Description |
---|---|---|---|
errcode | Int | Yes | Error code |
errmsg | String | Yes | Error message |
ticket | String | Yes | Temporary ticket used for invoice signature, i.e. the Api_ticket that will be used in subsequent invoice signatures. |
expires_in | Int | Yes | Validity period, in seconds. Within the validity period, the tickets of repeated requests are not refreshed. |
# 6.3.3 Sample Code
Response:
{
"errcode":0,
"errmsg":"ok",
"ticket":"bxLdikRXVbTPdHSM05e5u5sUoXNKdvsdshFKA",
"expires_in":7200
}
# 6.4 Invoice Signature Method
When using the JS-SDK and App SDK, signatures are required to ensure that data is not tampered with during transmission. The signature implementation method is as follows:
Parameters used in the signature
Parameter | Description |
---|---|
cardType | Enter "INVOICE". |
timestamp | The timestamp used when pulling up the invoice list (utc+8). |
appid | The appid that calls the API. |
nonceStr | Random string |
api_ticket | The temporary ticket obtained with the acess_token. For details, see 6.3. |
Signature method
Sort the values of api_ticket, appid, timestamp, nonceStr, and cardType in lexicographical order. Then, concatenate all the parameter strings and perform SHA-1 encryption to obtain cardSign.
For example, if api_ticket=aaa, appid=aab, timestamp=abc, nonceStr=bbc, and cardType=cde, the concatenated string is aaaaababcbbccde. Then, you perform SHA-1 encryption on this string to obtain cardSign.