counos/counospay-php

CounosPayment Php库

V1.1 2020-07-01 06:10 UTC

This package is auto-updated.

Last update: 2024-09-29 05:48:50 UTC


README

PHP Counos支付网关客户端

安装

通过Composer轻松安装

composer require counos/counospay-php

用法

要使用此库,首先您必须有一个API密钥。访问https://payment.counos.io/account/terminals,然后添加您的终端并获取API密钥。

连接到Counos支付

use Counos\CounosPay\Payment;

$counospay_api = new Payment("YOUR_COUNOS_PAYMENT_GATEWAY_API_KEY");

获取终端的所有激活代币

try
{
    $tickers = $counospay_api->Tickers();
}
catch (Exception $e)
{
    die("Cant't connect to payment gateway. please try again.");
}

创建新的支付

try
{
    /**
     * $invoice_id must be unique per invoice, payment gateway uses this id to check the payment status, it can be a string (eg. inv-1, inv-vps-service-125)
     */
    $invoice_id    = 1;
    $ticker        =  'cca'; //$_POST['ticker'];
    $amount        = 9.99;
    $base_currency = 'USD';
    /**
     * @var $payment Counos\CounosPay\Models\Response\Order
     */
    $payment             = $counospay_api->NewOrderFromFiat($invoice_id, $base_currency, $ticker, $amount, true);
    $new_payment_created = [
        'invoice_id'      => $invoice_id,
        'payment_id'      => $payment->id,
        'confirmations'   => $payment->paymentConfirmations,
        'paid_amount'     => $payment->paidAmount,
        'expected_amount' => $payment->expectedAmount,
        'address'         => $payment->orderAddress,
        'ticker'          => $payment->ticker->keyword,
        'transaction_id'  => $payment->transactionId,
        'payment_uri'     => $payment->paymentUriQrCode,
        'paid'            => $payment->paid,
        'base_ticker'     => $base_currency,
        'base_amount'     => $amount,
    ];
    //In this step you must save this info (e.g. in db) then redirect user to a page that shows payment QR code and other info.
}
catch (Exception $e)
{
    die("Cant't connect to payment gateway. please try again.");
}

检查支付状态

为了定期检查支付状态,必须在cron作业中声明

try
{
    try
    {
        $payment = $counospay_api->OrderStatus($invoice_id);
    }
    catch (Exception $e)
    {
        //log errors
    }

    $db_invoice_model = retrieveInvoiceFromDB($invoice_id);

    /**
     * When a payment has been completed and reaches the defined number of confirmations, 'paid' turns to true, in this stage you must change the invoice status to 'PAID' and do some stuff for order.
     */
    if ($payment->paid)
    {
        $db_invoice_model->status = 'paid';
        //notice the user about the payment status
        //place order
        //and etc.
    }
    else if ($payment->paidAmount >= $payment->expectedAmount)
    {
        $db_invoice_model->paid_status = 'await_confirmations';
        //show a progress bar to the user about how many confirmations...
    }
    else
    {
        //user not yet made the payment
    }
}
catch (Exception $e)
{
    die("Cant't connect to payment gateway. please try again.");
}
function retrieveInvoiceFromDB($invoice_id)
{
    //get invoice model from db and return
}