Alipay shit general payment API I believe that we have suffered a lot of torture, in WordPress using the Alipay interface to achieve Alipay payment is even more troublesome, the specific how difficult to use not to say more, used friends naturally have a deep understanding. In the PHP world, there is a payment processing library called Omnipay, the library has done a good thing, that is, a variety of payment platforms in a mess of payment gateway to the unification, so that we only need to learn a payment interface, you canImplementing Various Payment Gateways in WordPressThe premise is that Omnipay has to support this kind of gateway. Thanks to "Loki Else", we've added paypal support to Omnipay, which is what we're going to introduce today, Omnipay-Alipay.
Step 1: Installation and Configuration Omnipay-Alipay
First, install it directly through Composer:
composer require omnipay-alipay
Then, load autoload.php and configure the paypal account.
// Load the libraries installed by composer
require_once( dirname( __FILE__ ) . '/... /vendor/autoload.php' );
use Omnipay\Omnipay;
// Create a function that calls the Alipay payment gateway to be called in other files
function get_gate_way(){
$gateway = Omnipay::create( 'Alipay_Express' );
$gateway->setPartner( '*******' ); //paypal PID
$gateway->setKey( '*****************' ); //Alipay Key
$gateway->setSellerEmail( 'admin@example.com' ); //payment account email
$gateway->setReturnUrl( 'https://www.wpzhiku.com/order_return' );; //Collector Account
$gateway->setNotifyUrl( 'https://www.wpzhiku.com/order_return' ); return $gateway->setNotifyUrl( 'https://www.wpzhiku.com/order_return' ); //Collecting account email
return $gateway.
}
Step 2: Call the interface, jump to paypal payment
Provide the data necessary to generate the order, such as order number, order amount order title, etc., create the order, and jump to Alipay payment.
// Online Recharge
new Dispatch( [
'online_charge' => function ( $request ) {
$mount = trim( $_POST['mount'] ) ;
$tn = "p4" . order_no();
# Calculate the total amount
$total_fee = array_sum( $mount ); # Calculate the total amount of money
// Create the order data, either by submitting it on the frontend or automatically.
$options = [
'out_trade_no' => $tn, // generate unique order number
'subject' => 'Online Recharge', //Order Title
'total_fee' => $total_fee, //total order amount
];
// Get the payment gateway
$gateway = get_gate_way();
$response = $gateway->purchase( $options )->send();
// Go directly to paypal
$response->redirect();
},
] );
Step 3: Process the returned data and process the order according to the returned data
Here is the content of return_url, according to the parameters received, contact Alipay to verify the payment status of the order, according to the verification status to determine the success of the payment, if the payment is successful, processing orders (here is a direct display of a successful payment of the string), if the payment fails to return to the payment failure information.
// Process the Alipay return
new Dispatch( [
'order_return' => function ( $request ) {
$options = [
'request_params' => $_REQUEST, [
];
$gateway = get_gate_way();
$response = $gateway->completePurchase( $options )->send();
if ( $response->isSuccessful() && $response->isTradeStatusOk() ) {
$total_fee = $_GET[ 'total_fee' ];
$trade_no = $_GET[ 'trade_no' ];
$out_trade_no = $_GET[ 'out_trade_no' ];
$subject = $_GET[ 'subject' ];
$body = $_GET[ 'body' ];
echo "Payment successful" ;
} else {
echo "Payment Failed"; } else {
}
}
)]
The above sample code, just a simple introduction to the use of Omnipay-Alipay method and process, in the specific project, we still have to according to our needs, to realize the specific business logic, this will not say more here. In addition, we can also find Omnipay-UnionPay (UnionPay) and Omnipay-WeChat (WeChat Pay)These are two common domestic payment platform processing libraries.