Implement payment
The payment method to be implemented in the offline environment is to scan the My Code generated by the LINE Pay user with a merchant terminal. The workflow for this method is as follows.
Based on the above workflow structure, this section describes what needs to be implemented by the LINE Pay merchant.
For offline payments where the merchant provides a barcode or QR code for LINE Pay users to scan and pay, merchants do not need to implement anything.
Scan My Code
Scan My Code of the LINE Pay user with the merchant terminal. My Code is displayed in the form of a barcode or QR code, as shown in the right screen.
For testing, you can generate My Code for Sandbox. Access the URL below and use the temporarily generated My Code for testing.
// For Taiwan Merchant:
https://sandbox-web-pay.line.me/web/sandbox/payment/oneTimeKey?countryCode=TW
// For Thailand Merchant:
https://sandbox-web-pay.line.me/web/sandbox/payment/oneTimeKey
You can set the following query parameters when calling the above URL.
Name | Description |
---|---|
countryCode | A country code. Set to use each country's currency. You can enter TW (Taiwan dollar) or TH (Thai baht). The default value is TH . |
paymentMethod | A payment method. You can enter card (credit card), balance (LINE Pay balance), or ipass (LINE Pay account payment for Taiwan only). The default value is balance . |
Once you scan the LINE Pay user's My Code with the merchant terminal, you need to deliver the scanned My Code information and order information to the merchant server. The merchant server then requests payment. When a response is delivered from the merchant server, display the necessary information on the terminal.
The merchant must manually implement the function of the merchant terminal, such as scanning a barcode, and the method of implementing communication between the merchant terminal and the merchant server, which isn't described here.
Request and confirm payment
The merchant server requests a payment to the LINE Pay server using the information received by the terminal. The following is an example of calling the payment request API using the My Code information (oneTimeKey
) and the order information received by the terminal.
For information on obtaining the credentials needed to call the Offline API, see Prepare API credentials. The code example described on this page uses the
requestOfflineAPI()
function defined in the Prepare API credentials code example.
try {
let response = await requestOfflineAPI({
method: "POST",
baseUrl: targetAPIServer,
apiPath: "/v2/payments/oneTimeKeys/pay",
data: {
amount: 100,
currency: "TWD",
orderId: "EXAMPLE_ORDER_20230422_1000002",
productName: "Pen Brown",
oneTimeKey: "123456789012",
extras: {
branchName: "First branch",
branchId: "branch1",
},
},
});
console.log("Response: ", response);
} catch (error) {
console.log(error);
}
You may receive the following response as the payment request result.
{
"returnCode": "0000",
"returnMessage": "success",
"info": {
"transactionId": 2019010112345678910,
"orderId": "test_order_1",
"transactionDate": "2023-05-16T18:01:00Z",
"payInfo": [
{
"method": "BALANCE",
"amount": 100
}
],
"balance": 9900
}
}
Once the payment request is processed, it will be finalized with payment confirmation. If you did not separate the capture, the capture is automatically processed and the payment is settled upon confirmation.
The merchant server usually sets a read timeout of around 20 seconds after requesting payment to the LINE Pay server. If you do not get a response within the read timeout, check the payment status by calling the payment status check API. The following is an example of setting a read timeout of 20 seconds and checking the payment status at certain intervals in case of a timeout.
let intervalId = null;
// Check payment request status
const getPayRequestStatus = async function (orderId) {
try {
if (!orderId) throw new Error("Order ID is required!");
let response = await requestOfflineAPI({
method: "GET",
baseUrl: targetAPIServer,
apiPath: `/v2/payments/orders/${orderId}/check`,
});
console.log("Response: ", response);
switch (response.info.status) {
case "AUTH_READY":
console.log("In progress");
break;
case "COMPLETE":
console.log("Finished");
// Do something
case "CANCEL":
console.log("Cancelled");
// Do something
case "FAIL":
console.log("Failed");
// Do something
// ...
default:
clearInterval(intervalId);
}
} catch (error) {
console.log(error);
}
};
// Payment request
try {
let response = await requestOfflineAPI({
method: "POST",
baseUrl: targetAPIServer,
apiPath: "/v2/payments/oneTimeKeys/pay",
data: {
orderId: "test_order_1",
// ...
},
// Set read timeout
signal: AbortSignal.timeout(20000),
});
console.log("Response: ", response);
} catch (error) {
if (err.name === "TimeoutError") {
// Check payment request status every one second
intervalId = setInterval(getPayRequestStatus("test_order_1"), 1000);
} else {
// Handle other exceptions
}
}
The payment status check API response returns the payment status information in the info.status
field. The status information is as follows.
"AUTH_READY"
: Customer is waiting for LINE Pay authentication"CANCEL"
: Payment canceled by the customer"COMPLETE"
: Payment completed"FAIL"
: Payment failed