init
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
namespace Stemcell\Sms\Model;
|
||||
|
||||
use Stemcell\Sms\Api\Data\GatewayResponseInterface;
|
||||
use Magento\Backend\App\ConfigInterface;
|
||||
use Magento\Framework\Api\DataObjectHelper;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Math\Random;
|
||||
use Magento\Framework\Stdlib\DateTime\DateTimeFactory;
|
||||
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
|
||||
use Darabonba\OpenApi\Models\Config;
|
||||
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class AliyunSmsSender extends SmsSender
|
||||
{
|
||||
const ENDPOINT = 'dysmsapi.aliyuncs.com';
|
||||
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
public function __construct(
|
||||
ConfigInterface $config,
|
||||
Redis $redis,
|
||||
DateTimeFactory $dateTimeFactory,
|
||||
Random $random,
|
||||
GatewayResponseInterface $gatewayResponse,
|
||||
DataObjectHelper $dataObjectHelper,
|
||||
LoggerInterface $logger,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($config, $redis, $dateTimeFactory, $random, $gatewayResponse, $dataObjectHelper, $data);
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param String $field
|
||||
* @return string
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
protected function getAliyunConfig(String $field) {
|
||||
$path = 'sms/aliyun/'.$field;
|
||||
$value = (string)$this->config->getValue($path);
|
||||
if (!$value) {
|
||||
throw new LocalizedException(__('please config %path', ['path'=>$path]));
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return GatewayResponseInterface
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
protected function smsSend() : GatewayResponseInterface
|
||||
{
|
||||
$config = new Config([
|
||||
"accessKeyId" => $this->getAliyunConfig('accessKeyId'),
|
||||
"accessKeySecret" => $this->getAliyunConfig('accessKeySecret')
|
||||
]);
|
||||
$config->endpoint = self::ENDPOINT;
|
||||
$client = new Dysmsapi($config);
|
||||
$sendSmsRequest = new SendSmsRequest([
|
||||
"signName" => $this->getSmsConfig('signature'),
|
||||
"templateCode" => $this->getSmsConfig('template_code'),
|
||||
"phoneNumbers" => $this->mobile,
|
||||
"templateParam" => json_encode(['code'=>$this->code])
|
||||
]);
|
||||
|
||||
$resp = $client->sendSms($sendSmsRequest)->body->toMap();
|
||||
$this->logger->info('Aliyun Sms api response', $resp);
|
||||
$resp[GatewayResponseInterface::RESPONSE] = $resp;
|
||||
$resp[GatewayResponseInterface::MESSAGE] = $resp['Message'];
|
||||
$resp[GatewayResponseInterface::TYPE] = $this->getSenderType();
|
||||
$resp[GatewayResponseInterface::IS_SUCCESS] = $resp['Code']==='OK';
|
||||
|
||||
return $this->convertArrayToGatewayResponse($resp);
|
||||
}
|
||||
|
||||
protected function getSenderType(): string
|
||||
{
|
||||
return 'aliyun';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user