Yuansfer DOCS
Search…
PHP SDK

Requirements

    PHP >= 5.3
    CURL extension

Installation

1. Install composer:
1
$ curl -sS https://getcomposer.org/installer | php
Copied!
2. Run the Composer command to install the latest version of SDK:
1
php composer.phar require yuansfer/yuansfer-php-sdk
Copied!
3. Require Composer's autoloader in your PHP script (assuming it is in the same directory where you installed Composer):
1
require('vendor/autoload.php');
Copied!

PHAR with bundled dependencies

This is not recommended! Use Composer as a modern way of working with PHP packages.
1. Download PHAR file
2. Require files:
1
require('path-to-sdk/yuansfer-php-sdk.phar');
Copied!
Please keep in mind that bundled dependencies may interfere with your other dependencies.

Example

1. Init

1
use Yuansfer\Yuansfer;
2
3
$config = array(
4
Yuansfer::MERCHANT_NO => 'The merchant NO.',
5
Yuansfer::STORE_NO => 'The store NO.',
6
Yuansfer::API_TOKEN => 'Yuansfer API token',
7
Yuansfer::TEST_API_TOKEN => 'Yuansfer API token for test mode',
8
);
9
10
$yuansfer = new Yuansfer($config);
Copied!

2. Create API

1
$api = $yuansfer->createSecurePay();
Copied!

3. Set API Parameters

1
$api
2
->setAmount(9.9) //The amount of the transaction.
3
->setCurrency('USD') // The currency, USD, CAD supported yet.
4
->setVendor('alipay') // The payment channel, alipay, wechatpay, unionpay, enterprisepay are supported yet.
5
->setTerminal('ONLINE') // ONLINE, WAP
6
->setReference('44444') //The unque ID of client's system.
7
->setIpnUrl('http://domain/example/callback_secure_pay_ipn.php') // The asynchronous callback method.
8
->setCallbackUrl('http://domain/example/callback_secure_pay.php'); // The Synchronous callback method.
Copied!

4.1. Send

1
$response = $api->send();
Copied!

4.2. Use Test Mode

1
$yuansfer->setTestMode();
2
$response = $api->send();
Copied!

5. API return JSON, already decoded as an array

1
if ($response['ret_code'] === '000100') {
2
header('Location: ' . $response['result']['cashierUrl']);
3
}
Copied!

6. Exceptions when sending

1
try {
2
$response = $api->send();
3
} catch (\Yuansfer\Exception\YuansferException $e) {
4
// required param is empty
5
if ($e instanceof \Yuansfer\Exception\RequiredEmptyException) {
6
$message = 'The param: ' . $e->getParam() . ' is empty, in API: ' . $e->getApi();
7
}
8
9
// http connect error
10
if ($e instanceof \Yuansfer\Exception\HttpClientException) {
11
$message = $e->getMessage();
12
}
13
14
// http response status code < 200 or >= 300, 301 and 302 will auto redirect
15
if ($e instanceof \Yuansfer\Exception\HttpErrorException) {
16
/** @var \Httpful\Response http response */
17
$response = $e->getResponse();
18
}
19
}
Copied!
Last modified 11mo ago