saleemepoch/txtnation

一个用于通过txtNation发送免费和付费短信的Laravel库。从Marc O'Leary的txtNation Gateway分支而来

v1.0 2016-05-28 00:00 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:09:28 UTC


README

txtNation Gateway PHP库与Laravel 5+兼容。它是Marc O'Leary的txtnation/txtnation-gateway-php的扩展。

先决条件

本README假定您正在使用以下PHP扩展

  • php-curl
  • php-mbstring

安装

  • 使用以下命令进行安装
composer require saleemepoch/txtnation
  • 将服务提供者添加到config/app.php文件中的$providers数组中,例如
'saleemepoch\txtNation\Providers\txtNationServiceProvider' // Laravel 5
saleemepoch\txtNation\Providers\txtNationServiceProvider::class // Laravel 5.1 or greater
  • 运行以下命令发布配置
php artisan vendor:publish

配置

  • 安装后,您需要添加您的txtNation设置。以下是在config/txtNation.php中找到的代码,您应该相应地更新它。
return [
	/* REQUIRED */
	'username' => '',
	'ekey' => '',
	// also known as sender id.
	'title' => '',

	/* OPTIONAL */
	// if set, requests to txtNation will also consists of the supplied shortcode
	'shortcode' => '',

	// required only if sending MT to Bill based on texted keywords
	'keywords' => [
		/*
		 * keywords and their corresponding amounts.
		 *
		 * This will set the value when billing the customer based on the keyword texted by the user
		 *
		 *
		'BILL10' => 10.00,
		'BILL5' => 5.00,
		*/
		'BILL1' => 1.00
	]
];

使用

  • 发送免费短信
$message = new SMSMessage;
$result = $message->msisdn('447459831491')->body('Please reply to this message with keyword PENNY!')->senderId('784645')->send();

if ($result->success()){
    dd('Message sent!');
} else {
    dd('Error sending message! Code: ' . $result->getErrorCode() . ' (' . $result->getErrorMessage() . ')');
}

  • 用户确认后向用户收费
  1. 确保在config/txtNation.php中为要收费的用户设置了关键词映射的值,例如
'keywords' => [
		'BILL10' => 10.00,
		'BILL5' => 5.00,
		'BILL1' => 1.00
	]
  1. 登录您的txtNation账户 -> APIs -> Gateway并设置您的响应URL,例如 http://example.com/txtNationResponse

  2. 在routes.php中设置路由

Route::post('txtNationResponse', 'txtNationController@response');
  1. 通过将URI添加到VerifyCsrfToken中间件的$except属性中,排除此路由的CSRF保护
protected $except = [
        'txtNationResponse',
    ];
  1. 设置一个控制器,例如txtNationController,用于此示例并创建一个方法
public function response(Request $request) {
    
    if ($request->action == 'mpush_ir_message' &&
        (isset($request->billing) && $request->billing == 'MT')) {
         
        $keywords = config('txtNation.keywords');
        
        $message = new SMSMessage;
        $result = $message->msisdn($request->number)
        ->reply(1)
        ->body('Thank you for your business')
        ->id($request->id)
        ->network($request->network)
        ->currency('GBP')
        ->value($keywords[$request->message])
        ->send();
    
    }

}

上述控制器方法将接受来自txtNation的响应,如果设置为MT收费,则向用户收费。