WxPayApi.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. <?php
  2. namespace app\common\services\wechat\lib;
  3. /**
  4. *
  5. * 接口访问类,包含所有微信支付API列表的封装,类中方法为static方法,
  6. * 每个接口有默认超时时间(除提交被扫支付为10s,上报超时时间为1s外,其他均为6s)
  7. * @author widyhu
  8. *
  9. */
  10. class WxPayApi
  11. {
  12. /**
  13. * SDK版本号
  14. * @var string
  15. */
  16. public static $VERSION = "3.0.10";
  17. /**
  18. *
  19. * 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
  20. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  21. * @param WxPayConfig $config 配置对象
  22. * @param WxPayUnifiedOrder $inputObj
  23. * @param int $timeOut
  24. * @throws WxPayException
  25. * @return array 成功时返回,其他抛异常
  26. */
  27. public static function unifiedOrder($config, $inputObj, $timeOut = 6)
  28. {
  29. $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  30. //检测必填参数
  31. if(!$inputObj->IsOut_trade_noSet()) {
  32. throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
  33. }else if(!$inputObj->IsBodySet()){
  34. throw new WxPayException("缺少统一支付接口必填参数body!");
  35. }else if(!$inputObj->IsTotal_feeSet()) {
  36. throw new WxPayException("缺少统一支付接口必填参数total_fee!");
  37. }else if(!$inputObj->IsTrade_typeSet()) {
  38. throw new WxPayException("缺少统一支付接口必填参数trade_type!");
  39. }
  40. //关联参数
  41. if($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet()){
  42. //throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
  43. }
  44. if($inputObj->GetTrade_type() == "NATIVE" && !$inputObj->IsProduct_idSet()){
  45. throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
  46. }
  47. //异步通知url未设置,则使用配置文件中的url
  48. if(!$inputObj->IsNotify_urlSet() && $config->GetNotifyUrl() != ""){
  49. $inputObj->SetNotify_url($config->GetNotifyUrl());//异步通知url
  50. }
  51. $inputObj->SetProfit_sharing($config->GetProfitSharing());//是否开启分账
  52. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  53. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  54. $inputObj->SetSub_appid($config->GetSubAppId());//公众账号ID
  55. $inputObj->SetSub_Mch_id($config->GetSubMerchantId());//商户号
  56. $inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']);//终端ip
  57. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  58. //签名
  59. $inputObj->SetSign($config);
  60. $xml = $inputObj->ToXml();
  61. $startTimeStamp = self::getMillisecond();//请求开始时间
  62. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  63. $result = WxPayResults::Init($config, $response);
  64. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  65. return $result;
  66. }
  67. /**
  68. *
  69. * 查询订单,WxPayOrderQuery中out_trade_no、transaction_id至少填一个
  70. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  71. * @param WxPayConfig $config 配置对象
  72. * @param WxPayOrderQuery $inputObj
  73. * @param int $timeOut
  74. * @throws WxPayException
  75. * @return array 成功时返回,其他抛异常
  76. */
  77. public static function orderQuery($config, $inputObj, $timeOut = 6)
  78. {
  79. $url = "https://api.mch.weixin.qq.com/pay/orderquery";
  80. //检测必填参数
  81. if(!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet()) {
  82. throw new WxPayException("订单查询接口中,out_trade_no、transaction_id至少填一个!");
  83. }
  84. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  85. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  86. $inputObj->SetSub_appid($config->GetSubAppId());//子商户公众账号ID
  87. $inputObj->SetSub_mch_id($config->GetSubMerchantId());//子商户号
  88. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  89. $inputObj->SetSign($config);//签名
  90. $xml = $inputObj->ToXml();
  91. $startTimeStamp = self::getMillisecond();//请求开始时间
  92. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  93. $result = WxPayResults::Init($config, $response);
  94. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  95. return $result;
  96. }
  97. /**
  98. *
  99. * 关闭订单,WxPayCloseOrder中out_trade_no必填
  100. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  101. * @param WxPayConfigInterface $config 配置对象
  102. * @param WxPayCloseOrder $inputObj
  103. * @param int $timeOut
  104. * @throws WxPayException
  105. * @return 成功时返回,其他抛异常
  106. */
  107. public static function closeOrder($config, $inputObj, $timeOut = 6)
  108. {
  109. $url = "https://api.mch.weixin.qq.com/pay/closeorder";
  110. //检测必填参数
  111. if(!$inputObj->IsOut_trade_noSet()) {
  112. throw new WxPayException("订单查询接口中,out_trade_no必填!");
  113. }
  114. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  115. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  116. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  117. $inputObj->SetSign($config);//签名
  118. $xml = $inputObj->ToXml();
  119. $startTimeStamp = self::getMillisecond();//请求开始时间
  120. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  121. $result = WxPayResults::Init($config, $response);
  122. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  123. return $result;
  124. }
  125. /**
  126. *
  127. * 申请退款,WxPayRefund中out_trade_no、transaction_id至少填一个且
  128. * out_refund_no、total_fee、refund_fee、op_user_id为必填参数
  129. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  130. * @param WxPayConfigInterface $config 配置对象
  131. * @param WxPayRefund $inputObj
  132. * @param int $timeOut
  133. * @throws WxPayException
  134. * @return 成功时返回,其他抛异常
  135. */
  136. public static function refund($config, $inputObj, $timeOut = 6)
  137. {
  138. $url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
  139. //检测必填参数
  140. if(!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet()) {
  141. throw new WxPayException("退款申请接口中,out_trade_no、transaction_id至少填一个!");
  142. }else if(!$inputObj->IsOut_refund_noSet()){
  143. throw new WxPayException("退款申请接口中,缺少必填参数out_refund_no!");
  144. }else if(!$inputObj->IsTotal_feeSet()){
  145. throw new WxPayException("退款申请接口中,缺少必填参数total_fee!");
  146. }else if(!$inputObj->IsRefund_feeSet()){
  147. throw new WxPayException("退款申请接口中,缺少必填参数refund_fee!");
  148. }
  149. // else if(!$inputObj->IsOp_user_idSet()){
  150. // throw new WxPayException("退款申请接口中,缺少必填参数op_user_id!");
  151. // }
  152. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  153. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  154. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  155. $inputObj->SetSign($config);//签名
  156. $xml = $inputObj->ToXml();
  157. $startTimeStamp = self::getMillisecond();//请求开始时间
  158. $response = self::postXmlCurl($config, $xml, $url, true, $timeOut);
  159. $result = WxPayResults::Init($config, $response);
  160. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  161. return $result;
  162. }
  163. /**
  164. *
  165. * 查询退款
  166. * 提交退款申请后,通过调用该接口查询退款状态。退款有一定延时,
  167. * 用零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。
  168. * WxPayRefundQuery中out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个
  169. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  170. * @param WxPayConfigInterface $config 配置对象
  171. * @param WxPayRefundQuery $inputObj
  172. * @param int $timeOut
  173. * @throws WxPayException
  174. * @return 成功时返回,其他抛异常
  175. */
  176. public static function refundQuery($config, $inputObj, $timeOut = 6)
  177. {
  178. $url = "https://api.mch.weixin.qq.com/pay/refundquery";
  179. //检测必填参数
  180. if(!$inputObj->IsOut_refund_noSet() &&
  181. !$inputObj->IsOut_trade_noSet() &&
  182. !$inputObj->IsTransaction_idSet() &&
  183. !$inputObj->IsRefund_idSet()) {
  184. throw new WxPayException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!");
  185. }
  186. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  187. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  188. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  189. $inputObj->SetSign($config);//签名
  190. $xml = $inputObj->ToXml();
  191. $startTimeStamp = self::getMillisecond();//请求开始时间
  192. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  193. $result = WxPayResults::Init($config, $response);
  194. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  195. return $result;
  196. }
  197. /**
  198. * 下载对账单,WxPayDownloadBill中bill_date为必填参数
  199. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  200. * @param WxPayConfigInterface $config 配置对象
  201. * @param WxPayDownloadBill $inputObj
  202. * @param int $timeOut
  203. * @throws WxPayException
  204. * @return 成功时返回,其他抛异常
  205. */
  206. public static function downloadBill($config, $inputObj, $timeOut = 6)
  207. {
  208. $url = "https://api.mch.weixin.qq.com/pay/downloadbill";
  209. //检测必填参数
  210. if(!$inputObj->IsBill_dateSet()) {
  211. throw new WxPayException("对账单接口中,缺少必填参数bill_date!");
  212. }
  213. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  214. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  215. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  216. $inputObj->SetSign($config);//签名
  217. $xml = $inputObj->ToXml();
  218. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  219. if(substr($response, 0 , 5) == "<xml>"){
  220. return "";
  221. }
  222. return $response;
  223. }
  224. /**
  225. * 授权码查询openid
  226. * 收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台,
  227. * 由商户收银台或者商户后台调用该接口发起支付。
  228. * WxPayWxPayMicroPay中body、out_trade_no、total_fee、auth_code参数必填
  229. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  230. * @param WxPayConfig $config 配置对象
  231. * @param WxPayOpenId $inputObj
  232. * @param int $timeOut
  233. * @return mixed
  234. * @throws WxPayException
  235. */
  236. public static function authcodetoopenid($config, $inputObj, $timeOut = 10)
  237. {
  238. $url = "https://api.mch.weixin.qq.com/tools/authcodetoopenid";
  239. //检测必填参数
  240. if(!$inputObj->IsAuth_codeSet()) {
  241. throw new WxPayException("提交授权码查询API接口中,缺少必填参数auth_code!");
  242. }
  243. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  244. $inputObj->SetSign($config);//签名
  245. $xml = $inputObj->ToXml();
  246. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  247. $result = WxPayResults::Init($config, $response);
  248. return $result;
  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 WxPayConfig $config 配置对象
  257. * @param WxPayMicroPay $inputObj
  258. * @param int $timeOut
  259. * @return mixed
  260. * @throws WxPayException
  261. */
  262. public static function micropay($config, $inputObj, $timeOut = 10)
  263. {
  264. $url = "https://api.mch.weixin.qq.com/pay/micropay";
  265. //检测必填参数
  266. if(!$inputObj->IsBodySet()) {
  267. throw new WxPayException("提交被扫支付API接口中,缺少必填参数body!");
  268. } else if(!$inputObj->IsOut_trade_noSet()) {
  269. throw new WxPayException("提交被扫支付API接口中,缺少必填参数out_trade_no!");
  270. } else if(!$inputObj->IsTotal_feeSet()) {
  271. throw new WxPayException("提交被扫支付API接口中,缺少必填参数total_fee!");
  272. } else if(!$inputObj->IsAuth_codeSet()) {
  273. throw new WxPayException("提交被扫支付API接口中,缺少必填参数auth_code!");
  274. }
  275. $inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']);//终端ip
  276. $inputObj->SetProfit_sharing($config->GetProfitSharing());//是否开启分账
  277. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  278. $inputObj->SetSub_appid($config->GetSubAppId());//子商户公众账号ID
  279. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  280. $inputObj->SetSub_mch_id($config->GetSubMerchantId());//子商户号
  281. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  282. $inputObj->SetSign($config);//签名
  283. $xml = $inputObj->ToXml();
  284. $startTimeStamp = self::getMillisecond();//请求开始时间
  285. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  286. $result = WxPayResults::Init($config, $response);
  287. \Log::debug('微信扫码支付后数据', $result);
  288. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  289. //需要支付密码时轮询
  290. if (!empty($result) && ("SUCCESS" == $result['return_code']) && ("USERPAYING" == $result['err_code'])) {
  291. $orderQuery = new WxPayOrderQuery();
  292. $orderQuery->SetOut_trade_no($inputObj->GetOut_trade_no());
  293. $result = self::loopQueryResult($orderQuery, $config);
  294. }
  295. return $result;
  296. }
  297. /**
  298. * 人脸支付(旧版)
  299. * 收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台,
  300. * 由商户收银台或者商户后台调用该接口发起支付。
  301. * WxPayWxPayMicroPay中body、out_trade_no、total_fee、auth_code参数必填
  302. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  303. * @param WxPayConfigInterface $config 配置对象
  304. * @param WxPayFacePay $inputObj
  305. * @param int $timeOut
  306. * @return mixed
  307. * @throws WxPayException
  308. */
  309. public static function facepay($config, $inputObj, $timeOut = 10)
  310. {
  311. $url = "https://api.mch.weixin.qq.com/pay/facepay";
  312. //检测必填参数
  313. if(!$inputObj->IsBodySet()) {
  314. throw new WxPayException("提交人脸支付API接口中,缺少必填参数body!");
  315. } else if(!$inputObj->IsOut_trade_noSet()) {
  316. throw new WxPayException("提交人脸支付API接口中,缺少必填参数out_trade_no!");
  317. } else if(!$inputObj->IsTotal_feeSet()) {
  318. throw new WxPayException("提交人脸支付API接口中,缺少必填参数total_fee!");
  319. } else if(!$inputObj->IsFace_codeSet()) {
  320. throw new WxPayException("提交人脸支付API接口中,缺少必填参数face_code!");
  321. } else if(!$inputObj->IsOpenidSet()) {
  322. throw new WxPayException("提交人脸支付API接口中,缺少必填参数openid!");
  323. }
  324. $inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']);//终端ip
  325. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  326. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  327. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  328. $inputObj->SetSign($config);//签名
  329. $xml = $inputObj->ToXml();
  330. $startTimeStamp = self::getMillisecond();//请求开始时间
  331. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  332. $result = WxPayResults::Init($config, $response);
  333. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  334. return $result;
  335. }
  336. /**
  337. * 获取授权
  338. * WxPayWxPayMicroPay中body、out_trade_no、total_fee、auth_code参数必填
  339. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  340. * @param WxPayConfigInterface $config 配置对象
  341. * @param WxPayFaceAuthInfo $inputObj
  342. * @param int $timeOut
  343. * @return mixed
  344. * @throws WxPayException
  345. */
  346. public static function authInfo($config, $inputObj, $timeOut = 10)
  347. {
  348. $url = 'https://payapp.weixin.qq.com/face/get_wxpayface_authinfo';
  349. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  350. $inputObj->SetSign($config);
  351. $xml = $inputObj->ToXml();
  352. $response = self::postXmlCurl($config, $xml, $url, false, "5");
  353. $result = WxPayResults::Init($config, $response);
  354. return $result;
  355. }
  356. /**
  357. *
  358. * 撤销订单API接口,WxPayReverse中参数out_trade_no和transaction_id必须填写一个
  359. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  360. * @param WxPayConfigInterface $config 配置对象
  361. * @param WxPayReverse $inputObj
  362. * @param int $timeOut
  363. * @throws WxPayException
  364. */
  365. public static function reverse($config, $inputObj, $timeOut = 6)
  366. {
  367. $url = "https://api.mch.weixin.qq.com/secapi/pay/reverse";
  368. //检测必填参数
  369. if(!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet()) {
  370. throw new WxPayException("撤销订单API接口中,参数out_trade_no和transaction_id必须填写一个!");
  371. }
  372. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  373. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  374. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  375. $inputObj->SetSign($config);//签名
  376. $xml = $inputObj->ToXml();
  377. $startTimeStamp = self::getMillisecond();//请求开始时间
  378. $response = self::postXmlCurl($config, $xml, $url, true, $timeOut);
  379. $result = WxPayResults::Init($config, $response);
  380. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  381. return $result;
  382. }
  383. /**
  384. * 多次请求分账
  385. * 收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台,
  386. * 由商户收银台或者商户后台调用该接口发起支付。
  387. * WxPayWxPayMicroPay中body、out_trade_no、total_fee、auth_code参数必填
  388. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  389. * @param WxPayConfigInterface $config 配置对象
  390. * @param WxPayOpenId $inputObj
  391. * @param int $timeOut
  392. * @return mixed
  393. * @throws WxPayException
  394. */
  395. public static function multiprofitsharing($config, $inputObj, $timeOut = 10)
  396. {
  397. $url = "https://api.mch.weixin.qq.com/tools/authcodetoopenid";
  398. //检测必填参数
  399. if(!$inputObj->IsAuth_codeSet()) {
  400. throw new WxPayException("提交授权码查询API接口中,缺少必填参数auth_code!");
  401. }
  402. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  403. $inputObj->SetSign($config);//签名
  404. $xml = $inputObj->ToXml();
  405. $response = self::postXmlCurl($config, $xml, $url, true, $timeOut);
  406. $result = WxPayResults::Init($config, $response);
  407. return $result;
  408. }
  409. /**
  410. * 单次请求分账
  411. * 收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台,
  412. * 由商户收银台或者商户后台调用该接口发起支付。
  413. * WxPayWxPayMicroPay中body、out_trade_no、total_fee、auth_code参数必填
  414. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  415. * @param WxPayConfig $config 配置对象
  416. * @param WxPayProfitSharing $inputObj
  417. * @param int $timeOut
  418. * @return mixed
  419. * @throws WxPayException
  420. */
  421. public static function profitsharing($config, $inputObj, $timeOut = 10)
  422. {
  423. $url = "https://api.mch.weixin.qq.com/secapi/pay/profitsharing";
  424. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  425. $inputObj->SetSign($config);//签名
  426. $xml = $inputObj->ToXml();
  427. $response = self::postXmlCurl($config, $xml, $url, true, $timeOut);
  428. $result = WxPayResults::Init($config, $response);
  429. return $result;
  430. }
  431. /**
  432. * 添加分账接收方
  433. * 收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台,
  434. * 由商户收银台或者商户后台调用该接口发起支付。
  435. * WxPayWxPayMicroPay中body、out_trade_no、total_fee、auth_code参数必填
  436. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  437. * @param WxPayConfigInterface $config 配置对象
  438. * @param WxPayProfitSharing $inputObj
  439. * @param int $timeOut
  440. * @return mixed
  441. * @throws WxPayException
  442. */
  443. public static function profitsharingaddreceiver($config, $inputObj, $timeOut = 10)
  444. {
  445. $url = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver";
  446. //检测必填参数
  447. if(!$inputObj->IsReceiverSet()) {
  448. throw new WxPayException("提交API接口中,缺少必填参数receiver!");
  449. }
  450. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  451. $inputObj->SetSign($config);//签名
  452. $xml = $inputObj->ToXml();
  453. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  454. $result = WxPayResults::Init($config, $response);
  455. return $result;
  456. }
  457. /**
  458. * 删除分账接收方
  459. * 收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台,
  460. * 由商户收银台或者商户后台调用该接口发起支付。
  461. * WxPayWxPayMicroPay中body、out_trade_no、total_fee、auth_code参数必填
  462. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  463. * @param WxPayConfigInterface $config 配置对象
  464. * @param WxPayProfitSharing $inputObj
  465. * @param int $timeOut
  466. * @return mixed
  467. * @throws WxPayException
  468. */
  469. public static function profitsharingremovereceiver($config, $inputObj, $timeOut = 10)
  470. {
  471. $url = "https://api.mch.weixin.qq.com/pay/profitsharingremovereceiver";
  472. //检测必填参数
  473. if(!$inputObj->IsReceiverSet()) {
  474. throw new WxPayException("提交API接口中,缺少必填参数receiver!");
  475. }
  476. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  477. $inputObj->SetSign($config);//签名
  478. $xml = $inputObj->ToXml();
  479. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  480. $result = WxPayResults::Init($config, $response);
  481. return $result;
  482. }
  483. /**
  484. * 查询分账结果
  485. * 收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台,
  486. * 由商户收银台或者商户后台调用该接口发起支付。
  487. * WxPayWxPayMicroPay中body、out_trade_no、total_fee、auth_code参数必填
  488. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  489. * @param WxPayConfig $config 配置对象
  490. * @param WxPayProfitSharing $inputObj
  491. * @param int $timeOut
  492. * @return mixed
  493. * @throws WxPayException
  494. */
  495. public static function profitsharingquery($config, $inputObj, $timeOut = 10)
  496. {
  497. $url = "https://api.mch.weixin.qq.com/pay/profitsharingquery";
  498. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  499. $inputObj->SetSign($config);//签名
  500. $xml = $inputObj->ToXml();
  501. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  502. $result = WxPayResults::Init($config, $response);
  503. return $result;
  504. }
  505. /**
  506. * 完结分账
  507. * 1、不需要进行分账的订单,可直接调用本接口将订单的金额全部解冻给特约商户
  508. * 2、调用多次分账接口后,需要解冻剩余资金时,调用本接口将剩余的分账金额全部解冻给特约商户
  509. * 3、已调用请求单次分账后,剩余待分账金额为零,不需要再调用此接口。
  510. * WxPayWxPayMicroPay中body、out_trade_no、total_fee、auth_code参数必填
  511. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  512. * @param WxPayConfig $config 配置对象
  513. * @param WxPayProfitSharing $inputObj
  514. * @param int $timeOut
  515. * @return mixed
  516. * @throws WxPayException
  517. */
  518. public static function profitsharingfinish($config, $inputObj, $timeOut = 10)
  519. {
  520. $url = "https://api.mch.weixin.qq.com/secapi/pay/profitsharingfinish";
  521. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  522. $inputObj->SetSign($config);//签名
  523. $xml = $inputObj->ToXml();
  524. $response = self::postXmlCurl($config, $xml, $url, true, $timeOut);
  525. $result = WxPayResults::Init($config, $response);
  526. return $result;
  527. }
  528. /**
  529. *
  530. * 测速上报,该方法内部封装在report中,使用时请注意异常流程
  531. * WxPayReport中interface_url、return_code、result_code、user_ip、execute_time_必填
  532. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  533. * @param WxPayConfigInterface $config 配置对象
  534. * @param WxPayReport $inputObj
  535. * @param int $timeOut
  536. * @throws WxPayException
  537. * @return 成功时返回,其他抛异常
  538. */
  539. public static function report($config, $inputObj, $timeOut = 1)
  540. {
  541. $url = "https://api.mch.weixin.qq.com/payitil/report";
  542. //检测必填参数
  543. if(!$inputObj->IsInterface_urlSet()) {
  544. throw new WxPayException("接口URL,缺少必填参数interface_url!");
  545. } if(!$inputObj->IsReturn_codeSet()) {
  546. throw new WxPayException("返回状态码,缺少必填参数return_code!");
  547. } if(!$inputObj->IsResult_codeSet()) {
  548. throw new WxPayException("业务结果,缺少必填参数result_code!");
  549. } if(!$inputObj->IsUser_ipSet()) {
  550. throw new WxPayException("访问接口IP,缺少必填参数user_ip!");
  551. } if(!$inputObj->IsExecute_time_Set()) {
  552. throw new WxPayException("接口耗时,缺少必填参数execute_time_!");
  553. }
  554. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  555. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  556. $inputObj->SetUser_ip($_SERVER['REMOTE_ADDR']);//终端ip
  557. $inputObj->SetTime(date("YmdHis"));//商户上报时间
  558. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  559. $inputObj->SetSign($config);//签名
  560. $xml = $inputObj->ToXml();
  561. $startTimeStamp = self::getMillisecond();//请求开始时间
  562. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  563. return $response;
  564. }
  565. /**
  566. *
  567. * 生成二维码规则,模式一生成支付二维码
  568. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  569. * @param WxPayConfigInterface $config 配置对象
  570. * @param WxPayBizPayUrl $inputObj
  571. * @param int $timeOut
  572. * @throws WxPayException
  573. * @return 成功时返回,其他抛异常
  574. */
  575. public static function bizpayurl($config, $inputObj, $timeOut = 6)
  576. {
  577. if(!$inputObj->IsProduct_idSet()){
  578. throw new WxPayException("生成二维码,缺少必填参数product_id!");
  579. }
  580. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  581. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  582. $inputObj->SetTime_stamp(time());//时间戳
  583. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  584. $inputObj->SetSign($config);//签名
  585. return $inputObj->GetValues();
  586. }
  587. /**
  588. *
  589. * 转换短链接
  590. * 该接口主要用于扫码原生支付模式一中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX),
  591. * 减小二维码数据量,提升扫描速度和精确度。
  592. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  593. * @param WxPayConfigInterface $config 配置对象
  594. * @param WxPayShortUrl $inputObj
  595. * @param int $timeOut
  596. * @throws WxPayException
  597. * @return 成功时返回,其他抛异常
  598. */
  599. public static function shorturl($config, $inputObj, $timeOut = 6)
  600. {
  601. $url = "https://api.mch.weixin.qq.com/tools/shorturl";
  602. //检测必填参数
  603. if(!$inputObj->IsLong_urlSet()) {
  604. throw new WxPayException("需要转换的URL,签名用原串,传输需URL encode!");
  605. }
  606. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  607. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  608. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  609. $inputObj->SetSign($config);//签名
  610. $xml = $inputObj->ToXml();
  611. $startTimeStamp = self::getMillisecond();//请求开始时间
  612. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  613. $result = WxPayResults::Init($config, $response);
  614. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  615. return $result;
  616. }
  617. /**
  618. *
  619. * 支付结果通用通知
  620. * @param function $callback
  621. * 直接回调函数使用方法: notify(you_function);
  622. * 回调类成员函数方法:notify(array($this, you_function));
  623. * $callback 原型为:function function_name($data){}
  624. */
  625. public static function notify($config, $callback, &$msg)
  626. {
  627. //获取通知的数据
  628. $xml = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");
  629. if (empty($xml)) {
  630. # 如果没有数据,直接返回失败
  631. return false;
  632. }
  633. //如果返回成功则验证签名
  634. try {
  635. $result = WxPayNotifyResults::Init($config, $xml);
  636. } catch (WxPayException $e){
  637. $msg = $e->errorMessage();
  638. return false;
  639. }
  640. return call_user_func($callback, $result);
  641. }
  642. /**
  643. *
  644. * 产生随机字符串,不长于32位
  645. * @param int $length
  646. * @return 产生的随机字符串
  647. */
  648. public static function getNonceStr($length = 32)
  649. {
  650. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  651. $str ="";
  652. for ( $i = 0; $i < $length; $i++ ) {
  653. $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
  654. }
  655. return $str;
  656. }
  657. /**
  658. * 直接输出xml
  659. * @param string $xml
  660. */
  661. public static function replyNotify($xml)
  662. {
  663. echo $xml;
  664. }
  665. /**
  666. *
  667. * 上报数据, 上报的时候将屏蔽所有异常流程
  668. * @param WxPayConfigInterface $config 配置对象
  669. * @param string $url
  670. * @param int $startTimeStamp
  671. * @param array $data
  672. */
  673. private static function reportCostTime($config, $url, $startTimeStamp, $data)
  674. {
  675. //如果不需要上报数据
  676. $reportLevenl = $config->GetReportLevenl();
  677. if($reportLevenl == 0){
  678. return;
  679. }
  680. //如果仅失败上报
  681. if($reportLevenl == 1 &&
  682. array_key_exists("return_code", $data) &&
  683. $data["return_code"] == "SUCCESS" &&
  684. array_key_exists("result_code", $data) &&
  685. $data["result_code"] == "SUCCESS")
  686. {
  687. return;
  688. }
  689. //上报逻辑
  690. $endTimeStamp = self::getMillisecond();
  691. $objInput = new WxPayReport();
  692. $objInput->SetInterface_url($url);
  693. $objInput->SetExecute_time_($endTimeStamp - $startTimeStamp);
  694. //返回状态码
  695. if(array_key_exists("return_code", $data)){
  696. $objInput->SetReturn_code($data["return_code"]);
  697. }
  698. //返回信息
  699. if(array_key_exists("return_msg", $data)){
  700. $objInput->SetReturn_msg($data["return_msg"]);
  701. }
  702. //业务结果
  703. if(array_key_exists("result_code", $data)){
  704. $objInput->SetResult_code($data["result_code"]);
  705. }
  706. //错误代码
  707. if(array_key_exists("err_code", $data)){
  708. $objInput->SetErr_code($data["err_code"]);
  709. }
  710. //错误代码描述
  711. if(array_key_exists("err_code_des", $data)){
  712. $objInput->SetErr_code_des($data["err_code_des"]);
  713. }
  714. //商户订单号
  715. if(array_key_exists("out_trade_no", $data)){
  716. $objInput->SetOut_trade_no($data["out_trade_no"]);
  717. }
  718. //设备号
  719. if(array_key_exists("device_info", $data)){
  720. $objInput->SetDevice_info($data["device_info"]);
  721. }
  722. try{
  723. self::report($config, $objInput);
  724. } catch (WxPayException $e){
  725. //不做任何处理
  726. }
  727. }
  728. /**
  729. * 以post方式提交xml到对应的接口url
  730. * @param WxPayConfig $config 配置对象
  731. * @param string $xml 需要post的xml数据
  732. * @param string $url url
  733. * @param bool $useCert 是否需要证书,默认不需要
  734. * @param int $second url执行超时时间,默认30s
  735. * @return mixed
  736. * @throws WxPayException
  737. */
  738. private static function postXmlCurl($config, $xml, $url, $useCert = false, $second = 30)
  739. {
  740. $ch = curl_init();
  741. $curlVersion = curl_version();
  742. $ua = "WXPaySDK/".self::$VERSION." (".PHP_OS.") PHP/".PHP_VERSION." CURL/".$curlVersion['version']." "
  743. .$config->GetMerchantId();
  744. //设置超时
  745. curl_setopt($ch, CURLOPT_TIMEOUT, $second);
  746. $proxyHost = "0.0.0.0";
  747. $proxyPort = 0;
  748. $config->GetProxy($proxyHost, $proxyPort);
  749. //如果有配置代理这里就设置代理
  750. if($proxyHost != "0.0.0.0" && $proxyPort != 0){
  751. curl_setopt($ch,CURLOPT_PROXY, $proxyHost);
  752. curl_setopt($ch,CURLOPT_PROXYPORT, $proxyPort);
  753. }
  754. curl_setopt($ch,CURLOPT_URL, $url);
  755. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
  756. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验
  757. curl_setopt($ch,CURLOPT_USERAGENT, $ua);
  758. //设置header
  759. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  760. //要求结果为字符串且输出到屏幕上
  761. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  762. if($useCert == true){
  763. //设置证书
  764. //使用证书:cert 与 key 分别属于两个.pem文件
  765. //证书文件请放入服务器的非web目录下
  766. $sslCertPath = $config->GetCertPath();
  767. $sslKeyPath = $config->GetKeyPath();
  768. $config->GetSSLCertPath($sslCertPath, $sslKeyPath);
  769. curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
  770. curl_setopt($ch,CURLOPT_SSLCERT, $sslCertPath);
  771. curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
  772. curl_setopt($ch,CURLOPT_SSLKEY, $sslKeyPath);
  773. }
  774. //post提交方式
  775. curl_setopt($ch, CURLOPT_POST, TRUE);
  776. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  777. //运行curl
  778. $data = curl_exec($ch);
  779. //返回结果
  780. if($data){
  781. curl_close($ch);
  782. return $data;
  783. } else {
  784. $error = curl_errno($ch);
  785. curl_close($ch);
  786. throw new WxPayException("curl出错,错误码:$error");
  787. }
  788. }
  789. /**
  790. * 获取毫秒级别的时间戳
  791. */
  792. private static function getMillisecond()
  793. {
  794. //获取毫秒的时间戳
  795. $time = explode ( " ", microtime () );
  796. $time = $time[1] . ($time[0] * 1000);
  797. $time2 = explode( ".", $time );
  798. $time = $time2[0];
  799. return $time;
  800. }
  801. /**
  802. * 轮询查询订单支付结果
  803. * @param WxPayOrderQuery $queryContentBuilder
  804. * @param WxPayConfig $config
  805. * @return array|null 成功时返回,其他抛异常
  806. * @throws WxPayException
  807. */
  808. protected static function loopQueryResult($queryContentBuilder, $config){
  809. $queryResult = NULL;
  810. for ($i=1;$i<$config->MaxQueryRetry;$i++){
  811. try{
  812. sleep($config->QueryDuration);
  813. }catch (\Exception $e){
  814. print_r($e->getMessage());
  815. exit();
  816. }
  817. $queryResponse = self::orderquery($config, $queryContentBuilder);
  818. \Log::debug('微信扫码查询后数据', $queryResponse);
  819. if(!empty($queryResponse)){
  820. if ($queryResponse['return_code'] != "SUCCESS") {
  821. throw new WxPayException($queryResponse['return_msg']);
  822. }
  823. if ($queryResponse['result_code'] != "SUCCESS") {
  824. if ($queryResponse['err_code'] == 'SYSTEMERROR') {
  825. continue;
  826. }
  827. throw new WxPayException($queryResponse['err_code_des']);
  828. }
  829. if ($queryResponse['trade_state'] == 'REFUND'
  830. || $queryResponse['trade_state'] == "REVOKED"
  831. || $queryResponse['trade_state'] == 'CLOSED'
  832. || $queryResponse['trade_state'] == 'PAYERROR')
  833. {
  834. throw new WxPayException($queryResponse['trade_state_desc']);
  835. }
  836. if ($queryResponse['trade_state'] == "SUCCESS"){
  837. return $queryResponse;
  838. }
  839. $queryResult = $queryResponse;
  840. }
  841. }
  842. if ($queryResult['trade_state'] != 'SUCCESS') {
  843. throw new WxPayException('支付失败');
  844. }
  845. return $queryResult;
  846. }
  847. }