PHP code example of transfereasy / pay

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

    

transfereasy / pay example snippets




ansfereasy\Pay\TE;
use Transfereasy\Pay\Exception\Exception;

//1. 设置config
 $config = [
            'm_private_key_path' => '',//商户私钥文件路径,如:'./merchant_private_test.key'
            't_public_key_path' => '', //TE公钥文件路径 如: './te_public_test.key'
            't_merchant_no' => '80000138', // TE商户号
            't_product_code' => 'CP0001', // 产品号
            'env' => 'test', //设置为测试环境,生产环境可忽略该参数
        ];

try {
    //2. 发起API调用(以收单下单接口为例)
   $params = [
            'amount' => 100,
            'clientIp' => '127.0.0.1',
            'currency' => 'HKD',
            'notifyUrl' => 'https://demo-test.transfereasy.com/demo/paymentNotify',
            'outTradeNo' => 'T2024021811010030',
            'storeTerminalId' => 's',
            'settleCurrency' => 'HKD',
            'productId' => 'xx',
            'productInfo' => [
                [
                    'description' => "测试商品 1 个",
                    'amount' => 100,
                    'name' => "测试商品",
                    'quantity' => 1
                ]
            ],
            'returnUrl' => 'https://demo-test.transfereasy.com/demo/returnUrl',
            'tradeType' => 'NATIVE'
        ];
        //如果调用V2接口使用TE::transactionV2($config)
        $get_payment_result = TE::transaction($config)->payment($params);
        //处理自己的业务逻辑
        //...
} catch (Exception $e) {
    echo "调用失败,". $e->getMessage(). PHP_EOL;;
}



   //以laravel 为例子回调处理 
   public function paymentNotify(Request $request) {

        Log::info($request->getContent());
        Log::info($request->header('Signature'));
        Log::info($request->header('Timestamp'));
        $config = [
            'm_private_key_path' => storage_path('app/merchant_private_test.key'),//商户私钥文件路径,如:'./merchant_private_test.key'
            't_public_key_path' => storage_path('app/te_public_test.key'), //TE公钥文件路径 如: './te_public_test.key'
            't_merchant_no' => '80000138', // TE商户号
            't_product_code' => 'CP0009', // 产品号
            'env' => 'test', //设置为测试环境,生产环境可忽略该参数
        ];
        try {
            $sign = $request->header('Signature');
            $timestamp = $request->header('Timestamp');

            $get_res = TE::transaction($config)->notify($request->getContent(), $sign, $timestamp);
            Log::info($get_res);
            //验签成功后,将返回TE的通知body数组
            //return TE::transaction($config)->success();
            return Response('SUCCESS', 200); //laravel返回
        }catch (Exception $e) {
            echo "调用失败,". $e->getMessage(). PHP_EOL;;
        }
        //return TE::transaction($config)->fail(500, 'error');
        return response()->json(['message' => 'error'], 500);
    }