PHP code example of xxtime / paytime

1. Go to this page and download the library: Download xxtime/paytime 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/ */

    

xxtime / paytime example snippets




use Xxtime\PayTime\PayTime;

// 不同网关方法略有区别
// $payTime = new PayTime('Mycard_card');
$payTime = new PayTime('Alipay');

$payTime->setOption(
    array(
        'app_id'      => 123456,
        'private_key' => '/path/to/privateKey.pem',
        'public_key'  => '/path/to/publicKey.pem',
        'return_url'  => 'http://host/returnUrl',
        'notify_url'  => 'http://host/notifyUrl',
    );
);

$payTime->purchase([
    'transactionId' => 2016121417340937383,
    'amount'        => 0.05,
    'currency'      => 'CNY',
    'productId'     => 'com.xxtime.product.1',
    'productDesc'   => '测试产品',
    'custom'        => '自定义',   // 选填
    'userId'        => '123456'   // 选填
]);


try {
    $response = $payTime->send();

    // 个别渠道需要单独处理,例如:MyCard需要存储单号后跳转(其回调无单号)
    // start call service process, only MyCard can get here now
    // do something
    // end call

    if (isset($response['redirect'])) {
        $payTime->redirect();
    }
} catch (\Exception $e) {
    // TODO :: error log
    echo $e->getMessage();
}



use Xxtime\PayTime\PayTime;

// 订单验证
$payTime = new PayTime('Alipay');
$response = $payTime->notify();
if (!$response->isSuccessful()) {
    exit('失败');
}
echo '成功';