Skip to content
Max Sky edited this page May 15, 2019 · 7 revisions

Gateway (网关)

/**
 * @var AopAppGateway $gateway
 */
$gateway = Omnipay::create('Alipay_AopApp');
$gateway->setSignType('RSA2'); //RSA/RSA2
$gateway->setAppId('the_app_id');
$gateway->setPrivateKey('the_app_private_key');
$gateway->setAlipayPublicKey('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',
    'product_code' => 'QUICK_MSECURITY_PAY',
]);

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

$orderString = $response->getOrderString();

iOS 客户端

[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
    NSLog(@"reslut = %@",resultDic);
}];

Android 客户端

PayTask alipay = new PayTask(PayDemoActivity.this);
Map<String, String> result = alipay.payV2(orderString, true);

Return (同步验证)

$request = $gateway->completePurchase();
$request->setParams([
    'memo'         => '',
    'result'       => '{\"alipay_trade_app_pay_response\":{\"code\":\"10000\",\"msg\":\"Success\",\"app_id\":\"20151128008123456\",\"auth_app_id\":\"20151128008123456\",\"charset\":\"UTF-8\",\"timestamp\":\"2016-09-23 18:32:16\",\"total_amount\":\"0.01\",\"trade_no\":\"2016092321001003060123456789\",\"seller_id\":\"2088011466123456789\",\"out_trade_no\":\"201609231231447556\"},\"sign\":\"Q5n3zKIBzhBobd6Z6mP69ZvaBlVxkWOiti2ZCRBRhfEH8/sCWE89Iev94K++QH8W9Zakn9dXTq2tR0O5UWLS1XXgiSd+vUTMQNksxjddI39MQnbJ1hxEtwP5GcWzxeY9YRjXXJdzgdf/xmRS7uRQWv52cGYStlCNN/dianZmuDk=\",\"sign_type\":\"RSA\"}',
    'resultStatus' => '9000'
]);

/**
 * @var 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 (支付异步通知)

try {
    /** @var AopCompletePurchaseResponse $response */
    $response = $gateway->completePurchase()->setParams($_POST)->send();
} catch (InvalidRequestException $e) {
    /** Payment is not successful */
    die('fail');
}

if ($response->isPaid()) {
    /** Payment is successful */
    die('success'); //The response should be 'success' only
} else {
    /** Payment is not successful */
    die('fail');
}

Refund notify (退款异步通知)

try {
    /** @var AopCompleteRefundResponse $response */
    $response = $gateway->completeRefund()->setParams($_POST)->send();
} catch (InvalidRequestException $e) {
    /** Refund is not successful */
    die('fail');
}

if ($response->isRefunded()) {
    /** Refund is successful */
    die('success'); //The response should be 'success' only
} else {
    /** Refund is not successful */
    die('fail');
}