Skip to content

PHP Sample

This PHP example is based on the Laravel framework and uses the Http library to make requests.

Signature implemention

php
class Signature
{
  public static function sign($params, $apiToken)
  {
    $str = $apiToken;

    ksort($params);
    foreach ($params as $key => $value) {
        // Ignore empty params
        if (!is_empty($value) && $key !== "sign") $str .= "&$key=$value";
    }

    return md5($str);
  }
}

Create collection order

The following shows how to create a collection order. The payout request is similar.

php
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

$params = [
  "mch_id" => 2050,             // Merchant ID
  "trans_id" => "E1763923463",  // Transaction ID of your system
  "channel" => "bank",          // Channel code
  "amount" => "200000.00",      // Order amount
  "currency" => "VND",          // Currency
  "callback_url" => "https://api.blackhole.com", // Webhook url
  "remarks" => "callme",        // Remarks
  "nonce" => Str::random(8),    // Random string
  "timestamp" => time(),        // UNIX timestamp
];

$apiToken = "0xFAKE_TOKENx0"; // API Token
$params["sign"] = Signature::sign($params, $apiToken);

$gateway = "http://接口域名/api/v1/mch/pmt-orders";
$res = Http::timeout(5)->post($gateway, $data);
if ($res->sttus() != 200) {
  // todo: handle failure
} else {
  $json = $res->json();
  if ($json["code"] != 200) {
    $reason = $json["message"]; // failed reason

    // todo: handle failure
  } else {
    // todo: handle success
  }
}

Released under the MIT License.