Support.php 11 KB

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