Support.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. $result = self::$instance->post(
  140. $endpoint,
  141. self::toXml($data),
  142. $cert ? [
  143. 'cert' => self::$instance->cert_client,
  144. 'ssl_key' => self::$instance->cert_key,
  145. ] : [
  146. 'headers' => [
  147. 'Content-Type'=>'application/xml',
  148. ],
  149. ]
  150. );
  151. $result = is_array($result) ? $result : self::fromXml($result);
  152. Events::dispatch(new Events\ApiRequested('Wechat', '', self::$instance->getBaseUri().$endpoint, $result));
  153. return self::processingApiResult($endpoint, $result);
  154. }
  155. /**
  156. * Filter payload.
  157. *
  158. * @author yansongda <me@yansongda.cn>
  159. *
  160. * @param array $payload
  161. * @param array|string $params
  162. * @param bool $preserve_notify_url
  163. *
  164. * @throws InvalidArgumentException
  165. */
  166. public static function filterPayload($payload, $params, $preserve_notify_url = false): array
  167. {
  168. $type = self::getTypeName($params['type'] ?? '');
  169. $payload = array_merge(
  170. $payload,
  171. is_array($params) ? $params : ['out_trade_no' => $params]
  172. );
  173. $payload['appid'] = self::$instance->getConfig($type, '');
  174. if (Wechat::MODE_SERVICE === self::$instance->getConfig('mode', Wechat::MODE_NORMAL)) {
  175. $payload['sub_appid'] = self::$instance->getConfig('sub_'.$type, '');
  176. }
  177. unset($payload['trade_type'], $payload['type']);
  178. if (!$preserve_notify_url) {
  179. unset($payload['notify_url']);
  180. }
  181. $payload['sign'] = self::generateSign($payload);
  182. return $payload;
  183. }
  184. /**
  185. * Generate wechat sign.
  186. *
  187. * @author yansongda <me@yansongda.cn>
  188. *
  189. * @param array $data
  190. *
  191. * @throws InvalidArgumentException
  192. */
  193. public static function generateSign($data): string
  194. {
  195. $key = self::$instance->key;
  196. if (is_null($key)) {
  197. throw new InvalidArgumentException('Missing Wechat Config -- [key]');
  198. }
  199. ksort($data);
  200. $string = md5(self::getSignContent($data).'&key='.$key);
  201. Log::debug('Wechat Generate Sign Before UPPER', [$data, $string]);
  202. return strtoupper($string);
  203. }
  204. /**
  205. * Generate sign content.
  206. *
  207. * @author yansongda <me@yansongda.cn>
  208. *
  209. * @param array $data
  210. */
  211. public static function getSignContent($data): string
  212. {
  213. $buff = '';
  214. foreach ($data as $k => $v) {
  215. $buff .= ('sign' != $k && '' != $v && !is_array($v)) ? $k.'='.$v.'&' : '';
  216. }
  217. Log::debug('Wechat Generate Sign Content Before Trim', [$data, $buff]);
  218. return trim($buff, '&');
  219. }
  220. /**
  221. * Decrypt refund contents.
  222. *
  223. * @author yansongda <me@yansongda.cn>
  224. *
  225. * @param string $contents
  226. */
  227. public static function decryptRefundContents($contents): string
  228. {
  229. return openssl_decrypt(
  230. base64_decode($contents),
  231. 'AES-256-ECB',
  232. md5(self::$instance->key),
  233. OPENSSL_RAW_DATA
  234. );
  235. }
  236. /**
  237. * Convert array to xml.
  238. *
  239. * @author yansongda <me@yansongda.cn>
  240. *
  241. * @param array $data
  242. *
  243. * @throws InvalidArgumentException
  244. */
  245. public static function toXml($data): string
  246. {
  247. if (!is_array($data) || count($data) <= 0) {
  248. throw new InvalidArgumentException('Convert To Xml Error! Invalid Array!');
  249. }
  250. $xml = '<xml>';
  251. foreach ($data as $key => $val) {
  252. $xml .= is_numeric($val) ? '<'.$key.'>'.$val.'</'.$key.'>' :
  253. '<'.$key.'><![CDATA['.$val.']]></'.$key.'>';
  254. }
  255. $xml .= '</xml>';
  256. return $xml;
  257. }
  258. /**
  259. * Convert xml to array.
  260. *
  261. * @author yansongda <me@yansongda.cn>
  262. *
  263. * @param string $xml
  264. *
  265. * @throws InvalidArgumentException
  266. */
  267. public static function fromXml($xml): array
  268. {
  269. if (!$xml) {
  270. throw new InvalidArgumentException('Convert To Array Error! Invalid Xml!');
  271. }
  272. libxml_disable_entity_loader(true);
  273. return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA), JSON_UNESCAPED_UNICODE), true);
  274. }
  275. /**
  276. * Get service config.
  277. *
  278. * @author yansongda <me@yansongda.cn>
  279. *
  280. * @param string|null $key
  281. * @param mixed|null $default
  282. *
  283. * @return mixed|null
  284. */
  285. public function getConfig($key = null, $default = null)
  286. {
  287. if (is_null($key)) {
  288. return $this->config->all();
  289. }
  290. if ($this->config->has($key)) {
  291. return $this->config[$key];
  292. }
  293. return $default;
  294. }
  295. /**
  296. * Get app id according to param type.
  297. *
  298. * @author yansongda <me@yansongda.cn>
  299. *
  300. * @param string $type
  301. */
  302. public static function getTypeName($type = ''): string
  303. {
  304. switch ($type) {
  305. case '':
  306. $type = 'app_id';
  307. break;
  308. case 'app':
  309. $type = 'appid';
  310. break;
  311. default:
  312. $type = $type.'_id';
  313. }
  314. return $type;
  315. }
  316. /**
  317. * Get Base Uri.
  318. *
  319. * @author yansongda <me@yansongda.cn>
  320. *
  321. * @return string
  322. */
  323. public function getBaseUri()
  324. {
  325. return $this->baseUri;
  326. }
  327. /**
  328. * processingApiResult.
  329. *
  330. * @author yansongda <me@yansongda.cn>
  331. *
  332. * @param $endpoint
  333. *
  334. * @throws GatewayException
  335. * @throws InvalidArgumentException
  336. * @throws InvalidSignException
  337. *
  338. * @return Collection
  339. */
  340. protected static function processingApiResult($endpoint, array $result)
  341. {
  342. if (!isset($result['return_code']) || 'SUCCESS' != $result['return_code']) {
  343. throw new GatewayException('Get Wechat API Error:'.($result['return_msg'] ?? $result['retmsg'] ?? ''), $result);
  344. }
  345. if (isset($result['result_code']) && 'SUCCESS' != $result['result_code']) {
  346. throw new BusinessException('Wechat Business Error: '.$result['err_code'].' - '.$result['err_code_des'], $result);
  347. }
  348. if (false !== strpos($endpoint, 'xdc/apiv2getsignkey') ||
  349. false !== strpos($endpoint, 'mmpaymkttransfers') ||
  350. self::generateSign($result) === $result['sign']) {
  351. return new Collection($result);
  352. }
  353. Events::dispatch(new Events\SignFailed('Wechat', '', $result));
  354. throw new InvalidSignException('Wechat Sign Verify FAILED', $result);
  355. }
  356. /**
  357. * setDevKey.
  358. *
  359. * @author yansongda <me@yansongda.cn>
  360. *
  361. * @throws GatewayException
  362. * @throws InvalidArgumentException
  363. * @throws InvalidSignException
  364. * @throws Exception
  365. *
  366. * @return Support
  367. */
  368. private static function setDevKey()
  369. {
  370. if (Wechat::MODE_DEV == self::$instance->mode) {
  371. $data = [
  372. 'mch_id' => self::$instance->mch_id,
  373. 'nonce_str' => Str::random(),
  374. ];
  375. $data['sign'] = self::generateSign($data);
  376. $result = self::requestApi('https://api.mch.weixin.qq.com/xdc/apiv2getsignkey/sign/getsignkey', $data);
  377. self::$instance->config->set('key', $result['sandbox_signkey']);
  378. }
  379. return self::$instance;
  380. }
  381. /**
  382. * Set Http options.
  383. *
  384. * @author yansongda <me@yansongda.cn>
  385. */
  386. private function setHttpOptions(): self
  387. {
  388. if ($this->config->has('http') && is_array($this->config->get('http'))) {
  389. $this->config->forget('http.base_uri');
  390. $this->httpOptions = $this->config->get('http');
  391. }
  392. return $this;
  393. }
  394. }