/vendor/omnipay/alipay/src/Requests/AbstractAopRequest.php

https://github.com/ecjia/ecjia-daojia · PHP · 548 lines · 286 code · 130 blank · 132 comment · 22 complexity · dca8ae73c87de4fb06be545e00ba3693 MD5 · raw file

  1. <?php
  2. namespace Omnipay\Alipay\Requests;
  3. use Omnipay\Alipay\Common\Signer;
  4. use Omnipay\Common\Exception\InvalidRequestException;
  5. use Omnipay\Common\Message\AbstractRequest;
  6. abstract class AbstractAopRequest extends AbstractRequest
  7. {
  8. protected $method;
  9. protected $privateKey;
  10. protected $encryptKey;
  11. protected $alipayPublicKey;
  12. protected $endpoint = 'https://openapi.alipay.com/gateway.do';
  13. protected $returnable = false;
  14. protected $notifiable = false;
  15. /**
  16. * Get the raw data array for this message. The format of this varies from gateway to
  17. * gateway, but will usually be either an associative array, or a SimpleXMLElement.
  18. *
  19. * @return mixed
  20. */
  21. public function getData()
  22. {
  23. $this->validateParams();
  24. $this->setDefaults();
  25. $this->convertToString();
  26. $data = $this->parameters->all();
  27. $data['method'] = $this->method;
  28. ksort($data);
  29. $data['sign'] = $this->sign($data, $this->getSignType());
  30. return $data;
  31. }
  32. public function validateParams()
  33. {
  34. $this->validate(
  35. 'app_id',
  36. 'format',
  37. 'charset',
  38. 'sign_type',
  39. 'timestamp',
  40. 'version',
  41. 'biz_content'
  42. );
  43. }
  44. protected function setDefaults()
  45. {
  46. if (! $this->getTimestamp()) {
  47. $this->setTimestamp(date('Y-m-d H:i:s'));
  48. }
  49. }
  50. /**
  51. * @return mixed
  52. */
  53. public function getTimestamp()
  54. {
  55. return $this->getParameter('timestamp');
  56. }
  57. /**
  58. * @param $value
  59. *
  60. * @return $this
  61. */
  62. public function setTimestamp($value)
  63. {
  64. return $this->setParameter('timestamp', $value);
  65. }
  66. protected function convertToString()
  67. {
  68. foreach ($this->parameters->all() as $key => $value) {
  69. if (is_array($value) || is_object($value)) {
  70. $this->parameters->set($key, json_encode($value));
  71. }
  72. }
  73. }
  74. protected function sign($params, $signType)
  75. {
  76. $signer = new Signer($params);
  77. $signer->setIgnores(['sign']);
  78. $signType = strtoupper($signType);
  79. if ($signType == 'RSA') {
  80. $sign = $signer->signWithRSA($this->getPrivateKey());
  81. } elseif ($signType == 'RSA2') {
  82. $sign = $signer->signWithRSA($this->getPrivateKey(), OPENSSL_ALGO_SHA256);
  83. } else {
  84. throw new InvalidRequestException('The signType is invalid');
  85. }
  86. return $sign;
  87. }
  88. /**
  89. * @return mixed
  90. */
  91. public function getPrivateKey()
  92. {
  93. return $this->privateKey;
  94. }
  95. /**
  96. * @param $value
  97. *
  98. * @return $this
  99. */
  100. public function setPrivateKey($value)
  101. {
  102. $this->privateKey = $value;
  103. return $this;
  104. }
  105. /**
  106. * @return mixed
  107. */
  108. public function getSignType()
  109. {
  110. return $this->getParameter('sign_type');
  111. }
  112. /**
  113. * @return mixed
  114. */
  115. public function getAlipayPublicKey()
  116. {
  117. return $this->alipayPublicKey;
  118. }
  119. /**
  120. * @param $value
  121. *
  122. * @return $this
  123. */
  124. public function setAlipayPublicKey($value)
  125. {
  126. $this->alipayPublicKey = $value;
  127. return $this;
  128. }
  129. public function sendData($data)
  130. {
  131. $url = $this->getRequestUrl($data);
  132. $body = $this->getRequestBody();
  133. $response = $this->httpClient->post($url)/**/
  134. ->setBody($body, 'application/x-www-form-urlencoded')/**/
  135. ->send()->getBody();
  136. $response = $this->decode($response);
  137. return $response;
  138. }
  139. /**
  140. * @param $data
  141. *
  142. * @return string
  143. */
  144. protected function getRequestUrl($data)
  145. {
  146. $queryParams = $data;
  147. unset($queryParams['biz_content']);
  148. ksort($queryParams);
  149. $url = sprintf('%s?%s', $this->getEndpoint(), http_build_query($queryParams));
  150. return $url;
  151. }
  152. /**
  153. * @return mixed
  154. */
  155. public function getEndpoint()
  156. {
  157. return $this->endpoint;
  158. }
  159. /**
  160. * @param $value
  161. *
  162. * @return $this
  163. */
  164. public function setEndpoint($value)
  165. {
  166. $this->endpoint = $value;
  167. return $this;
  168. }
  169. /**
  170. * @return string
  171. */
  172. protected function getRequestBody()
  173. {
  174. $params = [
  175. 'biz_content' => $this->getBizContent()
  176. ];
  177. $body = http_build_query($params);
  178. return $body;
  179. }
  180. /**
  181. * @return mixed
  182. */
  183. public function getBizContent()
  184. {
  185. return $this->getParameter('biz_content');
  186. }
  187. protected function decode($data)
  188. {
  189. return json_decode($data, true);
  190. }
  191. /**
  192. * @param null $key
  193. * @param null $default
  194. *
  195. * @return mixed
  196. */
  197. public function getBizData($key = null, $default = null)
  198. {
  199. if (is_string($this->getBizContent())) {
  200. $data = json_decode($this->getBizContent(), true);
  201. } else {
  202. $data = $this->getBizContent();
  203. }
  204. if (is_null($key)) {
  205. return $data;
  206. } else {
  207. return array_get($data, $key, $default);
  208. }
  209. }
  210. /**
  211. * @return mixed
  212. */
  213. public function getAppId()
  214. {
  215. return $this->getParameter('app_id');
  216. }
  217. /**
  218. * @param $value
  219. *
  220. * @return $this
  221. */
  222. public function setAppId($value)
  223. {
  224. return $this->setParameter('app_id', $value);
  225. }
  226. /**
  227. * @return mixed
  228. */
  229. public function getFormat()
  230. {
  231. return $this->getParameter('format');
  232. }
  233. /**
  234. * @param $value
  235. *
  236. * @return $this
  237. */
  238. public function setFormat($value)
  239. {
  240. return $this->setParameter('format', $value);
  241. }
  242. /**
  243. * @return mixed
  244. */
  245. public function getCharset()
  246. {
  247. return $this->getParameter('charset');
  248. }
  249. /**
  250. * @param $value
  251. *
  252. * @return $this
  253. */
  254. public function setCharset($value)
  255. {
  256. return $this->setParameter('charset', $value);
  257. }
  258. /**
  259. * @param $value
  260. *
  261. * @return $this
  262. */
  263. public function setSignType($value)
  264. {
  265. return $this->setParameter('sign_type', $value);
  266. }
  267. /**
  268. * @param $value
  269. *
  270. * @return $this
  271. */
  272. public function setBizContent($value)
  273. {
  274. return $this->setParameter('biz_content', $value);
  275. }
  276. /**
  277. * @return mixed
  278. */
  279. public function getAlipaySdk()
  280. {
  281. return $this->getParameter('alipay_sdk');
  282. }
  283. /**
  284. * @param $value
  285. *
  286. * @return $this
  287. */
  288. public function setAlipaySdk($value)
  289. {
  290. return $this->setParameter('alipay_sdk', $value);
  291. }
  292. /**
  293. * @return mixed
  294. */
  295. public function getNotifyUrl()
  296. {
  297. return $this->getParameter('notify_url');
  298. }
  299. /**
  300. * @param $value
  301. *
  302. * @return $this
  303. */
  304. public function setNotifyUrl($value)
  305. {
  306. return $this->setParameter('notify_url', $value);
  307. }
  308. /**
  309. * @return mixed
  310. */
  311. public function getReturnUrl()
  312. {
  313. return $this->getParameter('return_url');
  314. }
  315. /**
  316. * @param $value
  317. *
  318. * @return $this
  319. */
  320. public function setReturnUrl($value)
  321. {
  322. return $this->setParameter('return_url', $value);
  323. }
  324. /**
  325. * @return mixed
  326. */
  327. public function getEncryptKey()
  328. {
  329. return $this->encryptKey;
  330. }
  331. /**
  332. * @param $value
  333. *
  334. * @return $this
  335. */
  336. public function setEncryptKey($value)
  337. {
  338. $this->encryptKey = $value;
  339. return $this;
  340. }
  341. /**
  342. * @return mixed
  343. */
  344. public function getVersion()
  345. {
  346. return $this->getParameter('version');
  347. }
  348. /**
  349. * @param $value
  350. *
  351. * @return $this
  352. */
  353. public function setVersion($value)
  354. {
  355. return $this->setParameter('version', $value);
  356. }
  357. public function validateBizContent()
  358. {
  359. $data = $this->getBizContent();
  360. if (is_string($data)) {
  361. $data = json_decode($data, true);
  362. }
  363. foreach (func_get_args() as $key) {
  364. if (! array_has($data, $key)) {
  365. throw new InvalidRequestException("The biz_content $key parameter is required");
  366. }
  367. }
  368. }
  369. public function validateBizContentOne()
  370. {
  371. $data = $this->getBizContent();
  372. if (is_string($data)) {
  373. $data = json_decode($data, true);
  374. }
  375. $keys = func_get_args();
  376. $allEmpty = true;
  377. foreach ($keys as $key) {
  378. if (array_has($data, $key)) {
  379. $allEmpty = false;
  380. break;
  381. }
  382. }
  383. if ($allEmpty) {
  384. throw new InvalidRequestException(
  385. sprintf('The biz_content (%s) parameter must provide one at least', implode(',', $keys))
  386. );
  387. }
  388. }
  389. protected function filter($data)
  390. {
  391. if (! $this->returnable) {
  392. unset($data['return_url']);
  393. }
  394. if (! $this->notifiable) {
  395. unset($data['notify_url']);
  396. }
  397. }
  398. protected function validateOne()
  399. {
  400. $keys = func_get_args();
  401. if ($keys && is_array($keys[0])) {
  402. $keys = $keys[0];
  403. }
  404. $allEmpty = true;
  405. foreach ($keys as $key) {
  406. $value = $this->parameters->get($key);
  407. if (! empty($value)) {
  408. $allEmpty = false;
  409. break;
  410. }
  411. }
  412. if ($allEmpty) {
  413. throw new InvalidRequestException(
  414. sprintf('The parameters (%s) must provide one at least', implode(',', $keys))
  415. );
  416. }
  417. }
  418. }