| Total Complexity | 147 |
| Total Lines | 1041 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like WechatPay often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WechatPay, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class WechatPay { |
||
| 11 | const TRADETYPE_JSAPI = 'JSAPI',TRADETYPE_NATIVE = 'NATIVE',TRADETYPE_APP = 'APP',TRADETYPE_MWEB = 'MWEB'; |
||
| 12 | const SIGNTYPE_MD5 = 'MD5', SIGNTYPE_HMACSHA256 = 'HMAC-SHA256'; |
||
| 13 | const CHECKNAME_FORCECHECK = 'FORCE_CHECK',CHECKNAME_NOCHECK = 'NO_CHECK'; |
||
| 14 | /** 支付 */ |
||
| 15 | const URL_UNIFIEDORDER = "https://api.mch.weixin.qq.com/pay/unifiedorder"; |
||
| 16 | const URL_ORDERQUERY = "https://api.mch.weixin.qq.com/pay/orderquery"; |
||
| 17 | const URL_CLOSEORDER = 'https://api.mch.weixin.qq.com/pay/closeorder'; |
||
| 18 | const URL_REFUND = 'https://api.mch.weixin.qq.com/secapi/pay/refund'; |
||
| 19 | const URL_REFUNDQUERY = 'https://api.mch.weixin.qq.com/pay/refundquery'; |
||
| 20 | const URL_DOWNLOADBILL = 'https://api.mch.weixin.qq.com/pay/downloadbill'; |
||
| 21 | const URL_REPORT = 'https://api.mch.weixin.qq.com/payitil/report'; |
||
| 22 | const URL_SHORTURL = 'https://api.mch.weixin.qq.com/tools/shorturl'; |
||
| 23 | const URL_MICROPAY = 'https://api.mch.weixin.qq.com/pay/micropay'; |
||
| 24 | const URL_GETHBINFO = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo'; |
||
| 25 | const URL_BATCHQUERYCOMMENT = 'https://api.mch.weixin.qq.com/billcommentsp/batchquerycomment'; |
||
| 26 | const URL_REVERSE = 'https://api.mch.weixin.qq.com/secapi/pay/reverse'; |
||
| 27 | const URL_AUTHCODETOOPENID = 'https://api.mch.weixin.qq.com/tools/authcodetoopenid'; |
||
| 28 | /** 红包 */ |
||
| 29 | const URL_SENDREDPACK = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack'; |
||
| 30 | const URL_SENDGROUPREDPACK = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack'; |
||
| 31 | /** 企业付款 */ |
||
| 32 | const URL_TRANSFER_WALLET = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; |
||
| 33 | const URL_QUERY_TRANSFER_WALLET = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo'; |
||
| 34 | const URL_TRANSFER_BANKCARD = 'https://api.mch.weixin.qq.com/mmpaysptrans/pay_bank'; |
||
| 35 | const URL_QUERY_TRANSFER_BANKCARD = 'https://api.mch.weixin.qq.com/mmpaysptrans/query_bank'; |
||
| 36 | /** 代金券 */ |
||
| 37 | const URL_SEND_COUPON = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/send_coupon'; |
||
| 38 | const URL_QUERY_COUPON_STOCK = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/query_coupon_stock'; |
||
| 39 | const URL_QUERY_COUPON_INFO = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/querycouponsinfo'; |
||
| 40 | /** Sandbox获取测试公钥 */ |
||
| 41 | const URL_GETPUBLICKEY = 'https://fraud.mch.weixin.qq.com/risk/getpublickey'; |
||
| 42 | public static $BANKCODE = ['工商银行' => '1002', '农业银行' => '1005', '中国银行' => '1026', '建设银行' => '1003', '招商银行' => '1001', |
||
| 43 | '邮储银行' => '1066', '交通银行' => '1020', '浦发银行' => '1004', '民生银行' => '1006', '兴业银行' => '1009', '平安银行' => '1010', |
||
| 44 | '中信银行' => '1021', '华夏银行' => '1025', '广发银行' => '1027', '光大银行' => '1022', '北京银行' => '1032', '宁波银行' => '1056',]; |
||
| 45 | |||
| 46 | public $getSignKeyUrl = "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey"; |
||
| 47 | public $sandbox = false; |
||
| 48 | |||
| 49 | /** @var string */ |
||
| 50 | public $returnCode; |
||
| 51 | /** @var string */ |
||
| 52 | public $returnMsg; |
||
| 53 | /** @var string */ |
||
| 54 | public $resultCode; |
||
| 55 | /** @var string */ |
||
| 56 | public $errCode; |
||
| 57 | /** @var string */ |
||
| 58 | public $errCodeDes; |
||
| 59 | /** @var string */ |
||
| 60 | public $requestXML = null; |
||
| 61 | /** @var string */ |
||
| 62 | public $responseXML = null; |
||
| 63 | /** @var array */ |
||
| 64 | public $requestArray = null; |
||
| 65 | /** @var array */ |
||
| 66 | public $responseArray = null; |
||
| 67 | /** @var array */ |
||
| 68 | private $config; |
||
| 69 | /** @var HttpClient */ |
||
| 70 | private $httpClient = null; |
||
| 71 | /** @var WechatOAuth */ |
||
| 72 | private $wechatOAuth = null; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param $config array 配置 |
||
| 76 | */ |
||
| 77 | public function __construct(array $config) { |
||
| 78 | $this->config = $config; |
||
| 79 | $this->httpClient = new HttpClient(5); |
||
| 80 | } |
||
| 81 | |||
| 82 | public function getWechatOAuth(){ |
||
| 83 | if(!$this->wechatOAuth){ |
||
| 84 | $this->wechatOAuth = new WechatOAuth($this->config['app_id'],$this->config['app_secret']); |
||
| 85 | } |
||
| 86 | return $this->wechatOAuth; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function setConfig($config){ |
||
| 90 | $this->config = $config; |
||
| 91 | } |
||
| 92 | |||
| 93 | public function setHttpClient($httpClient){ |
||
| 94 | $this->httpClient = $httpClient; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * 获取JSAPI(公众号/小程序)的预支付单信息(prepay_id) |
||
| 99 | * @ref https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1 |
||
| 100 | * @param $body string 内容 |
||
| 101 | * @param $out_trade_no string 商户订单号 |
||
| 102 | * @param $total_fee int 总金额 |
||
| 103 | * @param $openid string openid |
||
| 104 | * @param $spbill_create_ip |
||
| 105 | * @param $ext array |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | public function getPrepayId($body,$out_trade_no,$total_fee,$openid,$spbill_create_ip = null,$ext = null) { |
||
| 109 | $data = ($ext && is_array($ext))?$ext:array(); |
||
| 110 | $data["body"] = $body; |
||
| 111 | $data["out_trade_no"] = $out_trade_no; |
||
| 112 | $data["total_fee"] = $total_fee; |
||
| 113 | $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["REMOTE_ADDR"]; |
||
| 114 | $data["notify_url"] = $this->config["notify_url"]; |
||
| 115 | $data["trade_type"] = WechatPay::TRADETYPE_JSAPI; |
||
| 116 | $data["openid"] = $openid; |
||
| 117 | $result = $this->unifiedOrder($data); |
||
| 118 | return $result["prepay_id"]; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * 获取APP的的预支付单信息(prepay_id)(注意这里的appid是从开放平台申请的) |
||
| 123 | * @ref https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1 |
||
| 124 | * @param $body string 内容 |
||
| 125 | * @param $out_trade_no string 商户订单号 |
||
| 126 | * @param $total_fee int 总金额 |
||
| 127 | * @param $spbill_create_ip string 终端ID |
||
| 128 | * @param $ext array |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | public function getPrepayIdAPP($body,$out_trade_no,$total_fee,$spbill_create_ip,$ext = null) { |
||
| 132 | $data = ($ext && is_array($ext))?$ext:array(); |
||
| 133 | $data["body"] = $body; |
||
| 134 | $data["out_trade_no"] = $out_trade_no; |
||
| 135 | $data["total_fee"] = $total_fee; |
||
| 136 | $data["spbill_create_ip"] = $spbill_create_ip; |
||
| 137 | $data["notify_url"] = $this->config["notify_url"]; |
||
| 138 | $data["trade_type"] = WechatPay::TRADETYPE_APP; |
||
| 139 | $result = $this->unifiedOrder($data); |
||
| 140 | return $result["prepay_id"]; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * 扫码支付(模式二)获取支付二维码 |
||
| 145 | * @ref https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1 |
||
| 146 | * @param $body |
||
| 147 | * @param $out_trade_no |
||
| 148 | * @param $total_fee |
||
| 149 | * @param $product_id |
||
| 150 | * @param $spbill_create_ip string 本地IP |
||
| 151 | * @param $ext array |
||
| 152 | * @return null |
||
| 153 | */ |
||
| 154 | public function getCodeUrl($body,$out_trade_no,$total_fee,$product_id,$spbill_create_ip = null,$ext = null){ |
||
| 155 | $data = ($ext && is_array($ext))?$ext:array(); |
||
| 156 | $data["body"] = $body; |
||
| 157 | $data["out_trade_no"] = $out_trade_no; |
||
| 158 | $data["total_fee"] = $total_fee; |
||
| 159 | $data["spbill_create_ip"] = $spbill_create_ip?:$_SERVER["SERVER_ADDR"]; |
||
| 160 | $data["notify_url"] = $this->config["notify_url"]; |
||
| 161 | $data["trade_type"] = self::TRADETYPE_NATIVE; |
||
| 162 | $data["product_id"] = $product_id; |
||
| 163 | $result = $this->unifiedOrder($data); |
||
| 164 | return $result["code_url"]; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * H5支付获取支付跳转链接 |
||
| 169 | * @ref https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_20&index=1 |
||
| 170 | * @param $body string 商品描述 |
||
| 171 | * @param $out_trade_no string 商户订单号 |
||
| 172 | * @param $total_fee int 总金额(分) |
||
| 173 | * @param $ext array |
||
| 174 | * @return string |
||
| 175 | * @throws Exception |
||
| 176 | */ |
||
| 177 | public function getMwebUrl($body,$out_trade_no,$total_fee,$ext = null){ |
||
| 178 | $data = ($ext && is_array($ext))?$ext:array(); |
||
| 179 | $data["body"] = $body; |
||
| 180 | $data["out_trade_no"] = $out_trade_no; |
||
| 181 | $data["total_fee"] = $total_fee; |
||
| 182 | $data["spbill_create_ip"] = isset($_SERVER["REMOTE_ADDR"])?$_SERVER["REMOTE_ADDR"]:''; |
||
| 183 | $data["notify_url"] = $this->config["notify_url"]; |
||
| 184 | $data["trade_type"] = self::TRADETYPE_MWEB; |
||
| 185 | if(!isset($this->config['h5_scene_info'])) throw new \Exception('h5_scene_info should be configured'); |
||
| 186 | $data["scene_info"] = json_encode($this->config['h5_scene_info']); |
||
| 187 | $result = $this->unifiedOrder($data); |
||
| 188 | return $result["mweb_url"]; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * 统一下单接口 |
||
| 193 | * @ref https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1 |
||
| 194 | * @param array $params |
||
| 195 | * @return string JSON |
||
| 196 | */ |
||
| 197 | private function unifiedOrder($params) { |
||
| 198 | $data = array(); |
||
| 199 | $data["appid"] = $this->config["app_id"]; |
||
| 200 | $data["device_info"] = (isset($params['device_info'])&&trim($params['device_info'])!='')?$params['device_info']:null; |
||
| 201 | $data["body"] = $params['body']; |
||
| 202 | $data["detail"] = isset($params['detail'])?$params['detail']:null;//optional |
||
| 203 | $data["attach"] = isset($params['attach'])?$params['attach']:null;//optional |
||
| 204 | $data["out_trade_no"] = isset($params['out_trade_no'])?$params['out_trade_no']:null; |
||
| 205 | $data["fee_type"] = isset($params['fee_type'])?$params['fee_type']:'CNY'; |
||
| 206 | $data["total_fee"] = $params['total_fee']; |
||
| 207 | $data["spbill_create_ip"] = $params['spbill_create_ip']; |
||
| 208 | $data["time_start"] = isset($params['time_start'])?$params['time_start']:null;//optional |
||
| 209 | $data["time_expire"] = isset($params['time_expire'])?$params['time_expire']:null;//optional |
||
| 210 | $data["goods_tag"] = isset($params['goods_tag'])?$params['goods_tag']:null; |
||
| 211 | $data["notify_url"] = $this->config["notify_url"]; |
||
| 212 | $data["trade_type"] = $params['trade_type']; |
||
| 213 | $data["product_id"] = isset($params['product_id'])?$params['product_id']:null;//required when trade_type = NATIVE |
||
| 214 | $data["openid"] = isset($params['openid'])?$params['openid']:null;//required when trade_type = JSAPI |
||
| 215 | $result = $this->post(self::URL_UNIFIEDORDER, $data); |
||
| 216 | return $result; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * 查询订单(根据微信订单号) |
||
| 221 | * @ref https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_2 |
||
| 222 | * @param $transaction_id string 微信订单号 |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | public function queryOrderByTransactionId($transaction_id){ |
||
| 226 | $data = array(); |
||
| 227 | $data["appid"] = $this->config["app_id"]; |
||
| 228 | $data["transaction_id"] = $transaction_id; |
||
| 229 | $result = $this->post(self::URL_ORDERQUERY, $data); |
||
| 230 | return $result; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * 查询订单(根据商户订单号) |
||
| 235 | * @ref https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_2 |
||
| 236 | * @param $out_trade_no string 商户订单号 |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | public function queryOrderByOutTradeNo($out_trade_no){ |
||
| 240 | $data = array(); |
||
| 241 | $data["appid"] = $this->config["app_id"]; |
||
| 242 | $data["out_trade_no"] = $out_trade_no; |
||
| 243 | $result = $this->post(self::URL_ORDERQUERY, $data); |
||
| 244 | return $result; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * 查询退款(根据微信订单号) |
||
| 249 | * @ref https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_5 |
||
| 250 | * @param $transaction_id string 微信交易号 |
||
| 251 | * @param $offset int 偏移 |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | public function queryRefundByTransactionId($transaction_id,$offset = 0){ |
||
| 255 | $data = array(); |
||
| 256 | $data["appid"] = $this->config["app_id"]; |
||
| 257 | $data["transaction_id"] = $transaction_id; |
||
| 258 | $data["offset"] = $offset; |
||
| 259 | $result = $this->post(self::URL_REFUNDQUERY, $data); |
||
| 260 | return $result; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * 查询退款(根据商户订单号) |
||
| 265 | * @ref https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_5 |
||
| 266 | * @param $out_trade_no string 商户交易号 |
||
| 267 | * @param $offset int 偏移 |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | public function queryRefundByOutTradeNo($out_trade_no,$offset = 0){ |
||
| 271 | $data = array(); |
||
| 272 | $data["appid"] = $this->config["app_id"]; |
||
| 273 | $data["out_trade_no"] = $out_trade_no; |
||
| 274 | $data["offset"] = $offset; |
||
| 275 | $result = $this->post(self::URL_REFUNDQUERY, $data); |
||
| 276 | return $result; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * 查询退款(根据微信退款单号) |
||
| 281 | * @ref https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_5 |
||
| 282 | * @param $refund_id string 微信退款单号 |
||
| 283 | * @param $offset int 偏移 |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | public function queryRefundByRefundId($refund_id,$offset = 0){ |
||
| 287 | $data = array(); |
||
| 288 | $data["appid"] = $this->config["app_id"]; |
||
| 289 | $data["refund_id"] = $refund_id; |
||
| 290 | $data["offset"] = $offset; |
||
| 291 | $result = $this->post(self::URL_REFUNDQUERY, $data); |
||
| 292 | return $result; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * 查询退款(根据商户退款单号) |
||
| 297 | * @ref https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_5 |
||
| 298 | * @param $out_refund_no string 商户退款单号 |
||
| 299 | * @param $offset int 偏移 |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | public function queryRefundByOutRefundNo($out_refund_no,$offset = 0){ |
||
| 303 | $data = array(); |
||
| 304 | $data["appid"] = $this->config["app_id"]; |
||
| 305 | $data["out_refund_no"] = $out_refund_no; |
||
| 306 | $data["offset"] = $offset; |
||
| 307 | $result = $this->post(self::URL_REFUNDQUERY, $data); |
||
| 308 | return $result; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * 关闭订单 |
||
| 313 | * @ref https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_3 |
||
| 314 | * @param $out_trade_no string 商户订单号 |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | public function closeOrder($out_trade_no){ |
||
| 318 | $data = array(); |
||
| 319 | $data["appid"] = $this->config["app_id"]; |
||
| 320 | $data["out_trade_no"] = $out_trade_no; |
||
| 321 | $result = $this->post(self::URL_CLOSEORDER, $data,false); |
||
| 322 | return $result; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * 申请退款 - 使用商户订单号 |
||
| 327 | * @ref https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_4 |
||
| 328 | * @ref https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4 |
||
| 329 | * @param $out_trade_no string 商户订单号 |
||
| 330 | * @param $out_refund_no string 商户退款单号 |
||
| 331 | * @param $total_fee int 总金额(单位:分) |
||
| 332 | * @param $refund_fee int 退款金额(单位:分) |
||
| 333 | * @param $ext array 扩展数组 |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | public function refundByOutTradeNo($out_trade_no,$out_refund_no,$total_fee,$refund_fee,$ext = array()){ |
||
| 337 | $data = ($ext && is_array($ext))?$ext:array(); |
||
| 338 | $data["appid"] = $this->config["app_id"]; |
||
| 339 | $data["out_trade_no"] = $out_trade_no; |
||
| 340 | $data["out_refund_no"] = $out_refund_no; |
||
| 341 | $data["total_fee"] = $total_fee; |
||
| 342 | $data["refund_fee"] = $refund_fee; |
||
| 343 | $result = $this->post(self::URL_REFUND, $data,true); |
||
| 344 | return $result; |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * 申请退款 - 使用微信订单号 |
||
| 349 | * @ref https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_4 |
||
| 350 | * @ref https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4 |
||
| 351 | * @param $transaction_id string 微信订单号 |
||
| 352 | * @param $out_refund_no string 商户退款单号 |
||
| 353 | * @param $total_fee int 总金额(单位:分) |
||
| 354 | * @param $refund_fee int 退款金额(单位:分) |
||
| 355 | * @param $ext array 扩展数组 |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | public function refundByTransactionId($transaction_id,$out_refund_no,$total_fee,$refund_fee,$ext = array()){ |
||
| 359 | $data = ($ext && is_array($ext))?$ext:array(); |
||
| 360 | $data["appid"] = $this->config["app_id"]; |
||
| 361 | $data["transaction_id"] = $transaction_id; |
||
| 362 | $data["out_refund_no"] = $out_refund_no; |
||
| 363 | $data["total_fee"] = $total_fee; |
||
| 364 | $data["refund_fee"] = $refund_fee; |
||
| 365 | $result = $this->post(self::URL_REFUND, $data,true); |
||
| 366 | return $result; |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * 撤销订单 - 使用商户订单号 |
||
| 371 | * @ref https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_11&index=3 |
||
| 372 | * @param $out_trade_no string 商户订单号 |
||
| 373 | * @return array |
||
| 374 | */ |
||
| 375 | public function reverseByOutTradeNo($out_trade_no){ |
||
| 376 | $data = array(); |
||
| 377 | $data["appid"] = $this->config["app_id"]; |
||
| 378 | $data["out_trade_no"] = $out_trade_no; |
||
| 379 | $result = $this->post(self::URL_REVERSE, $data,true); |
||
| 380 | return $result; |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * 撤销订单 - 使用微信订单号 |
||
| 385 | * @ref https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_11&index=3 |
||
| 386 | * @param $transaction_id string 微信订单号 |
||
| 387 | * @return array |
||
| 388 | */ |
||
| 389 | public function reverseByTransactionId($transaction_id){ |
||
| 390 | $data = array(); |
||
| 391 | $data["appid"] = $this->config["app_id"]; |
||
| 392 | $data["transaction_id"] = $transaction_id; |
||
| 393 | $result = $this->post(self::URL_REVERSE, $data,true); |
||
| 394 | return $result; |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * 下载对账单 |
||
| 399 | * @param $bill_date string 下载对账单的日期,格式:20140603 |
||
| 400 | * @param $bill_type string 类型 |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | public function downloadBill($bill_date,$bill_type = 'ALL'){ |
||
| 404 | $data = array(); |
||
| 405 | $data["appid"] = $this->config["app_id"]; |
||
| 406 | $data["bill_date"] = $bill_date; |
||
| 407 | $data["bill_type"] = $bill_type; |
||
| 408 | $result = $this->post(self::URL_DOWNLOADBILL, $data); |
||
| 409 | return $result; |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * 发放普通红包 |
||
| 414 | * @ref https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3 |
||
| 415 | * @param $mch_billno string 商户订单号 |
||
| 416 | * @param $send_name string 商户名称 |
||
| 417 | * @param $re_openid string 用户openid |
||
| 418 | * @param $total_amount int 付款金额 单位分 |
||
| 419 | * @param $total_num int 红包发放总人数 |
||
| 420 | * @param $wishing string 红包祝福语 |
||
| 421 | * @param $act_name string 活动名称 |
||
| 422 | * @param $remark string 备注 |
||
| 423 | * @param $scene_id string 场景id,发放红包使用场景,红包金额大于200时必传 PRODUCT_1:商品促销 PRODUCT_2:抽奖 PRODUCT_3:虚拟物品兑奖 PRODUCT_4:企业内部福利 PRODUCT_5:渠道分润 PRODUCT_6:保险回馈 PRODUCT_7:彩票派奖 PRODUCT_8:税务刮奖 |
||
| 424 | * @param $riskinfo string 活动信息 |
||
| 425 | * @param $consume_mch_id string 资金授权商户号 |
||
| 426 | * @return array |
||
| 427 | * @throws Exception |
||
| 428 | */ |
||
| 429 | public function sendRedPack($mch_billno,$send_name,$re_openid,$total_amount,$total_num,$wishing,$act_name,$remark,$scene_id = '',$riskinfo = '',$consume_mch_id = ''){ |
||
| 430 | $data = array(); |
||
| 431 | $data["wxappid"] = $this->config["app_id"]; |
||
| 432 | $data["mch_billno"] = $mch_billno; |
||
| 433 | $data["send_name"] = $send_name; |
||
| 434 | $data["re_openid"] = $re_openid; |
||
| 435 | $data["total_amount"] = $total_amount; |
||
| 436 | if($total_amount > 20000 && trim($scene_id)=='') throw new \Exception("scene_id is required when total_amount beyond 20000"); |
||
| 437 | $data["total_num"] = $total_num; |
||
| 438 | $data["wishing"] = $wishing; |
||
| 439 | $data["act_name"] = $act_name; |
||
| 440 | $data["remark"] = $remark; |
||
| 441 | $data["scene_id"] = $scene_id; |
||
| 442 | $data["riskinfo"] = $riskinfo; |
||
| 443 | $data["consume_mch_id"] = $consume_mch_id; |
||
| 444 | $result = $this->post(self::URL_SENDREDPACK, $data, true); //cert is required |
||
| 445 | return $result; |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * 发放裂变红包 |
||
| 450 | * @ref https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5&index=4 |
||
| 451 | * @param $mch_billno string 商户订单号 |
||
| 452 | * @param $send_name string 商户名称 |
||
| 453 | * @param $re_openid string 用户openid |
||
| 454 | * @param $total_amount int 付款金额 单位分 |
||
| 455 | * @param $total_num int 红包发放总人数 |
||
| 456 | * @param $wishing string 红包祝福语 |
||
| 457 | * @param $act_name string 活动名称 |
||
| 458 | * @param $remark string 备注 |
||
| 459 | * @param $scene_id string 场景id,发放红包使用场景,红包金额大于200时必传 PRODUCT_1:商品促销 PRODUCT_2:抽奖 PRODUCT_3:虚拟物品兑奖 PRODUCT_4:企业内部福利 PRODUCT_5:渠道分润 PRODUCT_6:保险回馈 PRODUCT_7:彩票派奖 PRODUCT_8:税务刮奖 |
||
| 460 | * @param $riskinfo string 活动信息 |
||
| 461 | * @param $consume_mch_id string 资金授权商户号 |
||
| 462 | * @return array |
||
| 463 | * @throws Exception |
||
| 464 | */ |
||
| 465 | public function sendGroupRedPack($mch_billno,$send_name,$re_openid,$total_amount,$total_num,$wishing,$act_name,$remark,$scene_id = '',$riskinfo = '',$consume_mch_id = ''){ |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * 查询红包记录 |
||
| 487 | * @param $mch_billno string 商户订单号 |
||
| 488 | * @return array |
||
| 489 | * @throws Exception |
||
| 490 | * @ref https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_6&index=5 |
||
| 491 | */ |
||
| 492 | public function getHbInfo($mch_billno){ |
||
| 493 | $data = array(); |
||
| 494 | $data["mch_billno"] = $mch_billno; |
||
| 495 | $data["appid"] = $this->config["app_id"]; |
||
| 496 | $data["bill_type"] = 'MCHT'; //MCHT:通过商户订单号获取红包信息。 |
||
| 497 | $result = $this->post(self::URL_GETHBINFO, $data, true); //cert is required |
||
| 498 | return $result; |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * 拉取订单评价数据 |
||
| 503 | * @param string $begin_time 开始时间,格式为yyyyMMddHHmmss |
||
| 504 | * @param string $end_time 结束时间,格式为yyyyMMddHHmmss |
||
| 505 | * @param int $offset 偏移 |
||
| 506 | * @param int $limit 条数 |
||
| 507 | * @return array |
||
| 508 | */ |
||
| 509 | public function batchQueryComment($begin_time,$end_time,$offset = 0,$limit = 200){ |
||
| 510 | $data = array(); |
||
| 511 | $data["appid"] = $this->config["app_id"]; |
||
| 512 | $data["begin_time"] = $begin_time; |
||
| 513 | $data["end_time"] = $end_time; |
||
| 514 | $data["offset"] = $offset; |
||
| 515 | $data["limit"] = $limit; |
||
| 516 | $data["sign"] = $this->sign($data,WechatPay::SIGNTYPE_HMACSHA256); |
||
| 517 | $result = $this->post(self::URL_BATCHQUERYCOMMENT, $data, true); //cert is required |
||
| 518 | return $result; |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * 获取支付参数(JSAPI - 公众号/小程序支付 , APP - APP支付) |
||
| 523 | * @param $prepay_id string 预支付ID |
||
| 524 | * @param $trade_type string 支付类型 |
||
| 525 | * @return array |
||
| 526 | */ |
||
| 527 | public function getPackage($prepay_id, $trade_type = WechatPay::TRADETYPE_JSAPI) { |
||
| 528 | $data = array(); |
||
| 529 | if ($trade_type == WechatPay::TRADETYPE_JSAPI){ |
||
| 530 | $data["package"] = "prepay_id=$prepay_id"; |
||
| 531 | $data["timeStamp"] = time(); |
||
| 532 | $data["nonceStr"] = $this->getNonceStr(); |
||
| 533 | $data["appId"] = $this->config["app_id"]; |
||
| 534 | $data["signType"] = "MD5"; |
||
| 535 | $data["paySign"] = $this->sign($data); |
||
| 536 | } else if ($trade_type == WechatPay::TRADETYPE_APP){ |
||
| 537 | $data["package"] = "Sign=WXPay"; |
||
| 538 | $data['prepayid'] = $prepay_id; |
||
| 539 | $data['partnerid'] = $this->config["mch_id"]; |
||
| 540 | $data["timestamp"] = time(); |
||
| 541 | $data["noncestr"] = $this->getNonceStr(); |
||
| 542 | $data["appid"] = $this->config["app_id"]; |
||
| 543 | $data["sign"] = $this->sign($data); |
||
| 544 | } |
||
| 545 | return $data; |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * 提交刷卡支付 |
||
| 550 | * @ref https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1 |
||
| 551 | * @param $body |
||
| 552 | * @param $out_trade_no |
||
| 553 | * @param $total_fee |
||
| 554 | * @param $spbill_create_ip |
||
| 555 | * @param $auth_code |
||
| 556 | * @param array $ext |
||
| 557 | * @return array |
||
| 558 | */ |
||
| 559 | public function microPay($body,$out_trade_no,$total_fee,$spbill_create_ip,$auth_code,$ext = array()){ |
||
| 560 | $data = (!empty($ext) && is_array($ext))?$ext:array(); |
||
| 561 | $data["appid"] = $this->config["app_id"]; |
||
| 562 | $data["body"] = $body; |
||
| 563 | $data["out_trade_no"] = $out_trade_no; |
||
| 564 | $data["total_fee"] = $total_fee; |
||
| 565 | $data["spbill_create_ip"] = $spbill_create_ip; |
||
| 566 | $data["auth_code"] = $auth_code; |
||
| 567 | $result = $this->post(self::URL_MICROPAY,$data,false); |
||
| 568 | return $result; |
||
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * 支付结果通知处理 |
||
| 573 | * @param $notify_data array|string 通知数据 |
||
| 574 | * @param $callback callable 回调 |
||
| 575 | * @return null |
||
| 576 | * @throws Exception |
||
| 577 | */ |
||
| 578 | public function onPaidNotify($notify_data,callable $callback = null){ |
||
| 579 | if(!is_array($notify_data)){ |
||
| 580 | $notify_data = $this->xml2array($notify_data); |
||
| 581 | } |
||
| 582 | if($this->validateSign($notify_data)){ |
||
| 583 | if($callback && is_callable($callback)){ |
||
| 584 | return call_user_func_array( $callback , [$notify_data] ); |
||
| 585 | }else{ |
||
| 586 | $this->responseNotify(); |
||
| 587 | } |
||
| 588 | }else{ |
||
| 589 | throw new \Exception('Invalid paid notify data'); |
||
| 590 | } |
||
| 591 | } |
||
| 592 | |||
| 593 | /** |
||
| 594 | * 退款结果通知处理 |
||
| 595 | * @param string|array $notify_data 通知数据(XML/array) |
||
| 596 | * @param callable $callback 回调 |
||
| 597 | * @return mixed |
||
| 598 | * @throws Exception |
||
| 599 | */ |
||
| 600 | public function onRefundedNotify($notify_data,callable $callback = null){ |
||
| 612 | } |
||
| 613 | } |
||
| 614 | |||
| 615 | /** |
||
| 616 | * 验证数据签名 |
||
| 617 | * @param $data array 数据数组 |
||
| 618 | * @return boolean 数据校验结果 |
||
| 619 | */ |
||
| 620 | public function validateSign($data) { |
||
| 621 | if (!isset($data["sign"])) { |
||
| 622 | return false; |
||
| 623 | } |
||
| 624 | $sign = $data["sign"]; |
||
| 625 | unset($data["sign"]); |
||
| 626 | return $this->sign($data) == $sign; |
||
| 627 | } |
||
| 628 | |||
| 629 | /** |
||
| 630 | * 响应微信支付后台通知 |
||
| 631 | * @param $return_code string 返回状态码 SUCCESS/FAIL |
||
| 632 | * @param $return_msg string 返回信息 |
||
| 633 | */ |
||
| 634 | public function responseNotify($return_code="SUCCESS", $return_msg= 'OK') { |
||
| 635 | $data = array(); |
||
| 636 | $data["return_code"] = $return_code; |
||
| 637 | if ($return_msg) { |
||
| 638 | $data["return_msg"] = $return_msg; |
||
| 639 | } |
||
| 640 | $xml = $this->array2xml($data); |
||
| 641 | print $xml; |
||
| 642 | } |
||
| 643 | |||
| 644 | /** |
||
| 645 | * 交易保障 |
||
| 646 | * @ref https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_8&index=8 |
||
| 647 | * @param string $interface_url |
||
| 648 | * @param string $execution_time |
||
| 649 | * @param string $return_code |
||
| 650 | * @param string $result_code |
||
| 651 | * @param string $user_ip |
||
| 652 | * @param string $out_trade_no |
||
| 653 | * @param string $time |
||
| 654 | * @param string $device_info |
||
| 655 | * @param string $return_msg |
||
| 656 | * @param string $err_code |
||
| 657 | * @param string $err_code_des |
||
| 658 | * @return array |
||
| 659 | */ |
||
| 660 | public function report($interface_url,$execution_time,$return_code,$result_code,$user_ip,$out_trade_no = null,$time = null,$device_info = null, |
||
| 661 | $return_msg = null,$err_code = null,$err_code_des = null){ |
||
| 662 | $data = array(); |
||
| 663 | $data["appid"] = $this->config["app_id"]; |
||
| 664 | $data["interface_url"] = $interface_url; |
||
| 665 | $data["execution_time"] = $execution_time; |
||
| 666 | $data["return_code"] = $return_code; |
||
| 667 | $data["result_code"] = $result_code; |
||
| 668 | $data["user_ip"] = $user_ip; |
||
| 669 | if($out_trade_no) $data["out_trade_no"] = $out_trade_no; |
||
| 670 | if($time) $data["time"] = $time; |
||
| 671 | if($device_info) $data["device_info"] = $device_info; |
||
| 672 | if($return_msg) $data["return_msg"] = $return_msg; |
||
| 673 | if($err_code) $data["err_code"] = $err_code; |
||
| 674 | if($err_code_des) $data["err_code_des"] = $err_code_des; |
||
| 675 | $result = $this->post(self::URL_REPORT, $data, false); |
||
| 676 | return $result; |
||
| 677 | } |
||
| 678 | |||
| 679 | /** |
||
| 680 | * 转换短链接 |
||
| 681 | * @ref https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_9&index=8 |
||
| 682 | * @param $longurl |
||
| 683 | * @return string |
||
| 684 | */ |
||
| 685 | public function shortUrl($longurl){ |
||
| 686 | $data = array(); |
||
| 687 | $data["appid"] = $this->config["app_id"]; |
||
| 688 | $data["long_url"] = $longurl; |
||
| 689 | $result = $this->post(self::URL_SHORTURL,$data,false); |
||
| 690 | return $result['short_url']; |
||
| 691 | } |
||
| 692 | |||
| 693 | /** |
||
| 694 | * 授权码查询openid |
||
| 695 | * @param $auth_code |
||
| 696 | * @return mixed |
||
| 697 | */ |
||
| 698 | public function authCodeToOpenId($auth_code){ |
||
| 699 | $data = array(); |
||
| 700 | $data["appid"] = $this->config["app_id"]; |
||
| 701 | $data["auth_code"] = $auth_code; |
||
| 702 | $result = $this->post(self::URL_AUTHCODETOOPENID,$data,false); |
||
| 703 | return $result['openid']; |
||
| 704 | } |
||
| 705 | |||
| 706 | /** |
||
| 707 | * 企业付款到零钱 |
||
| 708 | * @ref https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2 |
||
| 709 | * @param $partner_trade_no |
||
| 710 | * @param $openid |
||
| 711 | * @param $amount |
||
| 712 | * @param $desc |
||
| 713 | * @param $spbill_create_ip |
||
| 714 | * @param $check_name |
||
| 715 | * @param $re_user_name |
||
| 716 | * @return array |
||
| 717 | * @throws Exception |
||
| 718 | */ |
||
| 719 | public function transferWallet($partner_trade_no,$openid,$amount,$desc,$spbill_create_ip = null,$re_user_name = null,$check_name = WechatPay::CHECKNAME_FORCECHECK){ |
||
| 720 | $data = array(); |
||
| 721 | if($check_name == WechatPay::CHECKNAME_FORCECHECK && !$re_user_name) throw new Exception('Real name is required'); |
||
| 722 | $data["mch_appid"] = $this->config["app_id"]; |
||
| 723 | $data["mchid"] = $this->config["mch_id"]; |
||
| 724 | $data["partner_trade_no"] = $partner_trade_no; |
||
| 725 | $data["openid"] = $openid; |
||
| 726 | $data["amount"] = $amount; |
||
| 727 | $data["desc"] = $desc; |
||
| 728 | $data['spbill_create_ip'] = $spbill_create_ip?:$_SERVER['SERVER_ADDR']; |
||
| 729 | $data["check_name"] = $check_name; |
||
| 730 | $data["re_user_name"] = $re_user_name; |
||
| 731 | $result = $this->post(self::URL_TRANSFER_WALLET,$data,true); |
||
| 732 | return $result; |
||
| 733 | } |
||
| 734 | |||
| 735 | /** |
||
| 736 | * 查询企业付款 |
||
| 737 | * @ref https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_3 |
||
| 738 | * @param $partner_trade_no |
||
| 739 | * @return array |
||
| 740 | */ |
||
| 741 | public function queryTransferWallet($partner_trade_no){ |
||
| 742 | $data = array(); |
||
| 743 | $data["appid"] = $this->config["app_id"]; |
||
| 744 | $data["mch_id"] = $this->config["mch_id"]; |
||
| 745 | $data["partner_trade_no"] = $partner_trade_no; |
||
| 746 | $result = $this->post(self::URL_QUERY_TRANSFER_WALLET,$data,true); |
||
| 747 | return $result; |
||
| 748 | } |
||
| 749 | |||
| 750 | /** |
||
| 751 | * 企业付款到银行卡 |
||
| 752 | * @ref https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_2 |
||
| 753 | * @param $partner_trade_no |
||
| 754 | * @param $bank_no |
||
| 755 | * @param $true_name |
||
| 756 | * @param $bank_code |
||
| 757 | * @param $amount |
||
| 758 | * @param $desc |
||
| 759 | * @return array |
||
| 760 | * @throws Exception |
||
| 761 | */ |
||
| 762 | public function transferBankCard($partner_trade_no,$bank_no,$true_name,$bank_code,$amount,$desc){ |
||
| 763 | if(!in_array($bank_code,array_values(self::$BANKCODE))) throw new Exception("Unsupported bank code - $bank_code"); |
||
| 764 | $data = array(); |
||
| 765 | $data["partner_trade_no"] = $partner_trade_no; |
||
| 766 | $enc_bank_no = $this->rsaEncrypt($bank_no); |
||
| 767 | $data["enc_bank_no"] = $enc_bank_no; |
||
| 768 | $enc_true_name = $this->rsaEncrypt($true_name); |
||
| 769 | $data["enc_true_name"] = $enc_true_name; |
||
| 770 | $data["bank_code"] = $bank_code; |
||
| 771 | $data["desc"] = $desc; |
||
| 772 | $data["amount"] = $amount; |
||
| 773 | $result = $this->post(self::URL_TRANSFER_BANKCARD,$data,true); |
||
| 774 | return $result; |
||
| 775 | } |
||
| 776 | |||
| 777 | /** |
||
| 778 | * 发放代金券 |
||
| 779 | * @ref https://pay.weixin.qq.com/wiki/doc/api/tools/sp_coupon.php?chapter=12_3&index=4 |
||
| 780 | * @param $coupon_stock_id |
||
| 781 | * @param $open_id |
||
| 782 | * @param $partner_trade_no |
||
| 783 | * @param string $op_user_id |
||
| 784 | * @param array $ext |
||
| 785 | * @return array |
||
| 786 | */ |
||
| 787 | public function sendCoupon($coupon_stock_id,$open_id,$partner_trade_no,$op_user_id = '',$ext = array()){ |
||
| 788 | $data = (!empty($ext) && is_array($ext))?$ext:array(); |
||
| 789 | $data["partner_trade_no"] = $partner_trade_no; |
||
| 790 | $data["coupon_stock_id"] = $coupon_stock_id; |
||
| 791 | $data["openid_count"] = 1; |
||
| 792 | $data["open_id"] = $open_id; |
||
| 793 | $data["op_user_id"] = $op_user_id; |
||
| 794 | $result = $this->post(self::URL_SEND_COUPON,$data,true); |
||
| 795 | return $result; |
||
| 796 | } |
||
| 797 | |||
| 798 | /** |
||
| 799 | * 查询代金券批次 |
||
| 800 | * @ref https://pay.weixin.qq.com/wiki/doc/api/tools/sp_coupon.php?chapter=12_4&index=5 |
||
| 801 | * @param $coupon_stock_id |
||
| 802 | * @param string $op_user_id |
||
| 803 | * @return array |
||
| 804 | */ |
||
| 805 | public function queryCouponStock($coupon_stock_id,$op_user_id = ''){ |
||
| 806 | $data = array(); |
||
| 807 | $data["coupon_stock_id"] = $coupon_stock_id; |
||
| 808 | $data["op_user_id"] = $op_user_id; |
||
| 809 | $result = $this->post(self::URL_QUERY_COUPON_STOCK,$data,false); |
||
| 810 | return $result; |
||
| 811 | } |
||
| 812 | |||
| 813 | /** |
||
| 814 | * 查询代金券信息 |
||
| 815 | * @ref https://pay.weixin.qq.com/wiki/doc/api/tools/sp_coupon.php?chapter=12_5&index=6 |
||
| 816 | * @param $coupon_id |
||
| 817 | * @param $open_id |
||
| 818 | * @param $stock_id |
||
| 819 | * @param string $op_user_id |
||
| 820 | * @param array $ext |
||
| 821 | * @return array |
||
| 822 | */ |
||
| 823 | public function queryCouponsInfo($coupon_id,$open_id,$stock_id,$op_user_id = '',$ext = array()){ |
||
| 824 | $data = (!empty($ext) && is_array($ext))?$ext:array(); |
||
| 825 | $data["coupon_id"] = $coupon_id; |
||
| 826 | $data["stock_id"] = $stock_id; |
||
| 827 | $data["open_id"] = $open_id; |
||
| 828 | $data["op_user_id"] = $op_user_id; |
||
| 829 | $result = $this->post(self::URL_QUERY_COUPON_INFO,$data,false); |
||
| 830 | return $result; |
||
| 831 | } |
||
| 832 | |||
| 833 | /** |
||
| 834 | * 获取RSA加密公钥 |
||
| 835 | * @ref https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_7&index=4 |
||
| 836 | * @param bool $refresh |
||
| 837 | * @return string |
||
| 838 | * @throws Exception |
||
| 839 | */ |
||
| 840 | public function getPublicKey($refresh = false){ |
||
| 841 | if (!$refresh && file_exists($this->config["rsa_pubkey_path"])) { |
||
| 842 | $pubkey = file_get_contents($this->config["rsa_pubkey_path"]); |
||
| 843 | return $pubkey; |
||
| 844 | } |
||
| 845 | $data = array(); |
||
| 846 | $data["mch_id"] = $this->config["mch_id"]; |
||
| 847 | $data["sign_type"] = $this->config["sign_type"]; |
||
| 848 | $result = $this->post(self::URL_GETPUBLICKEY,$data,true); |
||
| 849 | $pubkey = $result['pub_key']; |
||
| 850 | $pubkey = $this->convertPKCS1toPKCS8($pubkey); |
||
| 851 | $fp = fopen($this->config["rsa_pubkey_path"], "w"); |
||
| 852 | if ($fp) { |
||
|
|
|||
| 853 | fwrite($fp, $pubkey); |
||
| 854 | fclose($fp); |
||
| 855 | return $pubkey; |
||
| 856 | }else { |
||
| 857 | throw new Exception("RSA public key not found"); |
||
| 858 | } |
||
| 859 | } |
||
| 860 | |||
| 861 | private function convertPKCS1toPKCS8($pkcs1){ |
||
| 862 | $start_key = $pkcs1; |
||
| 863 | $start_key = str_replace('-----BEGIN RSA PUBLIC KEY-----', '', $start_key); |
||
| 864 | $start_key = trim(str_replace('-----END RSA PUBLIC KEY-----', '', $start_key)); |
||
| 865 | $key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A' . str_replace("\n", '', $start_key); |
||
| 866 | $key = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($key, 64, "\n", true) . "\n-----END PUBLIC KEY-----"; |
||
| 867 | return $key; |
||
| 868 | } |
||
| 869 | |||
| 870 | public function rsaEncrypt($data){ |
||
| 871 | $pubkey = $this->getPublicKey(); |
||
| 872 | $encrypted = null; |
||
| 873 | $pubkey = openssl_get_publickey($pubkey); |
||
| 874 | if (openssl_public_encrypt($data, $encrypted, $pubkey,OPENSSL_PKCS1_OAEP_PADDING)) |
||
| 875 | $data = base64_encode($encrypted); |
||
| 876 | else |
||
| 877 | throw new Exception('Unable to encrypt data'); |
||
| 878 | return $data; |
||
| 879 | } |
||
| 880 | |||
| 881 | /** |
||
| 882 | * 查询企业付款银行卡 |
||
| 883 | * @ref https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_3 |
||
| 884 | * @param $partner_trade_no |
||
| 885 | * @return array |
||
| 886 | */ |
||
| 887 | public function queryTransferBankCard($partner_trade_no){ |
||
| 888 | $data = array(); |
||
| 889 | $data["appid"] = $this->config["app_id"]; |
||
| 890 | $data["mch_id"] = $this->config["mch_id"]; |
||
| 891 | $data["partner_trade_no"] = $partner_trade_no; |
||
| 892 | $result = $this->post(self::URL_QUERY_TRANSFER_WALLET,$data,true); |
||
| 893 | return $result; |
||
| 894 | } |
||
| 895 | |||
| 896 | /** |
||
| 897 | * sandbox环境获取验签秘钥 |
||
| 898 | * @ref https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=23_1 |
||
| 899 | * @return array |
||
| 900 | */ |
||
| 901 | public function getSignKey(){ |
||
| 902 | $data = array(); |
||
| 903 | $data["mch_id"] = $this->config["mch_id"]; |
||
| 904 | $result = $this->post($this->getSignKeyUrl,$data,false); |
||
| 905 | return $result['sandbox_signkey']; |
||
| 906 | } |
||
| 907 | |||
| 908 | public function getSignPackage($url){ |
||
| 909 | $jsapiTicket = $this->getJSAPITicket(); |
||
| 910 | $timestamp = time(); |
||
| 911 | $nonceStr = $this->getNonceStr(); |
||
| 912 | $rawString = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; |
||
| 913 | $signature = sha1($rawString); |
||
| 914 | |||
| 915 | $signPackage = array( |
||
| 916 | "appId" => $this->config['app_id'], |
||
| 917 | "nonceStr" => $nonceStr, |
||
| 918 | "timestamp" => $timestamp, |
||
| 919 | "url" => $url, |
||
| 920 | "signature" => $signature, |
||
| 921 | "rawString" => $rawString |
||
| 922 | ); |
||
| 923 | return $signPackage; |
||
| 924 | } |
||
| 925 | |||
| 926 | public function getJSAPITicket(){ |
||
| 927 | if(isset($this->config['jsapi_ticket']) && file_exists($this->config['jsapi_ticket'])){ |
||
| 928 | $data = json_decode(file_get_contents($this->config['jsapi_ticket'])); |
||
| 929 | if (!$data || $data->expire_time < time()) { |
||
| 930 | $data = $this->getWechatOAuth()->getTicket(); |
||
| 931 | $fp = fopen($this->config["jsapi_ticket"], "w"); |
||
| 932 | fwrite($fp, $data); |
||
| 933 | if ($fp) fclose($fp); |
||
| 934 | $data = json_decode($data); |
||
| 935 | } |
||
| 936 | return $data->jsapi_ticket; |
||
| 937 | } |
||
| 938 | } |
||
| 939 | |||
| 940 | private function post($url, $data,$cert = true) { |
||
| 941 | if(!isset($data['mch_id']) && !isset($data['mchid'])) $data["mch_id"] = $this->config["mch_id"]; |
||
| 942 | if(!isset($data['nonce_str'])) $data["nonce_str"] = $this->getNonceStr(); |
||
| 943 | if(!isset($data['sign'])) $data['sign'] = $this->sign($data); |
||
| 944 | $this->requestXML = $this->responseXML = null; |
||
| 945 | $this->requestArray = $this->responseArray = null; |
||
| 946 | |||
| 947 | $this->requestArray = $data; |
||
| 948 | $this->requestXML = $this->array2xml($data); |
||
| 949 | $opts = [ |
||
| 950 | CURLOPT_SSL_VERIFYPEER => false, |
||
| 951 | CURLOPT_SSL_VERIFYHOST => false, |
||
| 952 | CURLOPT_RETURNTRANSFER => true, |
||
| 953 | CURLOPT_TIMEOUT => 10 |
||
| 954 | ]; |
||
| 955 | if($cert == true){ |
||
| 956 | $opts[CURLOPT_SSLCERTTYPE] = 'PEM'; |
||
| 957 | $opts[CURLOPT_SSLCERT] = $this->config['ssl_cert_path']; |
||
| 958 | $opts[CURLOPT_SSLKEYTYPE] = 'PEM'; |
||
| 959 | $opts[CURLOPT_SSLKEY] = $this->config['ssl_key_path']; |
||
| 960 | } |
||
| 961 | $processResponse = true; |
||
| 962 | if(in_array($url,[self::URL_DOWNLOADBILL])){ |
||
| 963 | $processResponse = false; |
||
| 964 | } |
||
| 965 | if($this->sandbox === true){ |
||
| 966 | $host = "https://api.mch.weixin.qq.com"; |
||
| 967 | $url = str_replace($host,'',$url); |
||
| 968 | $url = "{$host}/sandboxnew{$url}"; |
||
| 969 | } |
||
| 970 | $content = $this->httpClient->post($url,$this->requestXML,[],$opts); |
||
| 971 | if(!$content) throw new Exception("Empty response with {$this->requestXML}"); |
||
| 972 | |||
| 973 | $this->responseXML = $content; |
||
| 974 | if($processResponse) |
||
| 975 | return $this->processResponseXML($this->responseXML); |
||
| 976 | else return $this->responseXML; |
||
| 977 | |||
| 978 | } |
||
| 979 | |||
| 980 | private function processResponseXML($responseXML){ |
||
| 981 | $result = $this->xml2array($responseXML); |
||
| 982 | $this->responseArray = $result; |
||
| 983 | if(empty($result['return_code'])){ |
||
| 984 | throw new Exception("No return code presents in {$this->responseXML}"); |
||
| 985 | } |
||
| 986 | $this->returnCode = $result["return_code"]; |
||
| 987 | $this->returnMsg = isset($result['return_msg'])?$result['return_msg']:''; |
||
| 988 | |||
| 989 | if ($this->returnCode == "SUCCESS") { |
||
| 990 | if(isset($result['result_code']) && $result['result_code'] == "FAIL"){ |
||
| 991 | $this->resultCode = $result['result_code']; |
||
| 992 | $this->errCode = $result['err_code']; |
||
| 993 | $this->errCodeDes = $result['err_code_des']; |
||
| 994 | throw new Exception("[$this->errCode]$this->errCodeDes"); |
||
| 995 | }else{ |
||
| 996 | return $result; |
||
| 997 | } |
||
| 998 | } else { |
||
| 999 | if($result["return_code"] == "FAIL"){ |
||
| 1000 | throw new Exception($this->returnMsg); |
||
| 1001 | }else{ |
||
| 1002 | $this->resultCode = $result['result_code']; |
||
| 1003 | $this->errCode = $result['err_code']; |
||
| 1004 | $this->errCodeDes = $result['err_code_des']; |
||
| 1005 | throw new Exception("[$this->errCode]$this->errCodeDes"); |
||
| 1006 | } |
||
| 1007 | } |
||
| 1008 | } |
||
| 1009 | |||
| 1010 | public function sign($data,$sign_type = WechatPay::SIGNTYPE_MD5) { |
||
| 1011 | ksort($data); |
||
| 1012 | $string1 = ""; |
||
| 1013 | foreach ($data as $k => $v) { |
||
| 1014 | if ($v && trim($v)!='') { |
||
| 1015 | $string1 .= "$k=$v&"; |
||
| 1016 | } |
||
| 1017 | } |
||
| 1018 | $stringSignTemp = $string1 . "key=" . $this->config["api_key"]; |
||
| 1019 | if($sign_type == WechatPay::SIGNTYPE_MD5){ |
||
| 1020 | $sign = strtoupper(md5($stringSignTemp)); |
||
| 1021 | }elseif($sign_type == WechatPay::SIGNTYPE_HMACSHA256){ |
||
| 1022 | $sign = strtoupper(hash_hmac('sha256',$stringSignTemp,$this->config["api_key"])); |
||
| 1023 | }else throw new \Exception("Not supported sign type - $sign_type"); |
||
| 1024 | return $sign; |
||
| 1025 | } |
||
| 1026 | |||
| 1027 | private function array2xml($array) { |
||
| 1035 | } |
||
| 1036 | |||
| 1037 | private function xml2array($xml) { |
||
| 1038 | $array = array(); |
||
| 1039 | $tmp = array(); |
||
| 1040 | try{ |
||
| 1041 | $tmp = (array) simplexml_load_string($xml); |
||
| 1042 | }catch(\Exception $e){} |
||
| 1043 | foreach ( $tmp as $k => $v) { |
||
| 1044 | $array[$k] = (string) $v; |
||
| 1045 | } |
||
| 1046 | return $array; |
||
| 1047 | } |
||
| 1048 | |||
| 1049 | private function getNonceStr() { |
||
| 1050 | return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"),0,32); |
||
| 1051 | } |
||
| 1052 | |||
| 1053 | } |