Skip to content
Kuang Jiaye edited this page Jul 9, 2018 · 2 revisions

Gateway (网关)

/**
 * @var AopPageGateway $gateway
 */
$gateway = Omnipay::create('Alipay_AopPage');
$gateway->setSignType('RSA2'); //RSA/RSA2
$gateway->setAppId('the_app_id');
$gateway->setPrivateKey('the_app_private_key');
$gateway->setAlipayPublicKey('the_alipay_public_key');
$gateway->setReturnUrl('https://www.example.com/return');
$gateway->setNotifyUrl('https://www.example.com/notify');

Purchase (购买)

$request = $gateway->purchase();
$request->setBizContent([
    'out_trade_no' => date('YmdHis') . mt_rand(1000, 9999),
    'total_amount' => 0.01,
    'subject'      => 'test',
    'product_code' => 'FAST_INSTANT_TRADE_PAY',
]);

/**
 * @var AopTradePagePayResponse $response
 */
$response = $request->send();

$redirectUrl = $response->getRedirectUrl();
//or 
$response->redirect();

Return && Notify (同步、异步通知)

$request = $gateway->completePurchase();
$request->setParams(array_merge($_POST, $_GET)); //Don't use $_REQUEST for may contain $_COOKIE

/**
 * @var AopCompletePurchaseResponse $response
 */
try {
    $response = $request->send();
    
    if($response->isPaid()){
        /**
         * Payment is successful
         */
        die('success'); //The notify response should be 'success' only
    }else{
        /**
         * Payment is not successful
         */
        die('fail'); //The notify response
    }
} catch (Exception $e) {
    /**
     * Payment is not successful
     */
    die('fail'); //The notify response
}