Yuansfer DOCS
  • Introduction
  • GUIDE
    • Online Payment
    • Point of Sale
      • Scan QR Code
      • Create QR Code
    • Plugins
    • Payment Methods
    • Financial Report
    • Security
    • Incident Priorities
    • SDK
    • Tutorials & Examples
      • PHP SDK
      • JAVA SDK
      • C# SDK
      • JS SDK
  • API REFERENCE V3
    • Sandbox Environment
      • Apply Sandbox Credentials
    • Signing API Parameters
    • Customers
      • Register Customer
      • Retrieve Customer
      • Update Customer
    • Payments
      • Online Payment
        • Yuansfer Checkout
        • Yuansfer Integrated Payment
          • Braintree Payments
          • Prepay
        • Recurring Payments
          • Authorize
          • Apply Token
          • Pay
          • Revoke
      • Point of Sale Payment
        • Scan QR Code
          • Add
          • Prepay
        • Create QR Code
    • Transaction Revert
      • Refund
      • Cancel
    • Transaction Data Search
      • Transaction Query
    • Payouts
      • Create Payee
      • Retrieve Payee
      • Balance
      • Send Money
      • Search Payments
    • Notes
  • 中文
Powered by GitBook
On this page
  • Requirements
  • Installation
  • Example

Was this helpful?

  1. GUIDE
  2. Tutorials & Examples

PHP SDK

PreviousTutorials & ExamplesNextJAVA SDK

Last updated 4 years ago

Was this helpful?

Requirements

  • PHP >= 5.3

  • CURL extension

Installation

With Composer (recommended)

1. Install composer:

$ curl -sS https://getcomposer.org/installer | php

2. Run the Composer command to install the latest version of SDK:

php composer.phar require yuansfer/yuansfer-php-sdk

3. Require Composer's autoloader in your PHP script (assuming it is in the same directory where you installed Composer):

require('vendor/autoload.php');

PHAR with bundled dependencies

This is not recommended! Use as a modern way of working with PHP packages.

1. Download

2. Require files:

require('path-to-sdk/yuansfer-php-sdk.phar');

Please keep in mind that bundled dependencies may interfere with your other dependencies.

Example

1. Init

use Yuansfer\Yuansfer;

$config = array(
    Yuansfer::MERCHANT_NO => 'The merchant NO.',
    Yuansfer::STORE_NO => 'The store NO.',
    Yuansfer::API_TOKEN => 'Yuansfer API token',
    Yuansfer::TEST_API_TOKEN => 'Yuansfer API token for test mode',
);

$yuansfer = new Yuansfer($config);

2. Create API

$api = $yuansfer->createSecurePay();

3. Set API Parameters

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

4.1. Send

$response = $api->send();

4.2. Use Test Mode

$yuansfer->setTestMode();
$response = $api->send();

5. API return JSON, already decoded as an array

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

6. Exceptions when sending

try {
    $response = $api->send();
} catch (\Yuansfer\Exception\YuansferException $e) {
    // required param is empty
    if ($e instanceof \Yuansfer\Exception\RequiredEmptyException) {
        $message = 'The param: ' . $e->getParam() . ' is empty, in API: ' . $e->getApi();
    }

    // http connect error
    if ($e instanceof \Yuansfer\Exception\HttpClientException) {
        $message = $e->getMessage();
    }

    // http response status code < 200 or >= 300, 301 and 302 will auto redirect
    if ($e instanceof \Yuansfer\Exception\HttpErrorException) {
        /** @var \Httpful\Response http response */
        $response = $e->getResponse();
    }
}
Composer
PHAR file