Skip to content

<10 minutes solution to fully setup alipay testing env online (Alipay-api-php / Docker / Localtunnel)

License

Notifications You must be signed in to change notification settings

31ten/easy-alipay-php

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ready to use Alipay Testing Environment

Goal

Designed to be able to test and understand how Alipay payment works within 10 minutes, using a local environment.

Fork of https://github.com/bitmash/alipay-api-php including a ready to use docker php server, a basic logging system and extra-instructions to have a working environment tunneled through an URL like https://jxowrmxnbm.localtunnel.me to be able to fully test alipay interactions (especially for the notify workflow that requires Alipay server to do a POST request on an accessible URL (localhost won't work here))

Dependencies

  • docker
  • npm
  • localtunnel (npm install -g localtunnel) or similar tunneling solution

Installation

  • git clone
  • cd alipay-testing-environment/docker
  • docker-compose up -d
  • lt --port 8888
  • modify the URL in tests/transaction.php by the one provided by localtunnel (https://YOURURL.localtunnel.me)
  • go to https://YOURURL.localtunnel.me/tests/transaction.php
  • when choosing the payment method on alipay gateway, choose to login with the computer with their testing account (see details below)
  • Check the php logs in logs/log.txt
  • If some answers are missing, check the docker/nginx/access.log to see if alipay servers are sending requests

About the Alipay php Library

Fork of https://github.com/bitmash/alipay-api-php

This is a concise library written in PHP for the Alipay API merchant account for overseas merchants. It's basic and only offers support for creating direct pay by user transactions - also known as "Cross-border Website Payment". Alipay provides documentation and code examples at https://globalprod.alipay.com/order/integrationGuide.htm.

This library does not support Transaction Queries, Refunds and Reconciliation. I may add these another time unless someone else wants to take a crack at it.

Development

Alipay provides a test environment with a dummy account and test links assuming it's working.

Account

A test account is provided by Alipay.

Usage

All of this information is to the best of my knowledge, so there may be some inaccurate details here. You will need to automatically verify that the transaction is authentic by comparing their sign with yours that you compute from the response. You also need to verify that the notify_id Alipay sends to your return_url is valid by sending a GET request to https://mapi.alipay.com/gateway.do?service=notify_verify.... The notify_id is set to expire within a minute. If the transaction is authentic, you will receive a response of true; otherwise, it will be false.

return_url

The response will be in the GET data when Alipay sends the user back to your specified return_url. Check the trade_status once they return to see if it's already completed (TRADE_FINISHED).

notify_url

In some cases the payment verification takes longer on Alipay's end so they will ping your specified notify_url. The response will be in the POST data. Be sure to disable any CSRF security and remove the login requirement you might have in place for the notify_url.

Transaction Response Types

  • WAIT_BUYER_PAY - waiting for the buyer to pay.
  • TRADE_CLOSED - transaction closed without payment.
  • TRADE_FINISHED - payment was successful, and transaction is closed.

Response Handler

Be sure to store a unique ID to reference later on the return or notify URLs. For example, both the return_url and notify_url can be set to https://yoursite.com/account/add_funds/id/<id>. You can do a look-up in your own database to make sure the transaction hasn't already been completed in the past, and it can be linked to the user that initiated the request, along with other transaction details. After the response is handled and verified, you can then redirect the user to another page.

About

<10 minutes solution to fully setup alipay testing env online (Alipay-api-php / Docker / Localtunnel)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%