<?php
namespace Alipay\EasySDK\Marketing\TemplateMessage;
use Alipay\EasySDK\Kernel\EasySDKKernel;
use AlibabaCloud\Tea\Tea;
use AlibabaCloud\Tea\Request;
use AlibabaCloud\Tea\Exception\TeaError;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
use Alipay\EasySDK\Marketing\TemplateMessage\Models\AlipayOpenAppMiniTemplatemessageSendResponse;
use AlibabaCloud\Tea\Response;
class Client {
protected $_kernel;
public function __construct($kernel){
$this->_kernel = $kernel;
}
public function send($toUserId, $formId, $userTemplateId, $page, $data){
$_runtime = [
"ignoreSSL" => $this->_kernel->getConfig("ignoreSSL"),
"httpProxy" => $this->_kernel->getConfig("httpProxy"),
"connectTimeout" => 15000,
"readTimeout" => 15000,
"retry" => [
"maxAttempts" => 0
]
];
$_lastRequest = null;
$_lastException = null;
$_now = time();
$_retryTimes = 0;
while (Tea::allowRetry(@$_runtime["retry"], $_retryTimes, $_now)) {
if ($_retryTimes > 0) {
$_backoffTime = Tea::getBackoffTime(@$_runtime["backoff"], $_retryTimes);
if ($_backoffTime > 0) {
Tea::sleep($_backoffTime);
}
}
$_retryTimes = $_retryTimes + 1;
try {
$_request = new Request();
$systemParams = [
"method" => "alipay.open.app.mini.templatemessage.send",
"app_id" => $this->_kernel->getConfig("appId"),
"timestamp" => $this->_kernel->getTimestamp(),
"format" => "json",
"version" => "1.0",
"alipay_sdk" => $this->_kernel->getSdkVersion(),
"charset" => "UTF-8",
"sign_type" => $this->_kernel->getConfig("signType"),
"app_cert_sn" => $this->_kernel->getMerchantCertSN(),
"alipay_root_cert_sn" => $this->_kernel->getAlipayRootCertSN()
];
$bizParams = [
"to_user_id" => $toUserId,
"form_id" => $formId,
"user_template_id" => $userTemplateId,
"page" => $page,
"data" => $data
];
$textParams = [];
$_request->protocol = $this->_kernel->getConfig("protocol");
$_request->method = "POST";
$_request->pathname = "/gateway.do";
$_request->headers = [
"host" => $this->_kernel->getConfig("gatewayHost"),
"content-type" => "application/x-www-form-urlencoded;charset=utf-8"
];
$_request->query = $this->_kernel->sortMap(Tea::merge([
"sign" => $this->_kernel->sign($systemParams, $bizParams, $textParams, $this->_kernel->getConfig("merchantPrivateKey"))
], $systemParams, $textParams));
$_request->body = $this->_kernel->toUrlEncodedRequestBody($bizParams);
$_lastRequest = $_request;
$_response= Tea::send($_request, $_runtime);
$respMap = $this->_kernel->readAsJson($_response, "alipay.open.app.mini.templatemessage.send");
if ($this->_kernel->isCertMode()) {
if ($this->_kernel->verify($respMap, $this->_kernel->extractAlipayPublicKey($this->_kernel->getAlipayCertSN($respMap)))) {
return AlipayOpenAppMiniTemplatemessageSendResponse::fromMap($this->_kernel->toRespModel($respMap));
}
}
else {
if ($this->_kernel->verify($respMap, $this->_kernel->getConfig("alipayPublicKey"))) {
return AlipayOpenAppMiniTemplatemessageSendResponse::fromMap($this->_kernel->toRespModel($respMap));
}
}
throw new TeaError([
"message" => "验签失败,请检查支付宝公钥设置是否正确。"
]);
}
catch (Exception $e) {
if (!($e instanceof TeaError)) {
$e = new TeaError([], $e->getMessage(), $e->getCode(), $e);
}
if (Tea::isRetryable($e)) {
$_lastException = $e;
continue;
}
throw $e;
}
}
throw new TeaUnableRetryError($_lastRequest, $_lastException);
}
public function agent($appAuthToken)
{
$this->_kernel->injectTextParam("app_auth_token", $appAuthToken);
return $this;
}
public function auth($authToken)
{
$this->_kernel->injectTextParam("auth_token", $authToken);
return $this;
}
public function asyncNotify($url)
{
$this->_kernel->injectTextParam("notify_url", $url);
return $this;
}
public function route($testUrl)
{
$this->_kernel->injectTextParam("ws_service_url", $testUrl);
return $this;
}
public function optional($key, $value)
{
$this->_kernel->injectBizParam($key, $value);
return $this;
}
public function batchOptional($optionalArgs)
{
foreach ($optionalArgs as $key => $value) {
$this->_kernel->injectBizParam($key, $value);
}
return $this;
}
}