NoticeClient.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace common\expand\alipay;
  3. use addons\AlipayMchCoupon\common\models\AddonsAlipayMchAlipayNoticeModel;
  4. use AlibabaCloud\Tea\Exception\TeaError;
  5. use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
  6. use AlibabaCloud\Tea\FileForm\FileForm;
  7. use AlibabaCloud\Tea\FileForm\FileForm\FileField;
  8. use AlibabaCloud\Tea\Request;
  9. use AlibabaCloud\Tea\Tea;
  10. use Alipay\EasySDK\Kernel\EasySDKKernel;
  11. use GuzzleHttp\Psr7\Stream;
  12. class NoticeClient
  13. {
  14. /**
  15. * @var EasySDKKernel
  16. */
  17. protected $_kernel;
  18. public function __construct(EasySDKKernel $kernel){
  19. $this->_kernel = $kernel;
  20. }
  21. /**
  22. * 上传商品图片
  23. * @param string $method
  24. * @param string $src
  25. * @param string $scene
  26. * @return mixed
  27. * @throws \GuzzleHttp\Exception\GuzzleException
  28. */
  29. public function alipayMerchantItemFileUpload(string $method, string $src, string $scene = 'SYNC_ORDER')
  30. {
  31. $_runtime = [
  32. "connectTimeout" => 100000,
  33. "readTimeout" => 100000,
  34. "retry" => [
  35. "maxAttempts" => 0
  36. ]
  37. ];
  38. $_lastRequest = null;
  39. $_lastException = null;
  40. $_now = time();
  41. $_retryTimes = 0;
  42. while (Tea::allowRetry($_runtime["retry"], $_retryTimes, $_now)) {
  43. if ($_retryTimes > 0) {
  44. $_backoffTime = Tea::getBackoffTime($_runtime["backoff"], $_retryTimes);
  45. if ($_backoffTime > 0) {
  46. Tea::sleep($_backoffTime);
  47. }
  48. }
  49. $_retryTimes = $_retryTimes + 1;
  50. try {
  51. $_request = new Request();
  52. $systemParams = [
  53. "method" => $method,
  54. "app_id" => $this->_kernel->getConfig("appId"),
  55. "timestamp" => $this->_kernel->getTimestamp(),
  56. "format" => "json",
  57. "version" => "1.0",
  58. "alipay_sdk" => $this->_kernel->getSdkVersion(),
  59. "charset" => "UTF-8",
  60. "sign_type" => $this->_kernel->getConfig("signType"),
  61. "app_cert_sn" => $this->_kernel->getMerchantCertSN(),
  62. "alipay_root_cert_sn" => $this->_kernel->getAlipayRootCertSN()
  63. ];
  64. $bizParams = [
  65. ];
  66. $textParams = [
  67. 'scene' => $scene,
  68. ];
  69. $fileParams = [
  70. "file_content" => $src
  71. ];
  72. $boundary = $this->_kernel->getRandomBoundary();
  73. $_request->protocol = $this->_kernel->getConfig("protocol");
  74. $_request->method = "POST";
  75. $_request->pathname = "/gateway.do";
  76. $_request->headers = [
  77. "host" => $this->_kernel->getConfig("gatewayHost"),
  78. "content-type" => $this->_kernel->concatStr("multipart/form-data;charset=utf-8;boundary=", $boundary)
  79. ];
  80. $_request->query = $this->_kernel->sortMap(Tea::merge([
  81. "sign" => $this->_kernel->sign($systemParams, $bizParams, $textParams, $this->_kernel->getConfig("merchantPrivateKey"))
  82. ], $systemParams));
  83. // 给_kernel对象绑定一个闭包
  84. $call = function ($textParams, $fileParams, $boundary) {
  85. $this->addOtherParams($textParams, null);
  86. $fileField = new FileField();
  87. $fileField->filename = $fileParams['file_content'];
  88. $fileField->contentType = 'multipart/form-data;charset=utf-8;boundary=' . $boundary;
  89. $fileField->content = new Stream(fopen($fileParams['file_content'], 'r'));
  90. $map = [
  91. 'scene' => $this->textParams['scene'],
  92. 'file_content' => $fileField
  93. ];
  94. $stream = FileForm::toFileForm($map, $boundary);
  95. do {
  96. $readLength = $stream->read(1024);
  97. } while (0 != $readLength);
  98. return $stream;
  99. };
  100. $_request->body = $call->call($this->_kernel, $textParams, $fileParams, $boundary);
  101. $_lastRequest = $_request;
  102. $_response= Tea::send($_request, $_runtime);
  103. $respMap = $this->_kernel->readAsJson($_response, $method);
  104. AddonsAlipayMchAlipayNoticeModel::create(0, $method, [
  105. 'cat' => 'alipay_client_open_upload',
  106. 'method' => $method,
  107. 'res' => $respMap,
  108. 'params' => $bizParams,
  109. 'textParams'=> $textParams
  110. ]);
  111. if ($this->_kernel->isCertMode()) {
  112. if ($this->_kernel->verify($respMap, $this->_kernel->extractAlipayPublicKey($this->_kernel->getAlipayCertSN($respMap)))) {
  113. return $this->_kernel->toRespModel($respMap);
  114. }
  115. }
  116. else {
  117. if ($this->_kernel->verify($respMap, $this->_kernel->getConfig("alipayPublicKey"))) {
  118. return $this->_kernel->toRespModel($respMap);
  119. }
  120. }
  121. throw new TeaError([
  122. "message" => "验签失败,请检查支付宝公钥设置是否正确。"
  123. ]);
  124. }
  125. catch (\Exception $e) {
  126. if (Tea::isRetryable($e)) {
  127. $_lastException = $e;
  128. continue;
  129. }
  130. throw $e;
  131. }
  132. }
  133. throw new TeaUnableRetryError($_lastRequest, $_lastException);
  134. }
  135. }