weixinPayV2Old.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: YingZi
  8. // +----------------------------------------------------------------------
  9. // | Date: 2022-05-27 15:14
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace library\utils;
  13. use WeChatPay\Builder;
  14. use WeChatPay\Formatter;
  15. use WeChatPay\Transformer;
  16. use WeChatPay\Crypto\Hash;
  17. use WeChatPay\Crypto\AesEcb;
  18. use WeChatPay\Crypto\Rsa;
  19. use WeChatPay\Crypto\AesGcm;
  20. use WeChatPay\Util\PemUtil;
  21. use WeChatPay\Request\WeChatPayTradeOutTradeNoQueryRequest;
  22. class weixinPayV2{
  23. private $config;
  24. private $client;
  25. public $errorMsg="系统错误";
  26. private $merchantPrivateKeyInstance="";
  27. /**
  28. * 构造函数
  29. * @param type $config
  30. */
  31. public function __construct($config = [])
  32. {
  33. if(empty($config)) $config = config('wxpay');
  34. $this->config = $config;
  35. $instance = Builder::factory([
  36. 'mchid' => $this->config["MCHID"],
  37. 'serial' => 'nop',//商户证书序列号,不使用APIv3可填任意值
  38. 'privateKey' => 'any',//商户API私钥,不使用APIv3可填任意值
  39. 'certs' => ['any' => null],//不使用APIv3可填任意值
  40. 'secret' => $this->config["ApiV2Key"],
  41. 'merchant' => [
  42. 'cert' => $this->config["ApiclientCert"],
  43. 'key' => $this->config["ApiclientKey"],
  44. ],
  45. ]);
  46. $this->client = $instance;
  47. }
  48. /**
  49. * 微信小程序支付
  50. * @param type $post
  51. */
  52. public function wxmpPay($post=[]){
  53. $apiUrl = "pay/unifiedorder";
  54. $jsonData = [];
  55. //商户信息
  56. $jsonData["appid"] = $this->config["APPID"];
  57. $jsonData["mchid"] = $this->config["MCHID"];
  58. $jsonData["notify_url"] = $this->config["NOTIFY_URL"];
  59. //必填参数
  60. $jsonData["description"] = $post["description"];//描述
  61. $jsonData["out_trade_no"] = $post["out_trade_no"];//商户订单号
  62. $jsonData["amount"] = [
  63. "total"=> (int)(floatval($post["total"])*100),//订单金额,分
  64. "currency" => 'CNY'
  65. ];
  66. $jsonData["scene_info"] = [//支付场景描述
  67. "payer_client_ip"=>empty($post["payer_client_ip"])?"127.0.0.1":$post["payer_client_ip"],//客户端IP
  68. ];
  69. $jsonData["time_expire"] = date("Y-m-d\TH:i:s+08:00",time()+30*60);//交易结束时间2018-06-08T10:34:56+08:00
  70. $jsonData["payer"]=[
  71. "openid"=>$post["openid"]
  72. ];
  73. $result = $this->clientHttp("POST", $apiUrl, $jsonData);
  74. if(empty($result)){
  75. if(empty($this->errorMsg)){
  76. $this->errorMsg = "支付错误001";
  77. }
  78. return false;
  79. }
  80. $resuleAr = json_decode($result,true);
  81. if(empty($resuleAr)){
  82. if(empty($this->errorMsg)){
  83. $this->errorMsg = "支付错误002";
  84. }
  85. return false;
  86. }
  87. if(empty($resuleAr["prepay_id"])){
  88. if(empty($this->errorMsg)){
  89. $this->errorMsg = "支付错误003";
  90. }
  91. return false;
  92. }
  93. //组装支付参数
  94. $payInfo=array();
  95. $data=$this->makeSign(["appId"=>$this->config["APPID"],"prepay_id"=>$resuleAr["prepay_id"]]);
  96. $data["payData"] = $jsonData;
  97. return $data;
  98. }
  99. /**
  100. * 查询订单
  101. * @param type $out_trade_no 商户订单号
  102. */
  103. public function searchOrder($out_trade_no){
  104. $apiUrl = "v3/pay/transactions/out-trade-no/{out_trade_no}";
  105. $result = $this->clientHttp("GET", $apiUrl,[
  106. "out_trade_no"=>$out_trade_no,
  107. "query"=>["mchid"=>$this->config["MCHID"]],
  108. ]);
  109. return $result;
  110. }
  111. /**
  112. * 关闭订单
  113. * @param type $out_trade_no 商户订单号
  114. */
  115. public function closeOrder($out_trade_no){
  116. $apiUrl = "v3/pay/transactions/out-trade-no/{out_trade_no}/close";
  117. $result = $this->clientHttp("GET", $apiUrl,[
  118. "out_trade_no"=>$out_trade_no,
  119. "query"=>["mchid"=>$this->config["MCHID"]],
  120. ]);
  121. return $result;
  122. }
  123. /**
  124. * 生成签名
  125. * @param type $info
  126. * @return string
  127. */
  128. private function makeSign($info){
  129. $params = [
  130. 'appId' => $info["appId"],
  131. 'timeStamp' => (string)Formatter::timestamp(),
  132. 'nonceStr' => Formatter::nonce(),
  133. 'package' => 'prepay_id='.$info["prepay_id"],
  134. ];
  135. $params["paySign"] = Rsa::sign(Formatter::joinedByLineFeed(...array_values($params)),$this->merchantPrivateKeyInstance);
  136. $params["signType"] = 'RSA';
  137. return $params;
  138. }
  139. /**
  140. * 解密回调参数
  141. * @param type $data
  142. * @return type
  143. */
  144. public function aesGcmDecrypt($data){
  145. // 加密文本消息解密
  146. $inBodyResource = AesGcm::decrypt($data["ciphertext"], $this->config["apiv3Key"], $data["nonce"], $data["associated_data"]);
  147. // 把解密后的文本转换为PHP Array数组
  148. $inBodyResourceArray = (array)json_decode($inBodyResource, true);
  149. return $inBodyResourceArray;
  150. }
  151. /**
  152. * http提交[同步请求]
  153. * @param type $type
  154. * @param type $url 示例:v3/pay/transactions/native
  155. * @param type $json
  156. * @return boolean
  157. */
  158. private function clientHttp($type='POST',$url='',$json=[]){
  159. try {
  160. $resp=null;
  161. if($type=="POST"){
  162. $resp = $this->client->chain($url)->post(['json' => $json]);
  163. if(empty($json)){
  164. $resp = $this->client->chain($url)->post();
  165. }else{
  166. $resp = $this->client->chain($url)->post(['json' => $json]);
  167. }
  168. }
  169. if($type=="GET"){
  170. if(empty($json)){
  171. $resp = $this->client->chain($url)->get();
  172. }else{
  173. $resp = $this->client->chain($url)->get($json);
  174. }
  175. }
  176. if(empty($resp)){
  177. $this->errorMsg="提交方式错误";
  178. return false;
  179. }
  180. $statusCode = $resp->getStatusCode();
  181. if ($statusCode == 200) { //处理成功
  182. return $resp->getBody()->getContents();
  183. } else if ($statusCode == 204) { //处理成功,无返回Body
  184. $this->errorMsg = "处理成功,无返回Body";
  185. return false;
  186. }else{
  187. $this->errorMsg = "未知错误";
  188. return false;
  189. }
  190. } catch (\Exception $e) {
  191. // 进行错误处理
  192. $this->errorMsg = $e->getMessage();
  193. if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
  194. $r = $e->getResponse();
  195. $this->errorMsg=$r->getStatusCode()."".$r->getReasonPhrase().$r->getBody();
  196. }
  197. return false;
  198. }
  199. }
  200. }