Alipay Integration

The following guide explains the required steps in order to receive payments using Alipay through the Openpay API.

Flow

 

Flujo para realizar cargos en Alipay

 

Steps:

  1. The customer requests to pay through Alipay
  2. The merchant starts a new Alipay transaction using the Openpay API
  3. The merchant redirects the customer to the URL returned by Openpay
  4. The customer pays using his Alipay account
  5. The transaction is completed in Openpay
  6. The merchant is notified of a completed payment
    1. The customer is redirected back to the merchant website, and the merchant verifies the transaction status
    2. The merchant receives a notification from Openpay as an alternate confirmation method


The merchant must implement steps 1 through 3 to start the transaction, and step 6 to receive the completed payment notification.

Starting a transaction (Steps 1 through 3)

In order to start an Alipay transaction you must specify the transaction method as alipay. Additionaly, you must specify the URL to where the customer will be redirected to once the payment process is completed.

<?php
$openpay = Openpay::getInstance('mzdtln0bmtms6o3kck8f', 
  'sk_e568c42a6c384b7ab02cd47d2e407cab');

$chargeData = array(
    'method' => 'alipay',
    'amount' => 100.00,
    'description' => 'Cargo mediante Alipay',
    'redirect_url' => 'https://www.example.com/myUrl');

$charge = $openpay->charges->create($chargeData);
?>

If the process is successful, Openpay will return a response with an URL to which your customer must be redirected to in order to complete their payment through Alipay.

Response:

 

{
  "id": "trxxqosgmdsvt3hocoun",
  "authorization": null,
  "operation_type": "in",
  "method": "alipay",
  "transaction_type": "charge",
  "status": "charge_pending",
  "conciliated": false,
  "creation_date": "2018-06-20T10:45:20-05:00",
  "operation_date": "2018-06-20T10:45:20-05:00",
  "description": "Cargo mediante Alipay",
  "error_message": null,
  "order_id": null,
  "due_date": "2018-06-21T10:45:20-05:00",
  "payment_method": {
    "type": "redirect",
    "url": "https://sandbox-api.openpay.mx/v1/mzdtln0bmtms6o3kck8f/charges/trxxqosgmdsvt3hocoun/redirect/"
  },
  "amount": 100,
  "currency": "MXN",
  "fee": {
    "amount": 5.4,
    "tax": 0.86,
    "currency": "MXN"
  }
}

 

Alipay payment (Steps 4 and 5)

Once the user arrives in the Alipay website, they can use the Alipay App to scan the given QR Code:

 

Pago Alipay con QR Code

 

Or, alternatively, the customer can login into their Alipay account and complete payment in the browser:

 

Pago Alipay con Login

 

Payment confirmation (Step 6)

Once the transaction has been paid, Openpay will set the transaction status to completed. The customer will be redirected back to the merchant website and a notification will be sent to the merchant's configured webhooks.

Redirect confirmation (6a)

After the customer has completed their payment through Alipay, they will be redirected to the URL that you provided in the charge request, with the addition of a parameter named id with the transaction ID of the completed payment.

Example:

https://www.example.com/myUrl?id=trxxqosgmdsvt3hocoun

The merchant must then use this ID to retrieve the transaction status using the Openpay API, and confirm that the transaction status is completed.

Note: If the transaction status is not yet completed, the transaction is still pending payment and could still be completed at a later date.

<?php
$openpay = Openpay::getInstance('mzdtln0bmtms6o3kck8f', 
  'sk_e568c42a6c384b7ab02cd47d2e407cab');

$charge = $openpay->charges->get('trxxqosgmdsvt3hocoun');
?>

Response:

 

{
  "id": "trxxqosgmdsvt3hocoun",
  "authorization": "2018062121001003210200378650",
  "operation_type": "in",
  "method": "alipay",
  "transaction_type": "charge",
  "status": "completed",
  "conciliated": false,
  "creation_date": "2018-06-20T10:45:20-05:00",
  "operation_date": "2018-06-20T11:31:25-05:00",
  "description": "Cargo mediante Alipay",
  "error_message": null,
  "order_id": null,
  "due_date": "2018-06-21T10:45:20-05:00",
  "payment_method": {
    "type": "redirect",
    "url": "https://sandbox-api.openpay.mx/v1/mzdtln0bmtms6o3kck8f/charges/trxxqosgmdsvt3hocoun/redirect/"
  },
  "amount": 100,
  "currency": "MXN",
  "fee": {
    "amount": 5.4,
    "tax": 0.86,
    "currency": "MXN"
  }
}

 

Payment notification (6b)

There is the posibility that the customer is not returned to the final merchant webpage, for example, if the customer closes their browser after the payment, or if the internet communication fails during the redirect.

In order to handle these cases, the customer must also listen to the Openpay webhook notifications. As with other transactions, Openpay will send a notification once a transaction with Alipay is completed.

Note: The webhook notification may be sent before or after the customer is redirected back to your webpage.

Notification example:

 

{
  "type": "charge.succeeded",
  "event_date": "2018-06-20T11:31:24-05:00",
  "transaction": {
    "id": "trxxqosgmdsvt3hocoun",
    "authorization": "2018062121001003210200378650",
    "operation_type": "in",
    "method": "alipay",
    "transaction_type": "charge",
    "status": "completed",
    "conciliated": false,
    "creation_date": "2018-06-20T10:45:20-05:00",
    "operation_date": "2018-06-20T11:31:24-05:00",
    "description": "Cargo mediante Alipay",
    "error_message": null,
    "order_id": null,
    "due_date": "2018-06-21T10:45:20-05:00",
    "payment_method": {
      "type": "redirect",
      "url": "https://sandbox-api.openpay.mx/v1/mzdtln0bmtms6o3kck8f/charges/trxxqosgmdsvt3hocoun/redirect/"
    },
    "amount": 100,
    "currency": "MXN",
    "fee": {
      "amount": 5.4,
      "tax": 0.86,
      "currency": "MXN"
    }
  }
}

 

For more details regarding notifications, please check the notifications section

Sandbox testing

In order to test the payment in Alipay in the Sandbox environment, you can use the following Alipay credentials:

  • User: cnbuyer_6456@alitest.com
  • Password: b111111

You can also download the Alipay Sandbox App at the following link: