Skip to content

Aop Face To Face Gateway

Loki Else edited this page May 10, 2017 · 17 revisions

Gateway (网关)

/** @var \Omnipay\Alipay\AopF2FGateway $gateway */
$gateway = Omnipay::create('Alipay_AopF2F');
$gateway->setSignType('RSA2'); //RSA/RSA2
$gateway->setAppId('the_app_id');
$gateway->setPrivateKey('path/to/the_app_private_key'); // 可以是路径,也可以是密钥内容
$gateway->setAlipayPublicKey('path/to/the_alipay_public_key'); // 可以是路径,也可以是密钥内容
$gateway->setNotifyUrl('https://www.example.com/notify');

Purchase (创建收款二维码)

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

/** @var \Omnipay\Alipay\Responses\AopTradePreCreateResponse $response */
$response = $request->send();

// 获取收款二维码内容
$qrCodeContent = $response->getQrCode();

Capture (扫码收款)

$request = $gateway->capture();
$request->setBizContent([
    'out_trade_no' => date('YmdHis') . mt_rand(1000, 9999),
    'scene'        => 'bar_code',
    'auth_code'    => '288412621343841260',  //购买者手机上的付款二维码
    'subject'      => 'test',
    'total_amount' => 0.01,
]);

/** @var \Omnipay\Alipay\Responses\AopCompletePurchaseResponse $response */
try {
    $response = $request->send();
    
    if($response->isPaid()){
        /**
         * Payment is successful
         */
    }else{
        /**
         * Payment is not successful
         */
    }
} catch (Exception $e) {
    /**
     * Payment is not successful
     */
}

Notify (异步通知)

$request = $gateway->completePurchase();
$request->setParams($_POST); //Optional


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

Refund (退款)

/** @var \Omnipay\Alipay\Requests\AopTradeRefundRequest $request */
$request = $gateway->refund();

$request->setBizContent([
  'out_trade_no' => 'The existing Order ID',
  'trade_no' => 'The Transaction ID received in the previous request',
  'refund_amount' => 18.4,
  'out_request_no' => date('YmdHis') . mt_rand(1000, 9999)
]);