juspay/expresscheckout-php-sdk

PHP Express Checkout SDK 便于与Juspay的EC支付服务平台轻松集成

v2.0.4 2024-09-02 06:45 UTC

README

Juspay ExpressCheckout PHP SDK 可以让商家更容易地将快速结账API集成到他们的产品中。此SDK使用 composer 分发。要将SDK添加到项目中,请将以下代码添加到您的 composer.json

#!python

"require" : {
	"juspay/expresscheckout-php-sdk" : "1.0.4"
}

此包需要 minimum-stability 至少为 stable。请在您的 composer.json 中相应地设置 minimum-stability

设置SDK以供使用。

默认情况下,SDK已初始化为Juspay生产账户。

要使用默认超时设置配置PHP SDK以用于生产账户,请使用以下代码

JuspayEnvironment::init()
->withApiKey("your_api_key")

要使用默认超时设置配置PHP SDK以用于沙箱账户,请使用以下代码

JuspayEnvironment::init()
->withApiKey("yourApiKey")
->withBaseUrl(JuspayEnvironment::SANDBOX_BASE_URL)

要使用自定义超时设置配置PHP SDK以用于生产账户,请使用以下代码

JuspayEnvironment::init()
->withApiKey("yourApiKey")
->withConnectTimeout(connectTimeout)
->withReadTimeout(readTimeout);

要使用自定义超时设置配置PHP SDK以用于沙箱账户,请使用以下代码

JuspayEnvironment::init()
->withApiKey("yourApiKey")
->withBaseUrl(JuspayEnvironment::SANDBOX_BASE_URL)
->withConnectTimeout(connectTimeout)
->withReadTimeout(readTimeout);

要使用自定义CA证书配置PHP SDK,请使用以下代码

JuspayEnvironment::init()
->withApiKey("yourApiKey")
->withBaseUrl(JuspayEnvironment::SANDBOX_BASE_URL)
->withCACertificatePath("file path to ca certificate");

使用SDK

SDK中所有方法都接受关联数组作为输入,大部分方法将返回相应类的对象。

示例

将卡片添加到Juspay锁仓

$params = array ();
$params ['merchant_id'] = "merchantId";
$params ['customer_id'] = "customerId";
$params ['customer_email'] = "[email protected]";
$params ['card_number'] = "4111111111111111";
$params ['card_exp_year'] = "2018";
$params ['card_exp_month'] = "07";
$params ['name_on_card'] = "Juspay Technologies";
$params ['nickname'] = "ICICI VISA";
$card = Card::create ( $params );

使用JWT获取订单状态

在请求选项中传递JuspayJWT。JuspayJWT实现了IJuspayJWT接口。IJuspayJWT有三个方法 consumePayload, preparePayload 和 Initialize(一个用于初始化ISign和IEnc对象的工厂方法),以及三个属性:键数组、ISign类型的Sign和IEnc类型的Enc。目前,JuspayJWT使用SignRSA5,它是ISign接口的实现,EncRSAOEAP是IEnc接口的实现。目前,JuspayJWT类与SDK一起提供。实现IJuspayJWT以创建自定义JWT类。JuspayJWT构造函数接受$keys和两个kid作为参数。

带请求选项

$params = array ();
$params ['order_id'] = $this->order->orderId;
$keys = [];
$privateKey = file_get_contents("./tests/privateKey.pem");
$publicKey = file_get_contents("./tests/publicKey.pem");
$order = Order::encryptedOrderStatus($params, new RequestOptions(new JuspayJWT("testJwe", $publicKey, $privateKey)));

带Juspay环境

$params = array ();
$params ['order_id'] = $this->order->orderId;
$keys = [];
$privateKey = file_get_contents("./tests/privateKey.pem");
$publicKey = file_get_contents("./tests/publicKey.pem");
JuspayEnvironment::init()->withJuspayJWT(new JuspayJWT("testJwe", $publicKey, $privateKey));
$order = Order::status($params, null);

错误处理

<?php
use Juspay\Exception\AuthenticationException;
use Juspay\Exception\APIConnectionException;
use Juspay\Exception\APIException;
use Juspay\Exception\InvalidRequestException;
use Juspay\Exception\JuspayException;
use Juspay\Model\Order;
try {
$params = array ();
$params ['order_id'] = "order id";
$order = Order::status ( $params );
}
catch (APIConnectionException ex)
{
// Handle API connection exception
}
catch (APIException ex)
{
// Handle API exception
}
catch (AuthenticationException ex)
{
// Handle Authentication exception
}
catch (InvalidRequestException ex)
{
// Handle invalid request exception
}
catch (JuspayException ex)
{
// All above exception extends juspay exception
// Default exception handler
}

示例集成

<?php
use Juspay\JuspayEnvironment;
use Juspay\Model\Order;
use Juspay\Exception\JuspayException;
JuspayEnvironment::init ()
->withApiKey ("api key")
->withBaseUrl ("base url")
->withJuspay(new JuspayJWT($keys, "public key id");
try {

    // create order
    $orderId = uniqid ();
    $params = array ();
    $params ['order_id'] = $orderId;
    $params ['amount'] = 10000.0;
    $order = Order::create ( $params );
    $orderId = $order->orderId;

    // Get order
    $params = array ();
    $params ['order_id'] = $orderId;
    $order = Order::status ( $params );
    $status = $order->status; // verify status of the order ("NEW", "CHARGED"..)

    // update order
    $params = array ();
    $params ['amount'] = $order->amount + 100;
    $order = Order::update ( $params, $orderId );

    //Refund order
    $params = array ();
    $params ['order_id'] = $orderId;
    $params ['amount'] = 10;
    $params['unique_request_id'] = uniqid('php_sdk_test_');
    $order = Order::refund ( $params );
    $amountRefunded = $order->amountRefunded


}
catch (JuspayException $ex) {
    // Handle exception
}


## To Run Test

### PHP Version >=7.1 and <= 7.2
```shell
composer install --dev
./vendor/bin/phpunit tests -c ./phpunit-config-php7.xml

PHP版本 > 7.2

composer install --dev
./vendor/bin/phpunit tests -c ./phpunit-config-php8.xml

PHP版本 5

./vendor/bin/phpunit tests -c ./phpunit-config-php7.xml

注意:由于OTP请求限制,钱包测试认证可能会失败。