ApiV3Config.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\common\services\wechatApiV3;
  3. class ApiV3Config
  4. {
  5. private $config;
  6. /**
  7. * @var ApiV3Request
  8. */
  9. private $request;
  10. /**
  11. * @var ApiV3Encrypt
  12. */
  13. private $encrypt;
  14. public function __construct($config)
  15. {
  16. $this->config = $config;
  17. }
  18. private function check($key)
  19. {
  20. if (!$this->config[$key]) {
  21. throw new \Exception($key.'配置错误');
  22. }
  23. }
  24. /**
  25. * @return mixed
  26. * @throws \Exception
  27. */
  28. public function appid()
  29. {
  30. $this->check('appid');
  31. return $this->config['appid'];
  32. }
  33. /**
  34. * @return mixed
  35. * @throws \Exception
  36. */
  37. public function secret()
  38. {
  39. $this->check('secret');
  40. return $this->config['secret'];
  41. }
  42. /**
  43. * apiV3
  44. * @return mixed
  45. * @throws \Exception
  46. */
  47. public function secretV3()
  48. {
  49. $this->check('secret_v3');
  50. return $this->config['secret_v3'];
  51. }
  52. /**
  53. * 商户号
  54. * @return mixed
  55. * @throws \Exception
  56. */
  57. public function mchId()
  58. {
  59. $this->check('mchid');
  60. return $this->config['mchid'];
  61. }
  62. /**
  63. * API证书
  64. * @return mixed
  65. * @throws \Exception
  66. */
  67. public function apiCertPem()
  68. {
  69. $this->check('api_cert_pem');
  70. return $this->config['api_cert_pem'];
  71. }
  72. /**
  73. * API key
  74. * @return mixed
  75. * @throws \Exception
  76. */
  77. public function apiKeyPem()
  78. {
  79. $this->check('api_key_pem');
  80. return $this->config['api_key_pem'];
  81. }
  82. /**
  83. * 平台证书
  84. * @return string
  85. * @throws \Exception
  86. */
  87. public function platformCert($is_new = 0)
  88. {
  89. $file = $this->platformCertPath() . $this->platformCertFileName();
  90. if (!file_exists($file) || $is_new) {
  91. $this->request()->platformCertApply($this->platformCertPath(),$this->platformCertFileName());
  92. }
  93. return $file;
  94. }
  95. /**
  96. * 平台证书序列号
  97. * @return false|resource
  98. * @throws \Exception
  99. */
  100. public function platformSerialNo()
  101. {
  102. $ctx = stream_context_create([
  103. "ssl"=>[
  104. "verify_peer"=>false,
  105. "verify_peer_name"=>false,
  106. ]
  107. ]);
  108. $resource = openssl_x509_read(file_get_contents($this->platformCert(),false,$ctx));
  109. if (!$resource) {
  110. throw new \Exception('平台证书读取失败,请检查证书有效性!');
  111. }
  112. $arr = openssl_x509_parse($resource);
  113. return $arr['serialNumberHex'];
  114. }
  115. /**
  116. * API证书序列号
  117. * @return false|resource
  118. * @throws \Exception
  119. */
  120. public function apiSerialNo()
  121. {
  122. $ctx = stream_context_create([
  123. "ssl"=>[
  124. "verify_peer"=>false,
  125. "verify_peer_name"=>false,
  126. ]
  127. ]);
  128. $resource = openssl_x509_read(file_get_contents($this->apiCertPem(),false,$ctx));
  129. if (!$resource) {
  130. throw new \Exception('API证书读取失败,请检查证书有效性!');
  131. }
  132. $arr = openssl_x509_parse($resource);
  133. return $arr['serialNumberHex'];
  134. }
  135. /**
  136. * @return resource
  137. * @throws \Exception
  138. */
  139. public function privateKey()
  140. {
  141. $ctx = stream_context_create([
  142. "ssl"=>[
  143. "verify_peer"=>false,
  144. "verify_peer_name"=>false,
  145. ]
  146. ]);
  147. $resource = openssl_get_privatekey(file_get_contents($this->apiKeyPem(),false,$ctx));
  148. if (!$resource) {
  149. throw new \Exception('API秘钥读取失败,请检查秘钥有效性!');
  150. }
  151. return $resource;
  152. }
  153. /**
  154. * 平台公钥
  155. * @return resource
  156. * @throws \Exception
  157. */
  158. public function platformCertKey()
  159. {
  160. $ctx = stream_context_create([
  161. "ssl"=>[
  162. "verify_peer"=>false,
  163. "verify_peer_name"=>false,
  164. ]
  165. ]);
  166. $resource = openssl_get_publickey(file_get_contents($this->platformCert(),false,$ctx));
  167. if (!$resource) {
  168. throw new \Exception('平台证书秘钥读取失败!');
  169. }
  170. return $resource;
  171. }
  172. /**
  173. * @return ApiV3Request
  174. */
  175. public function request():ApiV3Request
  176. {
  177. if (!isset($this->request)) {
  178. $this->request = new ApiV3Request($this);
  179. }
  180. return $this->request;
  181. }
  182. /**
  183. * @return ApiV3Encrypt
  184. */
  185. public function encrypt():ApiV3Encrypt
  186. {
  187. if (!isset($this->encrypt)) {
  188. $this->encrypt = new ApiV3Encrypt($this);
  189. }
  190. return $this->encrypt;
  191. }
  192. private function platformCertPath()
  193. {
  194. return storage_path('platformcert' . DIRECTORY_SEPARATOR);
  195. }
  196. private function platformCertFileName()
  197. {
  198. return \YunShop::app()->uniacid . "_" . $this->mchId() . "_wechat_pay_cert.pem";
  199. }
  200. }