tendopay/tendopay-sdk-php

0.9.3 2024-09-10 15:03 UTC

This package is not auto-updated.

Last update: 2024-09-25 00:57:59 UTC


README

如果你找到了v1版本的文档,请访问TendoPay SDK for PHP (v1)

要求

PHP 7.0及以上。

升级

从v1升级

安装

使用Composer

您可以通过Composer安装SDK。运行以下命令

composer require tendopay/tendopay-sdk-php

运行SDK测试器

  • 运行示例服务器
php -s localhost:8000 -t vendor/tendopay/tendopay-sdk-php/samples
  • 打开浏览器并访问
https://:8000/

代码示例

创建TendoPayClient

## Client Credentials
CLIENT_ID=
CLIENT_SECRET=

## Redirect URI when the transaction is processed 
REDIRECT_URL=https://:8000/purhase.php

## Enable Sandbox, it must be false in production
TENDOPAY_SANDBOX_ENABLED=false
use TendoPay\SDK\TendoPayClient;

$client = new TendoPayClient();
  • 使用$config变量
use TendoPay\SDK\TendoPayClient;

$config = [
    'CLIENT_ID' => '',
    'CLIENT_SECRET' => '',
    'REDIRECT_URL' => '',
    'TENDOPAY_SANDBOX_ENABLED' => false,
];
$client = new TendoPayClient($config);

发起支付

use TendoPay\SDK\Exception\TendoPayConnectionException;
use TendoPay\SDK\Models\Payment;
use TendoPay\SDK\V2\TendoPayClient;

### S:Merchant set proper values
$merchant_order_id = $_POST['tp_merchant_order_id'];
$request_order_amount = $_POST['tp_amount'];
$request_order_title = $_POST['tp_description'];
$redirectUrl = $_POST['tp_redirect_url'] ?? '';
### E:Merchant set proper values

$client = new TendoPayClient();

try {
    $payment = new Payment();
    $payment->setMerchantOrderId($merchant_order_id)
        ->setDescription($request_order_title)
        ->setRequestAmount($request_order_amount)
        ->setCurrency('PHP')
        ->setRedirectUrl($redirectUrl);


    $client->setPayment($payment);

    $redirectURL = $client->getAuthorizeLink();
    header('Location: '.$redirectURL);
} catch (TendoPayConnectionException $e) {
    echo 'Connection Error:'.$e->getMessage();
} catch (Exception $e) {
    echo 'Runtime Error:'.$e->getMessage();
}

回调(重定向页面)

use TendoPay\SDK\Exception\TendoPayConnectionException;
use TendoPay\SDK\Models\VerifyTransactionRequest;
use TendoPay\SDK\V2\TendoPayClient;

$client = new TendoPayClient();

try {
    if (TendoPayClient::isCallBackRequest($_REQUEST)) {
        $transaction = $client->verifyTransaction(new VerifyTransactionRequest($_REQUEST));

        if (!$transaction->isVerified()) {
            throw new UnexpectedValueException('Invalid signature for the verification');
        }

        if ($transaction->getStatus() == \TendoPay\SDK\V2\ConstantsV2::STATUS_SUCCESS) {
            // PAID
            // Save $transactionNumber here
            // Proceed merchant post order process
        } else if ($transaction->getStatus() == \TendoPay\SDK\V2\ConstantsV2::STATUS_FAILURE) {
            // FAILED
            // do something in failure case
            // error message $transaction->getMessage()
        }       
    }
} catch (TendoPayConnectionException $e) {
    echo 'Connection Error:'.$e->getMessage();
} catch (Exception $e) {
    echo 'Runtime Error:'.$e->getMessage();
}

取消支付

use TendoPay\SDK\Exception\TendoPayConnectionException;
use TendoPay\SDK\V2\TendoPayClient;

$client = new TendoPayClient();

try {
    $client->cancelPayment($transactionNumber);
    // merchant process here

} catch (TendoPayConnectionException $e) {
    echo 'Connection Error:'.$e->getMessage();
} catch (Exception $e) {
    echo 'Runtime Error:'.$e->getMessage();
}

显示交易详情

use TendoPay\SDK\Exception\TendoPayConnectionException;
use TendoPay\SDK\V2\TendoPayClient;

$client = new TendoPayClient();

try {

    $transaction = $client->getTransactionDetail($transactionNumber);

    // merchant process here
    // $transaction->getMerchantId();
    // $transaction->getMerchantOrderId();
    // $transaction->getAmount();
    // $transaction->getTransactionNumber();
    // $transaction->getCreatedAt();
    // $transaction->getStatus();
    
} catch (TendoPayConnectionException $e) {
    echo 'Connection Error:'.$e->getMessage();
} catch (Exception $e) {
    echo 'Runtime Error:'.$e->getMessage();
}