sirumobile / siru-php-sdk

Siru Payment Gateway 的 PHP 软件开发包。

1.0.6 2021-12-22 04:37 UTC

This package is auto-updated.

Last update: 2024-09-22 10:29:56 UTC


README

Siru Payment Gateway 的 PHP 7.1+ 软件开发包。

Tests

要求

  • PHP 7.1+
  • 支持以下传输方式之一的 HTTP 客户端或您自己的传输。内置传输支持 Guzzle、Symfony HTTP 客户端和 WordPress HTTP API。

安装

包含 SDK 的最简单方法是使用 composer。打开命令控制台,进入您的项目目录并执行

$ composer require sirumobile/siru-php-sdk:^1.0

或者通过在您的 composer.json 文件中添加以下行

{
    "require": {
        "sirumobile/siru-php-sdk": "^1.0"
    }
}

使用方法

要开始使用,您需要您的 merchantId 和 merchant secret。如果您没有凭据,请联系 Siru Mobile 讨论您可用的支付方式,我们将向您发送您的沙盒凭据。

然后,通过我们的 API 文档 了解每个 API 和消息负载的更多信息。

示例

这是一个创建新交易并将用户重定向到 Siru 支付流程的简单示例

# web/checkout.php

require_once('../vendor/autoload.php');

/**
 * Siru\Signature is used to sign outgoing messages and verify
 * responses from API. Replace $merchantId and $secret with your own credentials.
 */
$signature = new \Siru\Signature($merchantId, $secret);

/**
 * Siru\API is used to retrieve API specific classes and setting default values
 * for outgoing requests. It requires instance of Siru\Signature as parameter.
 */
$api = new \Siru\API($signature);

// Select sandbox environment (default)
$api->useStagingEndpoint();

// You can set default values for all payment requests (not required)
$api->setDefaults([
  'variant' => 'variant4',
  'purchaseCountry' => 'GB'
]);

// Create payment
try {

  $transaction = $api->getPaymentApi()
    ->set('basePrice', '5.00')
    ->set('redirectAfterSuccess', 'https://my-shop.com/checkout/success')
    ->set('redirectAfterFailure', 'https://my-shop.com/checkout/failure')
    ->set('redirectAfterCancel', 'https://my-shop.com/checkout/cancel')
    ->set('customerNumber', '0401234567')
    ->set('title', 'Concert ticket')
    ->set('description', 'Concert ticket to see an awesome band live')
    ->createPayment();
  
  header('location: ' . $transaction['redirect']);
  exit();

} catch(\Siru\Exception\TransportException $e) {
  echo "Unable to contact Payment API. Error was: " . $e->getMessage();

} catch(\Siru\Exception\ApiException $e) {
  echo "API request failed with error code " . $e->getCode() . ": " . $e->getMessage();
  foreach($e->getErrorStack() as $error) {
    echo $error . "<br />";
  }
}

在您的 redirectAfter* URL 上,您需要验证用户是否确实来自 Siru Mobile 支付流程,并且所有参数都是真实的

# /web/checkout/success.php, failure.php or cancel.php
$signature = new \Siru\Signature($merchantId, $secret);

if(isset($_GET['siru_event']) == true) {
  if($signature->isNotificationAuthentic($_GET)) {
    // User was redirected from Siru payment page and query parameters are authentic
  }
}

建议您还设置一个回调 URL,使用 notifyAfterSuccess、notifyAfterFailure 和 notifyAfterCancel 字段,Siru 将在支付状态更改时自动发送通知。这允许您在用户由于网络故障等无法返回您的结账页面的情况下完成交易。

// /web/checkout/callback.php
$signature = new \Siru\Signature($merchantId, $secret);

$entityBody = file_get_contents('php://input');
$entityBodyAsJson = json_decode($entityBody, true);

if($signature->isNotificationAuthentic($entityBodyAsJson)) {
  // Notification was sent by Siru Mobile and is authentic
}

您还可以使用 \Siru\Signature 作为独立组件为您自己的代码创建签名。

/**
 * Imaginary example on calculating Signature without using \Siru\API.
 */
$paymentRequestFields = [
  // ... required fields as described in API documentation.
];

$hash = $signature->createMessageSignature($paymentRequestFields, [], Signature::FILTER_EMPTY | Signature::SORT_FIELDS);
$paymentRequestFields['signature'] = $hash;
$paymentRequestJson = json_encode($paymentRequestFields);

// Send request using what ever HTTP
$response = $myHttpClient->send('https://staging.sirumobile.com', 'POST', $paymentRequestJson);

// Then you would check API response status, parse the JSON string in response body
// and redirect user to the payment page.

API 文档

API 文档可在 此处 获得。