| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace common\expand\alipay;
- use addons\AlipayMchCoupon\common\models\AddonsAlipayMchAlipayNoticeModel;
- use AlibabaCloud\Tea\Exception\TeaError;
- use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
- use AlibabaCloud\Tea\FileForm\FileForm;
- use AlibabaCloud\Tea\FileForm\FileForm\FileField;
- use AlibabaCloud\Tea\Request;
- use AlibabaCloud\Tea\Tea;
- use Alipay\EasySDK\Kernel\EasySDKKernel;
- use GuzzleHttp\Psr7\Stream;
- class NoticeClient
- {
- /**
- * @var EasySDKKernel
- */
- protected $_kernel;
- public function __construct(EasySDKKernel $kernel){
- $this->_kernel = $kernel;
- }
- /**
- * 上传商品图片
- * @param string $method
- * @param string $src
- * @param string $scene
- * @return mixed
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function alipayMerchantItemFileUpload(string $method, string $src, string $scene = 'SYNC_ORDER')
- {
- $_runtime = [
- "connectTimeout" => 100000,
- "readTimeout" => 100000,
- "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" => $method,
- "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 = [
- ];
- $textParams = [
- 'scene' => $scene,
- ];
- $fileParams = [
- "file_content" => $src
- ];
- $boundary = $this->_kernel->getRandomBoundary();
- $_request->protocol = $this->_kernel->getConfig("protocol");
- $_request->method = "POST";
- $_request->pathname = "/gateway.do";
- $_request->headers = [
- "host" => $this->_kernel->getConfig("gatewayHost"),
- "content-type" => $this->_kernel->concatStr("multipart/form-data;charset=utf-8;boundary=", $boundary)
- ];
- $_request->query = $this->_kernel->sortMap(Tea::merge([
- "sign" => $this->_kernel->sign($systemParams, $bizParams, $textParams, $this->_kernel->getConfig("merchantPrivateKey"))
- ], $systemParams));
- // 给_kernel对象绑定一个闭包
- $call = function ($textParams, $fileParams, $boundary) {
- $this->addOtherParams($textParams, null);
- $fileField = new FileField();
- $fileField->filename = $fileParams['file_content'];
- $fileField->contentType = 'multipart/form-data;charset=utf-8;boundary=' . $boundary;
- $fileField->content = new Stream(fopen($fileParams['file_content'], 'r'));
- $map = [
- 'scene' => $this->textParams['scene'],
- 'file_content' => $fileField
- ];
- $stream = FileForm::toFileForm($map, $boundary);
- do {
- $readLength = $stream->read(1024);
- } while (0 != $readLength);
- return $stream;
- };
- $_request->body = $call->call($this->_kernel, $textParams, $fileParams, $boundary);
- $_lastRequest = $_request;
- $_response= Tea::send($_request, $_runtime);
- $respMap = $this->_kernel->readAsJson($_response, $method);
- AddonsAlipayMchAlipayNoticeModel::create(0, $method, [
- 'cat' => 'alipay_client_open_upload',
- 'method' => $method,
- 'res' => $respMap,
- 'params' => $bizParams,
- 'textParams'=> $textParams
- ]);
- if ($this->_kernel->isCertMode()) {
- if ($this->_kernel->verify($respMap, $this->_kernel->extractAlipayPublicKey($this->_kernel->getAlipayCertSN($respMap)))) {
- return $this->_kernel->toRespModel($respMap);
- }
- }
- else {
- if ($this->_kernel->verify($respMap, $this->_kernel->getConfig("alipayPublicKey"))) {
- return $this->_kernel->toRespModel($respMap);
- }
- }
- throw new TeaError([
- "message" => "验签失败,请检查支付宝公钥设置是否正确。"
- ]);
- }
- catch (\Exception $e) {
- if (Tea::isRetryable($e)) {
- $_lastException = $e;
- continue;
- }
- throw $e;
- }
- }
- throw new TeaUnableRetryError($_lastRequest, $_lastException);
- }
- }
|