Support.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. namespace Yansongda\Pay\Gateways\Wechat;
  3. use Exception;
  4. use Yansongda\Pay\Events;
  5. use Yansongda\Pay\Exceptions\BusinessException;
  6. use Yansongda\Pay\Exceptions\GatewayException;
  7. use Yansongda\Pay\Exceptions\InvalidArgumentException;
  8. use Yansongda\Pay\Exceptions\InvalidSignException;
  9. use Yansongda\Pay\Gateways\Wechat;
  10. use Yansongda\Pay\Log;
  11. use Yansongda\Supports\Collection;
  12. use Yansongda\Supports\Config;
  13. use Yansongda\Supports\Str;
  14. use Yansongda\Supports\Traits\HasHttpRequest;
  15. /**
  16. * @author yansongda <me@yansongda.cn>
  17. *
  18. * @property string appid
  19. * @property string app_id
  20. * @property string miniapp_id
  21. * @property string sub_appid
  22. * @property string sub_app_id
  23. * @property string sub_miniapp_id
  24. * @property string mch_id
  25. * @property string sub_mch_id
  26. * @property string key
  27. * @property string return_url
  28. * @property string cert_client
  29. * @property string cert_key
  30. * @property array log
  31. * @property array http
  32. * @property string mode
  33. */
  34. class Support
  35. {
  36. use HasHttpRequest;
  37. /**
  38. * Wechat gateway.
  39. *
  40. * @var string
  41. */
  42. protected $baseUri;
  43. /**
  44. * Config.
  45. *
  46. * @var Config
  47. */
  48. protected $config;
  49. /**
  50. * Instance.
  51. *
  52. * @var Support
  53. */
  54. private static $instance;
  55. /**
  56. * Bootstrap.
  57. *
  58. * @author yansongda <me@yansongda.cn>
  59. */
  60. private function __construct(Config $config)
  61. {
  62. $this->baseUri = Wechat::URL[$config->get('mode', Wechat::MODE_NORMAL)];
  63. $this->config = $config;
  64. $this->setHttpOptions();
  65. }
  66. /**
  67. * __get.
  68. *
  69. * @author yansongda <me@yansongda.cn>
  70. *
  71. * @param $key
  72. *
  73. * @return mixed|Config|null
  74. */
  75. public function __get($key)
  76. {
  77. return $this->getConfig($key);
  78. }
  79. /**
  80. * create.
  81. *
  82. * @author yansongda <me@yansongda.cn>
  83. *
  84. * @throws GatewayException
  85. * @throws InvalidArgumentException
  86. * @throws InvalidSignException
  87. *
  88. * @return Support
  89. */
  90. public static function create(Config $config)
  91. {
  92. if ('cli' === php_sapi_name() || !(self::$instance instanceof self)) {
  93. self::$instance = new self($config);
  94. self::setDevKey();
  95. }
  96. return self::$instance;
  97. }
  98. /**
  99. * getInstance.
  100. *
  101. * @author yansongda <me@yansongda.cn>
  102. *
  103. * @throws InvalidArgumentException
  104. *
  105. * @return Support
  106. */
  107. public static function getInstance()
  108. {
  109. if (is_null(self::$instance)) {
  110. throw new InvalidArgumentException('You Should [Create] First Before Using');
  111. }
  112. return self::$instance;
  113. }
  114. /**
  115. * clear.
  116. *
  117. * @author yansongda <me@yansongda.cn>
  118. */
  119. public static function clear()
  120. {
  121. self::$instance = null;
  122. }
  123. /**
  124. * Request wechat api.
  125. *
  126. * @author yansongda <me@yansongda.cn>
  127. *
  128. * @param string $endpoint
  129. * @param array $data
  130. * @param bool $cert
  131. *
  132. * @throws GatewayException
  133. * @throws InvalidArgumentException
  134. * @throws InvalidSignException
  135. */
  136. public static function requestApi($endpoint, $data, $cert = false): Collection
  137. {
  138. Events::dispatch(new Events\ApiRequesting('Wechat', '', self::$instance->getBaseUri().$endpoint, $data));
  139. //xmlData需增加headers配置,否则微信方无法接收到参数
  140. $options = [
  141. 'headers' => [
  142. 'Content-Type' => 'application/xml',
  143. ],
  144. ];
  145. $certOptions = $cert ? [
  146. 'cert' => self::$instance->cert_client,
  147. 'ssl_key' => self::$instance->cert_key,
  148. ] : [];
  149. $result = self::$instance->post(
  150. $endpoint,
  151. self::toXml($data),
  152. array_merge($options, $certOptions)
  153. );
  154. $result = is_array($result) ? $result : self::fromXml($result);
  155. Events::dispatch(new Events\ApiRequested('Wechat', '', self::$instance->getBaseUri().$endpoint, $result));
  156. return self::processingApiResult($endpoint, $result);
  157. }
  158. /**
  159. * Filter payload.
  160. *
  161. * @author yansongda <me@yansongda.cn>
  162. *
  163. * @param array $payload
  164. * @param array|string $params
  165. * @param bool $preserve_notify_url
  166. *
  167. * @throws InvalidArgumentException
  168. */
  169. public static function filterPayload($payload, $params, $preserve_notify_url = false): array
  170. {
  171. $type = self::getTypeName($params['type'] ?? '');
  172. $payload = array_merge(
  173. $payload,
  174. is_array($params) ? $params : ['out_trade_no' => $params]
  175. );
  176. $payload['appid'] = self::$instance->getConfig($type, '');
  177. if (Wechat::MODE_SERVICE === self::$instance->getConfig('mode', Wechat::MODE_NORMAL)) {
  178. $payload['sub_appid'] = self::$instance->getConfig('sub_'.$type, '');
  179. }
  180. unset($payload['trade_type'], $payload['type']);
  181. if (!$preserve_notify_url) {
  182. unset($payload['notify_url']);
  183. }
  184. $payload['sign'] = self::generateSign($payload);
  185. return $payload;
  186. }
  187. /**
  188. * Generate wechat sign.
  189. *
  190. * @author yansongda <me@yansongda.cn>
  191. *
  192. * @param array $data
  193. *
  194. * @throws InvalidArgumentException
  195. */
  196. public static function generateSign($data): string
  197. {
  198. $key = self::$instance->key;
  199. if (is_null($key)) {
  200. throw new InvalidArgumentException('Missing Wechat Config -- [key]');
  201. }
  202. ksort($data);
  203. $string = md5(self::getSignContent($data).'&key='.$key);
  204. Log::debug('Wechat Generate Sign Before UPPER', [$data, $string]);
  205. return strtoupper($string);
  206. }
  207. /**
  208. * Generate sign content.
  209. *
  210. * @author yansongda <me@yansongda.cn>
  211. *
  212. * @param array $data
  213. */
  214. public static function getSignContent($data): string
  215. {
  216. $buff = '';
  217. foreach ($data as $k => $v) {
  218. $buff .= ('sign' != $k && '' != $v && !is_array($v)) ? $k.'='.$v.'&' : '';
  219. }
  220. Log::debug('Wechat Generate Sign Content Before Trim', [$data, $buff]);
  221. return trim($buff, '&');
  222. }
  223. /**
  224. * Decrypt refund contents.
  225. *
  226. * @author yansongda <me@yansongda.cn>
  227. *
  228. * @param string $contents
  229. */
  230. public static function decryptRefundContents($contents): string
  231. {
  232. return openssl_decrypt(
  233. base64_decode($contents),
  234. 'AES-256-ECB',
  235. md5(self::$instance->key),
  236. OPENSSL_RAW_DATA
  237. );
  238. }
  239. /**
  240. * Convert array to xml.
  241. *
  242. * @author yansongda <me@yansongda.cn>
  243. *
  244. * @param array $data
  245. *
  246. * @throws InvalidArgumentException
  247. */
  248. public static function toXml($data): string
  249. {
  250. if (!is_array($data) || count($data) <= 0) {
  251. throw new InvalidArgumentException('Convert To Xml Error! Invalid Array!');
  252. }
  253. $xml = '<xml>';
  254. foreach ($data as $key => $val) {
  255. $xml .= is_numeric($val) ? '<'.$key.'>'.$val.'</'.$key.'>' :
  256. '<'.$key.'><![CDATA['.$val.']]></'.$key.'>';
  257. }
  258. $xml .= '</xml>';
  259. return $xml;
  260. }
  261. /**
  262. * Convert xml to array.
  263. *
  264. * @author yansongda <me@yansongda.cn>
  265. *
  266. * @param string $xml
  267. *
  268. * @throws InvalidArgumentException
  269. */
  270. public static function fromXml($xml): array
  271. {
  272. if (!$xml) {
  273. throw new InvalidArgumentException('Convert To Array Error! Invalid Xml!');
  274. }
  275. if (PHP_VERSION_ID < 80000) {
  276. libxml_disable_entity_loader(true);
  277. }
  278. return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA), JSON_UNESCAPED_UNICODE), true);
  279. }
  280. /**
  281. * Get service config.
  282. *
  283. * @author yansongda <me@yansongda.cn>
  284. *
  285. * @param string|null $key
  286. * @param mixed|null $default
  287. *
  288. * @return mixed|null
  289. */
  290. public function getConfig($key = null, $default = null)
  291. {
  292. if (is_null($key)) {
  293. return $this->config->all();
  294. }
  295. if ($this->config->has($key)) {
  296. return $this->config[$key];
  297. }
  298. return $default;
  299. }
  300. /**
  301. * Get app id according to param type.
  302. *
  303. * @author yansongda <me@yansongda.cn>
  304. *
  305. * @param string $type
  306. */
  307. public static function getTypeName($type = ''): string
  308. {
  309. switch ($type) {
  310. case '':
  311. $type = 'app_id';
  312. break;
  313. case 'app':
  314. $type = 'appid';
  315. break;
  316. default:
  317. $type = $type.'_id';
  318. }
  319. return $type;
  320. }
  321. /**
  322. * Get Base Uri.
  323. *
  324. * @author yansongda <me@yansongda.cn>
  325. *
  326. * @return string
  327. */
  328. public function getBaseUri()
  329. {
  330. return $this->baseUri;
  331. }
  332. /**
  333. * processingApiResult.
  334. *
  335. * @author yansongda <me@yansongda.cn>
  336. *
  337. * @param $endpoint
  338. *
  339. * @throws GatewayException
  340. * @throws InvalidArgumentException
  341. * @throws InvalidSignException
  342. *
  343. * @return Collection
  344. */
  345. protected static function processingApiResult($endpoint, array $result)
  346. {
  347. if (!isset($result['return_code']) || 'SUCCESS' != $result['return_code']) {
  348. throw new GatewayException('Get Wechat API Error:'.($result['return_msg'] ?? $result['retmsg'] ?? ''), $result);
  349. }
  350. if (isset($result['result_code']) && 'SUCCESS' != $result['result_code']) {
  351. throw new BusinessException('Wechat Business Error: '.$result['err_code'].' - '.$result['err_code_des'], $result);
  352. }
  353. if (false !== strpos($endpoint, 'xdc/apiv2getsignkey') ||
  354. false !== strpos($endpoint, 'mmpaymkttransfers') ||
  355. self::generateSign($result) === $result['sign']) {
  356. return new Collection($result);
  357. }
  358. Events::dispatch(new Events\SignFailed('Wechat', '', $result));
  359. throw new InvalidSignException('Wechat Sign Verify FAILED', $result);
  360. }
  361. /**
  362. * setDevKey.
  363. *
  364. * @author yansongda <me@yansongda.cn>
  365. *
  366. * @throws GatewayException
  367. * @throws InvalidArgumentException
  368. * @throws InvalidSignException
  369. * @throws Exception
  370. *
  371. * @return Support
  372. */
  373. private static function setDevKey()
  374. {
  375. if (Wechat::MODE_DEV == self::$instance->mode) {
  376. $data = [
  377. 'mch_id' => self::$instance->mch_id,
  378. 'nonce_str' => Str::random(),
  379. ];
  380. $data['sign'] = self::generateSign($data);
  381. $result = self::requestApi('https://api.mch.weixin.qq.com/xdc/apiv2getsignkey/sign/getsignkey', $data);
  382. self::$instance->config->set('key', $result['sandbox_signkey']);
  383. }
  384. return self::$instance;
  385. }
  386. /**
  387. * Set Http options.
  388. *
  389. * @author yansongda <me@yansongda.cn>
  390. */
  391. private function setHttpOptions(): self
  392. {
  393. if ($this->config->has('http') && is_array($this->config->get('http'))) {
  394. $this->config->forget('http.base_uri');
  395. $this->httpOptions = $this->config->get('http');
  396. }
  397. return $this;
  398. }
  399. }