WxPay.Api.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. <?php
  2. //require_once "WxPay.Exception.php";
  3. //require_once "WxPay.Config.php";
  4. //require_once "WxPay.Data.php";
  5. /**
  6. *
  7. * 接口访问类,包含所有微信支付API列表的封装,类中方法为static方法,
  8. * 每个接口有默认超时时间(除提交被扫支付为10s,上报超时时间为1s外,其他均为6s)
  9. * @author widyhu
  10. *
  11. */
  12. class WxPayApi
  13. {
  14. /**
  15. *
  16. * 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
  17. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  18. * @param WxPayUnifiedOrder $inputObj
  19. * @param int $timeOut
  20. * @throws WxPayException
  21. * @return 成功时返回,其他抛异常
  22. */
  23. public static function unifiedOrder($inputObj, $timeOut = 6, $key = '')
  24. {
  25. $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  26. //检测必填参数
  27. if(!$inputObj->IsOut_trade_noSet()) {
  28. throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
  29. }else if(!$inputObj->IsBodySet()){
  30. throw new WxPayException("缺少统一支付接口必填参数body!");
  31. }else if(!$inputObj->IsTotal_feeSet()) {
  32. throw new WxPayException("缺少统一支付接口必填参数total_fee!");
  33. }else if(!$inputObj->IsTrade_typeSet()) {
  34. throw new WxPayException("缺少统一支付接口必填参数trade_type!");
  35. }else if(!$inputObj->IsAppidSet()){
  36. throw new WxPayException("缺少统一支付接口必填参数appid!");
  37. }else if(!$inputObj->IsMch_idSet()){
  38. throw new WxPayException("缺少统一支付接口必填参数mch_id!");
  39. }else if(!$inputObj->IsNotify_urlSet()){
  40. throw new WxPayException("缺少统一支付接口必填参数notify_url!");
  41. }
  42. //关联参数
  43. if($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet()){
  44. throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
  45. }
  46. if($inputObj->GetTrade_type() == "NATIVE" && !$inputObj->IsProduct_idSet()){
  47. throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
  48. }
  49. //异步通知url未设置,则使用配置文件中的url
  50. // if(!$inputObj->IsNotify_urlSet()){
  51. // $inputObj->SetNotify_url(WxPayConfig::NOTIFY_URL);//异步通知url
  52. // }
  53. // $inputObj->SetAppid(WxPayConfig::APPID);//公众账号ID
  54. // $inputObj->SetMch_id(WxPayConfig::MCHID);//商户号
  55. $appid = $inputObj->GetAppid();
  56. $mch_id = $inputObj->GetMch_id();
  57. $inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']);//终端ip
  58. //$inputObj->SetSpbill_create_ip("1.1.1.1");
  59. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  60. //签名
  61. $inputObj->SetSign($key);
  62. $xml = $inputObj->ToXml();
  63. $startTimeStamp = self::getMillisecond();//请求开始时间
  64. $response = self::postXmlCurl($xml, $url, false, $timeOut);
  65. $result = WxPayResults::Init($response, $key);
  66. self::reportCostTime($url, $startTimeStamp, $result, $appid, $mch_id);//上报请求花费时间
  67. return $result;
  68. }
  69. /**
  70. *
  71. * 查询订单,WxPayOrderQuery中out_trade_no、transaction_id至少填一个
  72. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  73. * @param WxPayOrderQuery $inputObj
  74. * @param int $timeOut
  75. * @throws WxPayException
  76. * @return 成功时返回,其他抛异常
  77. */
  78. public static function orderQuery($inputObj, $timeOut = 6, $key = '')
  79. {
  80. $url = "https://api.mch.weixin.qq.com/pay/orderquery";
  81. //检测必填参数
  82. if(!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet()) {
  83. throw new WxPayException("订单查询接口中,out_trade_no、transaction_id至少填一个!");
  84. }else if(!$inputObj->IsAppidSet()){
  85. throw new WxPayException("缺少统一支付接口必填参数appid!");
  86. }else if(!$inputObj->IsMch_idSet()){
  87. throw new WxPayException("缺少统一支付接口必填参数mch_id!");
  88. }
  89. // $inputObj->SetAppid(WxPayConfig::APPID);//公众账号ID
  90. // $inputObj->SetMch_id(WxPayConfig::MCHID);//商户号
  91. $appid = $inputObj->GetAppid();
  92. $mch_id = $inputObj->GetMch_id();
  93. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  94. $inputObj->SetSign($key);//签名
  95. $xml = $inputObj->ToXml();
  96. $startTimeStamp = self::getMillisecond();//请求开始时间
  97. $response = self::postXmlCurl($xml, $url, false, $timeOut);
  98. $result = WxPayResults::Init($response, $key);
  99. self::reportCostTime($url, $startTimeStamp, $result, $appid, $mch_id);//上报请求花费时间
  100. return $result;
  101. }
  102. /**
  103. *
  104. * 关闭订单,WxPayCloseOrder中out_trade_no必填
  105. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  106. * @param WxPayCloseOrder $inputObj
  107. * @param int $timeOut
  108. * @throws WxPayException
  109. * @return 成功时返回,其他抛异常
  110. */
  111. public static function closeOrder($inputObj, $timeOut = 6, $key = '')
  112. {
  113. $url = "https://api.mch.weixin.qq.com/pay/closeorder";
  114. //检测必填参数
  115. if(!$inputObj->IsOut_trade_noSet()) {
  116. throw new WxPayException("订单查询接口中,out_trade_no必填!");
  117. }else if(!$inputObj->IsAppidSet()){
  118. throw new WxPayException("缺少统一支付接口必填参数appid!");
  119. }else if(!$inputObj->IsMch_idSet()){
  120. throw new WxPayException("缺少统一支付接口必填参数mch_id!");
  121. }
  122. // $inputObj->SetAppid(WxPayConfig::APPID);//公众账号ID
  123. // $inputObj->SetMch_id(WxPayConfig::MCHID);//商户号
  124. $appid = $inputObj->GetAppid();
  125. $mch_id = $inputObj->GetMch_id();
  126. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  127. $inputObj->SetSign($key);//签名
  128. $xml = $inputObj->ToXml();
  129. $startTimeStamp = self::getMillisecond();//请求开始时间
  130. $response = self::postXmlCurl($xml, $url, false, $timeOut);
  131. $result = WxPayResults::Init($response, $key);
  132. self::reportCostTime($url, $startTimeStamp, $result, $appid, $mch_id);//上报请求花费时间
  133. return $result;
  134. }
  135. /**
  136. *
  137. * 申请退款,WxPayRefund中out_trade_no、transaction_id至少填一个且
  138. * out_refund_no、total_fee、refund_fee、op_user_id为必填参数
  139. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  140. * @param WxPayRefund $inputObj
  141. * @param int $timeOut
  142. * @throws WxPayException
  143. * @return 成功时返回,其他抛异常
  144. */
  145. public static function refund($inputObj , $ssl_cert, $ssl_key, $timeOut = 6, $key = '')
  146. {
  147. $url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
  148. //检测必填参数
  149. if(!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet()) {
  150. throw new WxPayException("退款申请接口中,out_trade_no、transaction_id至少填一个!");
  151. }else if(!$inputObj->IsOut_refund_noSet()){
  152. throw new WxPayException("退款申请接口中,缺少必填参数out_refund_no!");
  153. }else if(!$inputObj->IsTotal_feeSet()){
  154. throw new WxPayException("退款申请接口中,缺少必填参数total_fee!");
  155. }else if(!$inputObj->IsRefund_feeSet()){
  156. throw new WxPayException("退款申请接口中,缺少必填参数refund_fee!");
  157. }else if(!$inputObj->IsOp_user_idSet()){
  158. throw new WxPayException("退款申请接口中,缺少必填参数op_user_id!");
  159. }else if(!$inputObj->IsAppidSet()){
  160. throw new WxPayException("缺少统一支付接口必填参数appid!");
  161. }else if(!$inputObj->IsMch_idSet()){
  162. throw new WxPayException("缺少统一支付接口必填参数mch_id!");
  163. }else if(empty($ssl_cert)){
  164. throw new WxPayException("缺少统一支付接口必填参数ssl_cert_path!");
  165. }else if(empty($ssl_key)){
  166. throw new WxPayException("缺少统一支付接口必填参数ssl_key_path!");
  167. }
  168. // $inputObj->SetAppid(WxPayConfig::APPID);//公众账号ID
  169. // $inputObj->SetMch_id(WxPayConfig::MCHID);//商户号
  170. $appid = $inputObj->GetAppid();
  171. $mch_id = $inputObj->GetMch_id();
  172. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  173. $inputObj->SetSign($key);//签名
  174. $xml = $inputObj->ToXml();
  175. $startTimeStamp = self::getMillisecond();//请求开始时间
  176. $response = self::postXmlCurl($xml, $url, true, $timeOut, $ssl_cert, $ssl_key);
  177. $result = WxPayResults::Init($response, $key);
  178. self::reportCostTime($url, $startTimeStamp, $result,$appid,$mch_id);//上报请求花费时间
  179. return $result;
  180. }
  181. /**
  182. *
  183. * 查询退款
  184. * 提交退款申请后,通过调用该接口查询退款状态。退款有一定延时,
  185. * 用零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。
  186. * WxPayRefundQuery中out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个
  187. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  188. * @param WxPayRefundQuery $inputObj
  189. * @param int $timeOut
  190. * @throws WxPayException
  191. * @return 成功时返回,其他抛异常
  192. */
  193. public static function refundQuery($inputObj, $timeOut = 6, $key = '')
  194. {
  195. $url = "https://api.mch.weixin.qq.com/pay/refundquery";
  196. //检测必填参数
  197. if(!$inputObj->IsOut_refund_noSet() &&
  198. !$inputObj->IsOut_trade_noSet() &&
  199. !$inputObj->IsTransaction_idSet() &&
  200. !$inputObj->IsRefund_idSet()) {
  201. throw new WxPayException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!");
  202. }else if(!$inputObj->IsAppidSet()){
  203. throw new WxPayException("缺少统一支付接口必填参数appid!");
  204. }else if(!$inputObj->IsMch_idSet()){
  205. throw new WxPayException("缺少统一支付接口必填参数mch_id!");
  206. }
  207. // $inputObj->SetAppid(WxPayConfig::APPID);//公众账号ID
  208. // $inputObj->SetMch_id(WxPayConfig::MCHID);//商户号
  209. $appid = $inputObj->GetAppid();
  210. $mch_id = $inputObj->GetMch_id();
  211. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  212. $inputObj->SetSign($key);//签名
  213. $xml = $inputObj->ToXml();
  214. $startTimeStamp = self::getMillisecond();//请求开始时间
  215. $response = self::postXmlCurl($xml, $url, false, $timeOut);
  216. $result = WxPayResults::Init($response, $key);
  217. self::reportCostTime($url, $startTimeStamp, $result,$appid,$mch_id);//上报请求花费时间
  218. return $result;
  219. }
  220. /**
  221. * 下载对账单,WxPayDownloadBill中bill_date为必填参数
  222. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  223. * @param WxPayDownloadBill $inputObj
  224. * @param int $timeOut
  225. * @throws WxPayException
  226. * @return 成功时返回,其他抛异常
  227. */
  228. public static function downloadBill($inputObj, $timeOut = 6, $key = '')
  229. {
  230. $url = "https://api.mch.weixin.qq.com/pay/downloadbill";
  231. //检测必填参数
  232. if(!$inputObj->IsBill_dateSet()) {
  233. throw new WxPayException("对账单接口中,缺少必填参数bill_date!");
  234. }else if(!$inputObj->IsAppidSet()){
  235. throw new WxPayException("缺少统一支付接口必填参数appid!");
  236. }else if(!$inputObj->IsMch_idSet()){
  237. throw new WxPayException("缺少统一支付接口必填参数mch_id!");
  238. }
  239. // $inputObj->SetAppid(WxPayConfig::APPID);//公众账号ID
  240. // $inputObj->SetMch_id(WxPayConfig::MCHID);//商户号
  241. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  242. $inputObj->SetSign($key);//签名
  243. $xml = $inputObj->ToXml();
  244. $response = self::postXmlCurl($xml, $url, false, $timeOut);
  245. if(substr($response, 0 , 5) == "<xml>"){
  246. return "";
  247. }
  248. return $response;
  249. }
  250. /**
  251. * 提交被扫支付API
  252. * 收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台,
  253. * 由商户收银台或者商户后台调用该接口发起支付。
  254. * WxPayWxPayMicroPay中body、out_trade_no、total_fee、auth_code参数必填
  255. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  256. * @param WxPayWxPayMicroPay $inputObj
  257. * @param int $timeOut
  258. */
  259. public static function micropay($inputObj, $timeOut = 10, $key = '')
  260. {
  261. $url = "https://api.mch.weixin.qq.com/pay/micropay";
  262. //检测必填参数
  263. if(!$inputObj->IsBodySet()) {
  264. throw new WxPayException("提交被扫支付API接口中,缺少必填参数body!");
  265. } else if(!$inputObj->IsOut_trade_noSet()) {
  266. throw new WxPayException("提交被扫支付API接口中,缺少必填参数out_trade_no!");
  267. } else if(!$inputObj->IsTotal_feeSet()) {
  268. throw new WxPayException("提交被扫支付API接口中,缺少必填参数total_fee!");
  269. } else if(!$inputObj->IsAuth_codeSet()) {
  270. throw new WxPayException("提交被扫支付API接口中,缺少必填参数auth_code!");
  271. }else if(!$inputObj->IsAppidSet()){
  272. throw new WxPayException("缺少统一支付接口必填参数appid!");
  273. }else if(!$inputObj->IsMch_idSet()){
  274. throw new WxPayException("缺少统一支付接口必填参数mch_id!");
  275. }
  276. $inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']);//终端ip
  277. // $inputObj->SetAppid(WxPayConfig::APPID);//公众账号ID
  278. // $inputObj->SetMch_id(WxPayConfig::MCHID);//商户号
  279. $appid = $inputObj->GetAppid();
  280. $mch_id = $inputObj->GetMch_id();
  281. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  282. $inputObj->SetSign($key);//签名
  283. $xml = $inputObj->ToXml();
  284. $startTimeStamp = self::getMillisecond();//请求开始时间
  285. $response = self::postXmlCurl($xml, $url, false, $timeOut);
  286. $result = WxPayResults::Init($response, $key);
  287. self::reportCostTime($url, $startTimeStamp, $result,$appid,$mch_id);//上报请求花费时间
  288. return $result;
  289. }
  290. /**
  291. *
  292. * 撤销订单API接口,WxPayReverse中参数out_trade_no和transaction_id必须填写一个
  293. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  294. * @param WxPayReverse $inputObj
  295. * @param int $timeOut
  296. * @throws WxPayException
  297. */
  298. public static function reverse($inputObj, $ssl_cert, $ssl_key, $timeOut = 6, $key = '')
  299. {
  300. $url = "https://api.mch.weixin.qq.com/secapi/pay/reverse";
  301. //检测必填参数
  302. if(!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet()) {
  303. throw new WxPayException("撤销订单API接口中,参数out_trade_no和transaction_id必须填写一个!");
  304. }else if(!$inputObj->IsAppidSet()){
  305. throw new WxPayException("缺少统一支付接口必填参数appid!");
  306. }else if(!$inputObj->IsMch_idSet()){
  307. throw new WxPayException("缺少统一支付接口必填参数mch_id!");
  308. }else if(empty($ssl_cert)){
  309. throw new WxPayException("缺少统一支付接口必填参数ssl_cert_path!");
  310. }else if(empty($ssl_key)){
  311. throw new WxPayException("缺少统一支付接口必填参数ssl_key_path!");
  312. }
  313. // $inputObj->SetAppid(WxPayConfig::APPID);//公众账号ID
  314. // $inputObj->SetMch_id(WxPayConfig::MCHID);//商户号
  315. $appid = $inputObj->GetAppid();
  316. $mch_id = $inputObj->GetMch_id();
  317. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  318. $inputObj->SetSign($key);//签名
  319. $xml = $inputObj->ToXml();
  320. $startTimeStamp = self::getMillisecond();//请求开始时间
  321. $response = self::postXmlCurl($xml, $url, true, $timeOut, $ssl_cert, $ssl_key);
  322. $result = WxPayResults::Init($response, $key);
  323. self::reportCostTime($url, $startTimeStamp, $result,$appid,$mch_id);//上报请求花费时间
  324. return $result;
  325. }
  326. /**
  327. *
  328. * 测速上报,该方法内部封装在report中,使用时请注意异常流程
  329. * WxPayReport中interface_url、return_code、result_code、user_ip、execute_time_必填
  330. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  331. * @param WxPayReport $inputObj
  332. * @param int $timeOut
  333. * @throws WxPayException
  334. * @return 成功时返回,其他抛异常
  335. */
  336. public static function report($inputObj, $timeOut = 1, $key = '')
  337. {
  338. $url = "https://api.mch.weixin.qq.com/payitil/report";
  339. //检测必填参数
  340. if(!$inputObj->IsInterface_urlSet()) {
  341. throw new WxPayException("接口URL,缺少必填参数interface_url!");
  342. } if(!$inputObj->IsReturn_codeSet()) {
  343. throw new WxPayException("返回状态码,缺少必填参数return_code!");
  344. } if(!$inputObj->IsResult_codeSet()) {
  345. throw new WxPayException("业务结果,缺少必填参数result_code!");
  346. } if(!$inputObj->IsUser_ipSet()) {
  347. throw new WxPayException("访问接口IP,缺少必填参数user_ip!");
  348. } if(!$inputObj->IsExecute_time_Set()) {
  349. throw new WxPayException("接口耗时,缺少必填参数execute_time_!");
  350. }else if(!$inputObj->IsAppidSet()){
  351. throw new WxPayException("缺少统一支付接口必填参数appid!");
  352. }else if(!$inputObj->IsMch_idSet()){
  353. throw new WxPayException("缺少统一支付接口必填参数mch_id!");
  354. }
  355. // $inputObj->SetAppid(WxPayConfig::APPID);//公众账号ID
  356. // $inputObj->SetMch_id(WxPayConfig::MCHID);//商户号
  357. $inputObj->SetUser_ip($_SERVER['REMOTE_ADDR']);//终端ip
  358. $inputObj->SetTime(date("YmdHis"));//商户上报时间
  359. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  360. $inputObj->SetSign($key);//签名
  361. $xml = $inputObj->ToXml();
  362. $startTimeStamp = self::getMillisecond();//请求开始时间
  363. $response = self::postXmlCurl($xml, $url, false, $timeOut);
  364. return $response;
  365. }
  366. /**
  367. *
  368. * 生成二维码规则,模式一生成支付二维码
  369. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  370. * @param WxPayBizPayUrl $inputObj
  371. * @param int $timeOut
  372. * @throws WxPayException
  373. * @return 成功时返回,其他抛异常
  374. */
  375. public static function bizpayurl($inputObj, $timeOut = 6, $key = '')
  376. {
  377. if(!$inputObj->IsProduct_idSet()){
  378. throw new WxPayException("生成二维码,缺少必填参数product_id!");
  379. }else if(!$inputObj->IsAppidSet()){
  380. throw new WxPayException("缺少统一支付接口必填参数appid!");
  381. }else if(!$inputObj->IsMch_idSet()){
  382. throw new WxPayException("缺少统一支付接口必填参数mch_id!");
  383. }
  384. // $inputObj->SetAppid(WxPayConfig::APPID);//公众账号ID
  385. // $inputObj->SetMch_id(WxPayConfig::MCHID);//商户号
  386. $inputObj->SetTime_stamp(time());//时间戳
  387. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  388. $inputObj->SetSign($key);//签名
  389. return $inputObj->GetValues();
  390. }
  391. /**
  392. *
  393. * 转换短链接
  394. * 该接口主要用于扫码原生支付模式一中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX),
  395. * 减小二维码数据量,提升扫描速度和精确度。
  396. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  397. * @param WxPayShortUrl $inputObj
  398. * @param int $timeOut
  399. * @throws WxPayException
  400. * @return 成功时返回,其他抛异常
  401. */
  402. public static function shorturl($inputObj, $timeOut = 6, $key = '')
  403. {
  404. $url = "https://api.mch.weixin.qq.com/tools/shorturl";
  405. //检测必填参数
  406. if(!$inputObj->IsLong_urlSet()) {
  407. throw new WxPayException("需要转换的URL,签名用原串,传输需URL encode!");
  408. }else if(!$inputObj->IsAppidSet()){
  409. throw new WxPayException("缺少统一支付接口必填参数appid!");
  410. }else if(!$inputObj->IsMch_idSet()){
  411. throw new WxPayException("缺少统一支付接口必填参数mch_id!");
  412. }
  413. // $inputObj->SetAppid(WxPayConfig::APPID);//公众账号ID
  414. // $inputObj->SetMch_id(WxPayConfig::MCHID);//商户号
  415. $appid = $inputObj->GetAppid();
  416. $mch_id = $inputObj->GetMch_id();
  417. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  418. $inputObj->SetSign($key);//签名
  419. $xml = $inputObj->ToXml();
  420. $startTimeStamp = self::getMillisecond();//请求开始时间
  421. $response = self::postXmlCurl($xml, $url, false, $timeOut);
  422. $result = WxPayResults::Init($response, $key);
  423. self::reportCostTime($url, $startTimeStamp, $result,$appid,$mch_id);//上报请求花费时间
  424. return $result;
  425. }
  426. /**
  427. *
  428. * 支付结果通用通知
  429. * @param function $callback
  430. * 直接回调函数使用方法: notify(you_function);
  431. * 回调类成员函数方法:notify(array($this, you_function));
  432. * $callback 原型为:function function_name($data){}
  433. */
  434. public static function notify($callback, &$msg, $key = '')
  435. {
  436. //获取通知的数据
  437. $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
  438. //如果返回成功则验证签名
  439. try {
  440. $result = WxPayResults::Init($xml, $key);
  441. } catch (WxPayException $e){
  442. $msg = $e->errorMessage();
  443. return false;
  444. }
  445. return call_user_func($callback, $result);
  446. }
  447. /**
  448. *
  449. * 产生随机字符串,不长于32位
  450. * @param int $length
  451. * @return 产生的随机字符串
  452. */
  453. public static function getNonceStr($length = 32)
  454. {
  455. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  456. $str ="";
  457. for ( $i = 0; $i < $length; $i++ ) {
  458. $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
  459. }
  460. return $str;
  461. }
  462. /**
  463. * 直接输出xml
  464. * @param string $xml
  465. */
  466. public static function replyNotify($xml)
  467. {
  468. echo $xml;
  469. }
  470. /**
  471. *
  472. * 上报数据, 上报的时候将屏蔽所有异常流程
  473. * @param string $usrl
  474. * @param int $startTimeStamp
  475. * @param array $data
  476. */
  477. private static function reportCostTime($url, $startTimeStamp, $data, $appid, $mch_id)
  478. {
  479. //如果不需要上报数据
  480. if(WxPayConfig::REPORT_LEVENL == 0){
  481. return;
  482. }
  483. //如果仅失败上报
  484. if(WxPayConfig::REPORT_LEVENL == 1 &&
  485. array_key_exists("return_code", $data) &&
  486. $data["return_code"] == "SUCCESS" &&
  487. array_key_exists("result_code", $data) &&
  488. $data["result_code"] == "SUCCESS")
  489. {
  490. return;
  491. }
  492. //上报逻辑
  493. $endTimeStamp = self::getMillisecond();
  494. $objInput = new WxPayReport();
  495. $objInput->SetInterface_url($url);
  496. $objInput->SetExecute_time_($endTimeStamp - $startTimeStamp);
  497. $objInput->SetAppid($appid);
  498. $objInput->SetMch_id($mch_id);
  499. $objInput->SetExecute_time_($endTimeStamp - $startTimeStamp);
  500. //返回状态码
  501. if(array_key_exists("return_code", $data)){
  502. $objInput->SetReturn_code($data["return_code"]);
  503. }
  504. //返回信息
  505. if(array_key_exists("return_msg", $data)){
  506. $objInput->SetReturn_msg($data["return_msg"]);
  507. }
  508. //业务结果
  509. if(array_key_exists("result_code", $data)){
  510. $objInput->SetResult_code($data["result_code"]);
  511. }
  512. //错误代码
  513. if(array_key_exists("err_code", $data)){
  514. $objInput->SetErr_code($data["err_code"]);
  515. }
  516. //错误代码描述
  517. if(array_key_exists("err_code_des", $data)){
  518. $objInput->SetErr_code_des($data["err_code_des"]);
  519. }
  520. //商户订单号
  521. if(array_key_exists("out_trade_no", $data)){
  522. $objInput->SetOut_trade_no($data["out_trade_no"]);
  523. }
  524. //设备号
  525. if(array_key_exists("device_info", $data)){
  526. $objInput->SetDevice_info($data["device_info"]);
  527. }
  528. try{
  529. self::report($objInput);
  530. } catch (WxPayException $e){
  531. //不做任何处理
  532. }
  533. }
  534. /**
  535. * 以post方式提交xml到对应的接口url
  536. *
  537. * @param string $xml 需要post的xml数据
  538. * @param string $url url
  539. * @param bool $useCert 是否需要证书,默认不需要
  540. * @param int $second url执行超时时间,默认30s
  541. * @param string $ssl_cert 证书地址
  542. * @param string $ssl_key 证书key地址
  543. * @return mixed
  544. * @throws WxPayException
  545. */
  546. private static function postXmlCurl($xml, $url, $useCert = false, $second = 30, $ssl_cert = '', $ssl_key = '')
  547. {
  548. $ch = curl_init();
  549. //设置超时
  550. curl_setopt($ch, CURLOPT_TIMEOUT, $second);
  551. //如果有配置代理这里就设置代理
  552. if(WxPayConfig::CURL_PROXY_HOST != "0.0.0.0"
  553. && WxPayConfig::CURL_PROXY_PORT != 0){
  554. curl_setopt($ch,CURLOPT_PROXY, WxPayConfig::CURL_PROXY_HOST);
  555. curl_setopt($ch,CURLOPT_PROXYPORT, WxPayConfig::CURL_PROXY_PORT);
  556. }
  557. curl_setopt($ch,CURLOPT_URL, $url);
  558. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
  559. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验
  560. //设置header
  561. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  562. //要求结果为字符串且输出到屏幕上
  563. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  564. if($useCert == true){
  565. //设置证书
  566. //使用证书:cert 与 key 分别属于两个.pem文件
  567. curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
  568. curl_setopt($ch,CURLOPT_SSLCERT, $ssl_cert);
  569. curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
  570. curl_setopt($ch,CURLOPT_SSLKEY, $ssl_key);
  571. }
  572. //post提交方式
  573. curl_setopt($ch, CURLOPT_POST, TRUE);
  574. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  575. //运行curl
  576. $data = curl_exec($ch);
  577. //返回结果
  578. if($data){
  579. curl_close($ch);
  580. return $data;
  581. } else {
  582. $error = curl_errno($ch);
  583. curl_close($ch);
  584. throw new WxPayException("curl出错,错误码:$error");
  585. }
  586. }
  587. /**
  588. * 获取毫秒级别的时间戳
  589. */
  590. private static function getMillisecond()
  591. {
  592. //获取毫秒的时间戳
  593. $time = explode ( " ", microtime () );
  594. $time = $time[1] . ($time[0] * 1000);
  595. $time2 = explode( ".", $time );
  596. $time = $time2[0];
  597. return $time;
  598. }
  599. /**
  600. *
  601. * 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
  602. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  603. * @param WxPayUnifiedOrder $inputObj
  604. * @param int $timeOut
  605. * @throws WxPayException
  606. * @return 成功时返回,其他抛异常
  607. */
  608. public static function unifiedOrder1($inputObj){}
  609. /**
  610. * 微信企业支付
  611. * @param WxPayTransaction $inputObj
  612. * @param int $timeOut
  613. * @return array 支付结果
  614. * @throws WxPayException
  615. */
  616. public static function transferToDib($inputObj, $ssl_cert, $ssl_key, $timeOut = 6, $key){
  617. $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
  618. //检测必填参数
  619. if(!$inputObj->IsPartnerTradeNoSet()) {
  620. throw new WxPayException("缺少微信企业支付接口必填参数partner_trade_no!");
  621. }
  622. if(!$inputObj->IsOpenidSet()){
  623. throw new WxPayException("缺少微信企业支付接口必填参数openid!");
  624. }
  625. if(!$inputObj->IsAmountSet()) {
  626. throw new WxPayException("缺少微信企业支付接口必填参数amount!");
  627. }
  628. if(!$inputObj->IsCheckNameSet()) {
  629. throw new WxPayException("缺少微信企业支付接口必填参数check_name!");
  630. }
  631. if(!$inputObj->IsDescSet()) {
  632. throw new WxPayException("缺少微信企业支付接口必填参数desc!");
  633. }
  634. if(!$inputObj->IsMchAppid()){
  635. throw new WxPayException("缺少微信企业支付接口必填参数mch_appid!");
  636. }
  637. if(!$inputObj->IsMchId()){
  638. throw new WxPayException("缺少微信企业支付接口必填参数mchid!");
  639. }
  640. if(empty($ssl_cert)){
  641. throw new WxPayException("缺少微信企业支付接口必填参数ssl_cert_path!");
  642. }
  643. if(empty($ssl_key)){
  644. throw new WxPayException("缺少微信企业支付接口必填参数ssl_key_path!");
  645. }
  646. //关联参数
  647. if($inputObj->GetCheckName() == "FORCE_CHECK" && !$inputObj->IsReUserNameSet()){
  648. throw new WxPayException("微信企业支付接口中,check_name为FORCE_CHECK时,re_user_name为必填参数!");
  649. }
  650. // $inputObj->SetMchAppid(WxPayConfig::APPID);//公众账号ID
  651. // $inputObj->SetMchId(WxPayConfig::MCHID);//商户号
  652. $appid = $inputObj->GetMchAppid();
  653. $mch_id = $inputObj->GetMchId();
  654. $inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']);//终端ip
  655. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  656. //生成签名
  657. $inputObj->SetSign($key);
  658. $xml = $inputObj->ToXml();
  659. $startTimeStamp = self::getMillisecond();//请求开始时间
  660. $response = self::postXmlCurl($xml, $url, true, $timeOut, $ssl_cert, $ssl_key);
  661. $result = $inputObj->FromXml($response);
  662. self::reportCostTime($url, $startTimeStamp, $result,$appid,$mch_id);//上报请求花费时间
  663. return $result;
  664. }
  665. /**
  666. * 微信企业支付
  667. * @param WxPayPayBank $inputObj
  668. * @param int $timeOut
  669. * @return array 支付结果
  670. * @throws WxPayException
  671. */
  672. public static function PayBankToDib($inputObj, $ssl_cert, $ssl_key, $timeOut = 6, $key){
  673. $url = 'https://api.mch.weixin.qq.com/mmpaysptrans/pay_bank';
  674. //检测必填参数
  675. if(!$inputObj->IsMchId()){
  676. throw new WxPayException("缺少微信企业支付接口必填参数mchid!");
  677. }
  678. if(!$inputObj->IsPartnerTradeNoSet()) {
  679. throw new WxPayException("缺少微信企业支付接口必填参数partner_trade_no!");
  680. }
  681. if(!$inputObj->IsAmountSet()) {
  682. throw new WxPayException("缺少微信企业支付接口必填参数amount!");
  683. }
  684. if(!$inputObj->IsEncBankNoSet()) {
  685. throw new WxPayException("缺少微信企业支付接口必填参数enc_bank_no!");
  686. }
  687. if(!$inputObj->IsEncTrueNameSet()) {
  688. throw new WxPayException("缺少微信企业支付接口必填参数enc_true_name!");
  689. }
  690. if(!$inputObj->IsBankCodeSet()) {
  691. throw new WxPayException("缺少微信企业支付接口必填参数bank_code!");
  692. }
  693. if(empty($ssl_cert)){
  694. throw new WxPayException("缺少微信企业支付接口必填参数ssl_cert_path!");
  695. }
  696. if(empty($ssl_key)){
  697. throw new WxPayException("缺少微信企业支付接口必填参数ssl_key_path!");
  698. }
  699. // $inputObj->SetMchAppid(WxPayConfig::APPID);//公众账号ID
  700. // $inputObj->SetMchId(WxPayConfig::MCHID);//商户号
  701. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  702. //生成签名
  703. $inputObj->SetSign($key);
  704. $xml = $inputObj->ToXml();
  705. $response = self::postXmlCurl($xml, $url, true, $timeOut, $ssl_cert, $ssl_key);
  706. $result = $inputObj->FromXml($response);
  707. return $result;
  708. }
  709. /**
  710. * @param \WxPublicKeyByBank $inputObj
  711. * @param $ssl_cert
  712. * @param $ssl_key
  713. * @param int $timeOut
  714. * @param $key
  715. * @return mixed
  716. * @throws WxPayException
  717. */
  718. public static function get_rsa_public_key($inputObj, $ssl_cert, $ssl_key, $timeOut = 6, $key){
  719. $url = 'https://fraud.mch.weixin.qq.com/risk/getpublickey';
  720. //检测必填参数
  721. if(!$inputObj->IsMchId()){
  722. throw new WxPayException("缺少微信企业支付接口必填参数mchid!");
  723. }
  724. if(empty($ssl_cert)){
  725. throw new WxPayException("缺少微信企业支付接口必填参数ssl_cert_path!");
  726. }
  727. if(empty($ssl_key)){
  728. throw new WxPayException("缺少微信企业支付接口必填参数ssl_key_path!");
  729. }
  730. // $inputObj->SetMchAppid(WxPayConfig::APPID);//公众账号ID
  731. // $inputObj->SetMchId(WxPayConfig::MCHID);//商户号
  732. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  733. //生成签名
  734. $inputObj->SetSign($key);
  735. $xml = $inputObj->ToXml();
  736. var_dump($xml);
  737. $response = self::postXmlCurl($xml, $url, true, $timeOut, $ssl_cert, $ssl_key);
  738. $result = $inputObj->FromXml($response);
  739. return $result;
  740. }
  741. }