<?php
namespace Alipay\EasySDK\Marketing\Pass;
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\Pass\Models\AlipayPassTemplateAddResponse;
use AlibabaCloud\Tea\Response;
use Alipay\EasySDK\Marketing\Pass\Models\AlipayPassTemplateUpdateResponse;
use Alipay\EasySDK\Marketing\Pass\Models\AlipayPassInstanceAddResponse;
use Alipay\EasySDK\Marketing\Pass\Models\AlipayPassInstanceUpdateResponse;
class Client {
protected $_kernel;
public function __construct($kernel){
$this->_kernel = $kernel;
}
public function createTemplate($uniqueId, $tplContent){
$_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.pass.template.add",
"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 = [
"unique_id" => $uniqueId,
"tpl_content" => $tplContent
];
$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.pass.template.add");
if ($this->_kernel->isCertMode()) {
if ($this->_kernel->verify($respMap, $this->_kernel->extractAlipayPublicKey($this->_kernel->getAlipayCertSN($respMap)))) {
return AlipayPassTemplateAddResponse::fromMap($this->_kernel->toRespModel($respMap));
}
}
else {
if ($this->_kernel->verify($respMap, $this->_kernel->getConfig("alipayPublicKey"))) {
return AlipayPassTemplateAddResponse::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 updateTemplate($tplId, $tplContent){
$_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.pass.template.update",
"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 = [
"tpl_id" => $tplId,
"tpl_content" => $tplContent
];
$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.pass.template.update");
if ($this->_kernel->isCertMode()) {
if ($this->_kernel->verify($respMap, $this->_kernel->extractAlipayPublicKey($this->_kernel->getAlipayCertSN($respMap)))) {
return AlipayPassTemplateUpdateResponse::fromMap($this->_kernel->toRespModel($respMap));
}
}
else {
if ($this->_kernel->verify($respMap, $this->_kernel->getConfig("alipayPublicKey"))) {
return AlipayPassTemplateUpdateResponse::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 addInstance($tplId, $tplParams, $recognitionType, $recognitionInfo){
$_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.pass.instance.add",
"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 = [
"tpl_id" => $tplId,
"tpl_params" => $tplParams,
"recognition_type" => $recognitionType,
"recognition_info" => $recognitionInfo
];
$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.pass.instance.add");
if ($this->_kernel->isCertMode()) {
if ($this->_kernel->verify($respMap, $this->_kernel->extractAlipayPublicKey($this->_kernel->getAlipayCertSN($respMap)))) {
return AlipayPassInstanceAddResponse::fromMap($this->_kernel->toRespModel($respMap));
}
}
else {
if ($this->_kernel->verify($respMap, $this->_kernel->getConfig("alipayPublicKey"))) {
return AlipayPassInstanceAddResponse::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 updateInstance($serialNumber, $channelId, $tplParams, $status, $verifyCode, $verifyType){
$_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.pass.instance.update",
"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 = [
"serial_number" => $serialNumber,
"channel_id" => $channelId,
"tpl_params" => $tplParams,
"status" => $status,
"verify_code" => $verifyCode,
"verify_type" => $verifyType
];
$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.pass.instance.update");
if ($this->_kernel->isCertMode()) {
if ($this->_kernel->verify($respMap, $this->_kernel->extractAlipayPublicKey($this->_kernel->getAlipayCertSN($respMap)))) {
return AlipayPassInstanceUpdateResponse::fromMap($this->_kernel->toRespModel($respMap));
}
}
else {
if ($this->_kernel->verify($respMap, $this->_kernel->getConfig("alipayPublicKey"))) {
return AlipayPassInstanceUpdateResponse::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;
}
}