PHP code example of lonestonewy / omnipay-alipay

1. Go to this page and download the library: Download lonestonewy/omnipay-alipay library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lonestonewy / omnipay-alipay example snippets


$gateway = Omnipay::create('Alipay_Express');
$gateway->setPartner('8888666622221111');
$gateway->setKey('your**key**here');
$gateway->setSellerEmail('[email protected]');
$gateway->setReturnUrl('http://www.example.com/return');
$gateway->setNotifyUrl('http://www.example.com/notify');

//For 'Alipay_MobileExpress', 'Alipay_WapExpress'
//$gateway->setPrivateKey('/such-as/private_key.pem');

$options = [
    'out_trade_no' => date('YmdHis') . mt_rand(1000,9999), //your site trade no, unique
    'subject'      => 'test', //order title
    'total_fee'    => '0.01', //order total fee
];

$response = $gateway->purchase($options)->send();

$response->getRedirectUrl();
$response->getRedirectData();

//For 'Alipay_MobileExpress'
//Use the order string with iOS or Android SDK
$response->getOrderString();

$gateway = Omnipay::create('Alipay_Express');
$gateway->setPartner('8888666622221111');
$gateway->setKey('your**key**here');
$gateway->setSellerEmail('[email protected]');

//For 'Alipay_MobileExpress', 'Alipay_WapExpress'
//$gateway->setAlipayPublicKey('/such-as/alipay_public_key.pem');

$options = [
    'request_params'=> array_merge($_POST, $_GET), //Don't use $_REQUEST for may contain $_COOKIE
];

$response = $gateway->completePurchase($options)->send();

if ($response->isPaid()) {

   // Paid success, your statements go here.

   //For notify, response 'success' only please.
   //die('success');
} else {

   //For notify, response 'fail' only please.
   //die('fail');
}

$gateway    = Omnipay::create('Alipay_BatchTransGateway');
$gateway->setPartner('8888666622221111');
$gateway->setKey('your**key**here');
$gateway->setSellerEmail('[email protected]');
$gateway->setNotifyUrl('http://www.example.com/notify');

$detail_data = '流水号1^收款方账号1^收款账号姓名1^付款金额1^备注说明1|流水号2^收款方账号2^收款账号姓名2^付款金额2^备注说明2......';

$params = [
    'email'=>'[email protected]',
    'account_name'=>'merchant.name',
    'detail_data'=>$detail_data,
    'batch_no'=>$batch_no,//批号
    'batch_num'=>$batch_num,//笔数
    'batch_fee'=>$batch_fee,//总金额
    'pay_date'=>$pay_date,//付款日期
];
$response = $gateway->purchase($params)->send();

$redirect_url = $response->getRedirectUrl();

$gateway    = Omnipay::create('Alipay_RefundExpressGateway');
$gateway->setPartner('8888666622221111');
$gateway->setKey('your**key**here');
$gateway->setSellerEmail('[email protected]');
$gateway->setSignType('MD5');
$gateway->setInputCharset('UTF-8');
$gateway->setNotifyUrl('http://www.example.com/notify');

$data    = array(
    'refund_date' => date('Y-m-d H:i:s',time()),
    'batch_no'=>'1234567890',//退款编号
    'batch_num' => 1,//退款笔数
    'detail_data'=> mb_convert_encoding($this->alipay_trade_no.'^'.$amount.'^客户取消订单','GBK'),//退款数据,一次可以发起一批退款
);

$response = $gateway->refund($data)->send();
$debugData = $response->getData();

Yii::info('支付宝退款数据:'.print_r($debugData, true), 'payment');

if ($response->isSuccessful()) {
    //退款处理成功
}