roamtech/gateway-api

Roamtech Gateway API 实现

v0.1.3 2020-08-05 07:17 UTC

This package is auto-updated.

Last update: 2024-09-05 16:25:45 UTC


README

安装

通过 Composer 拉取包以获取最新稳定版本。

$ composer require roamtech/gateway-api

本地插件

当使用原生 PHP 时,修改您的 composer.json 文件以包含

  "scripts": {
    "post-update-cmd": [
        "Roamtech\\Gateway\\Support\\Bootstrap::run"
    ]
  },

此脚本将默认配置文件复制到项目根目录的 config 文件夹中。现在继续引入包。

Laravel

当使用 Laravel 5.5+ 时,包将自动注册。对于 Laravel 5.4 及以下版本,在您的 config/app.php 中包含服务提供者和其别名。

'providers' => [
    Roamtech\Gateay\Laravel\ServiceProvider::class,
],

使用以下命令发布特定配置

php artisan vendor:publish --provider 'Roamtech\Gateay\Laravel\ServiceProvider'

配置

原生 PHP

对于原生 PHP,您需要初始化 sdk 引导程序以开始。

use GuzzleHttp\Client;
use Roamtech\Gateway\Engine\Core;
use Roamtech\Gateway\Native\NativeCache;
use Roamtech\Gateway\Native\NativeConfig;
use Roamtech\Gateway\Client as GatewayClient;

require "vendor/autoload.php";

$config = new NativeConfig(__DIR__.'/config/roamtechapi.php');
// Configure the HTTP client
$client = new Client(['base_uri' => $config->get('roamtechapi.api_endpoint')]);
$core = new Core($client, $config, new NativeCache($config));
$gateway = new GatewayClient($core);

Laravel

Laravel 配置很简单。安装后,API 客户端作为以下内容注册在服务容器中

$gateway = resolve('roamtech.client');
// Or Type hint it in method definitions
use Roamtech\Gateway\Client as GatewayClient;

class Mycontroller extends Controller {
    
    /** 
     * @var GatewayClient 
     */
    private $apiClient;
    
    /** 
     * Inject client in constructor
     *
     * @param GatewayClient $apiClient
     */
    public function __construct(GatewayClient $apiClient)
    {
        $this->apiClient = $apiClient;
    }
}

短信

发送批量短信

批量模式下发送短信,此端点允许您在单个 API 调用中发送多条消息。

$messages = [
    ['recipient' => '25472xxxxxxx', 'message' => 'This is a test message'],
    ['recipient' => '25471xxxxxxx', 'message' => 'This is a a custom message']
];
// Using the gateway instance we can now invoke the API with our payload

$response = $gateway->sms()->sendBulkMessages($messages, ['from' => 'YourSenderId']);
var_dump($response);

发送单条短信

此 API 允许您向一个或多个收件人发送单条消息。

$recipients = ['25472xxxxxxx', '25471xxxxxxx'];
$message = 'A test message to say hello';
$options = [
    'from' => 'YourSenderId'
    'messageId' => '345623', 
    'callback' => 'http://yoursite.com/sms/callback/345623',
];

// Let us send our message 
$response = $gateway->sms()->sendMessage($message, $recipients, $options);
var_dump($response);

获取投递报告

要获取消息的投递报告,请使用此代码片段

$messageId = '448768fjkhgcs4cykxuy8747r9c489';
$response = $gateway->sms()->getDeliveryReport($messageId);
var_dump($response);

通话时间

您还可以立即向客户充值通话时间。当您发起请求时,此 API 是异步的,我们的 API 会向您返回交易和待处理状态。然后我们将向您的应用程序发送回调,并包含最终状态。

$recipients = [
    [
        'phoneNumber' => '25472xxxxxxx',
        'amount' => 10
    ],
    [
        'phoneNumber' => '25471xxxxxxx',
        'amount' => 10
    ]
];
$callback = 'http://mysite.com/callback?id=50';

// initiate the airtime purchase transaction
$response = $gateway->airtime()
->setRecipients($recipients)
->setCallback($callback)
->purchase();

var_dump($response);