WxPay.MicroPay.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace App\Servers\Weixin;
  3. /**
  4. *
  5. * 刷卡支付实现类
  6. * 该类实现了一个刷卡支付的流程,流程如下:
  7. * 1、提交刷卡支付
  8. * 2、根据返回结果决定是否需要查询订单,如果查询之后订单还未变则需要返回查询(一般反复查10次)
  9. * 3、如果反复查询10订单依然不变,则发起撤销订单
  10. * 4、撤销订单需要循环撤销,一直撤销成功为止(注意循环次数,建议10次)
  11. *
  12. * 该类是微信支付提供的样例程序,商户可根据自己的需求修改,或者使用lib中的api自行开发,为了防止
  13. * 查询时hold住后台php进程,商户查询和撤销逻辑可在前端调用
  14. *
  15. * @author widy
  16. *
  17. */
  18. class MicroPay
  19. {
  20. /**
  21. *
  22. * 提交刷卡支付,并且确认结果,接口比较慢
  23. * @param WxPayMicroPay $microPayInput
  24. * @throws WxpayException
  25. * @return 返回查询接口的结果
  26. */
  27. public function pay($microPayInput)
  28. {
  29. //①、提交被扫支付
  30. $result = WxPayApi::micropay($microPayInput, 5);
  31. //签名验证
  32. $out_trade_no = $microPayInput->GetOut_trade_no();
  33. //②、接口调用成功,明确返回调用失败
  34. if($result["return_code"] == "SUCCESS" &&
  35. $result["result_code"] == "FAIL" &&
  36. $result["err_code"] != "USERPAYING" &&
  37. $result["err_code"] != "SYSTEMERROR")
  38. {
  39. return false;
  40. }
  41. //③、确认支付是否成功
  42. $queryTimes = 10;
  43. while($queryTimes > 0)
  44. {
  45. $succResult = 0;
  46. $queryResult = $this->query($out_trade_no, $succResult);
  47. //如果需要等待1s后继续
  48. if($succResult == 2){
  49. sleep(2);
  50. continue;
  51. } else if($succResult == 1){//查询成功
  52. return $queryResult;
  53. } else {//订单交易失败
  54. return false;
  55. }
  56. }
  57. //④、次确认失败,则撤销订单
  58. if(!$this->cancel($out_trade_no))
  59. {
  60. throw new WxpayException("撤销单失败!");
  61. }
  62. return false;
  63. }
  64. /**
  65. *
  66. * 查询订单情况
  67. * @param string $out_trade_no 商户订单号
  68. * @param int $succCode 查询订单结果
  69. * @return 0 订单不成功,1表示订单成功,2表示继续等待
  70. */
  71. public function query($out_trade_no, &$succCode)
  72. {
  73. $queryOrderInput = new WxPayOrderQuery();
  74. $queryOrderInput->SetOut_trade_no($out_trade_no);
  75. $result = WxPayApi::orderQuery($queryOrderInput);
  76. if($result["return_code"] == "SUCCESS"
  77. && $result["result_code"] == "SUCCESS")
  78. {
  79. //支付成功
  80. if($result["trade_state"] == "SUCCESS"){
  81. $succCode = 1;
  82. return $result;
  83. }
  84. //用户支付中
  85. else if($result["trade_state"] == "USERPAYING"){
  86. $succCode = 2;
  87. return false;
  88. }
  89. }
  90. //如果返回错误码为“此交易订单号不存在”则直接认定失败
  91. if($result["err_code"] == "ORDERNOTEXIST")
  92. {
  93. $succCode = 0;
  94. } else{
  95. //如果是系统错误,则后续继续
  96. $succCode = 2;
  97. }
  98. return false;
  99. }
  100. /**
  101. *
  102. * 撤销订单,如果失败会重复调用10次
  103. * @param string $out_trade_no
  104. * @param 调用深度 $depth
  105. */
  106. public function cancel($out_trade_no, $depth = 0)
  107. {
  108. if($depth > 10){
  109. return false;
  110. }
  111. $clostOrder = new WxPayReverse();
  112. $clostOrder->SetOut_trade_no($out_trade_no);
  113. $result = WxPayApi::reverse($clostOrder);
  114. //接口调用失败
  115. if($result["return_code"] != "SUCCESS"){
  116. return false;
  117. }
  118. //如果结果为success且不需要重新调用撤销,则表示撤销成功
  119. if($result["result_code"] != "SUCCESS"
  120. && $result["recall"] == "N"){
  121. return true;
  122. } else if($result["recall"] == "Y") {
  123. return $this->cancel($out_trade_no, ++$depth);
  124. }
  125. return false;
  126. }
  127. }