WxPay.MicroPay.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. *
  4. * example目录下为简单的支付样例,仅能用于搭建快速体验微信支付使用
  5. * 样例的作用仅限于指导如何使用sdk,在安全上面仅做了简单处理, 复制使用样例代码时请慎重
  6. * 请勿直接直接使用样例对外提供服务
  7. *
  8. **/
  9. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  10. require_once($wx . '/lib/WxPay.Api.php');
  11. require_once($wx . '/example/WxPay.Config.php');
  12. /**
  13. *
  14. * 刷卡支付实现类
  15. * 该类实现了一个刷卡支付的流程,流程如下:
  16. * 1、提交刷卡支付
  17. * 2、根据返回结果决定是否需要查询订单,如果查询之后订单还未变则需要返回查询(一般反复查10次)
  18. * 3、如果反复查询10订单依然不变,则发起撤销订单
  19. * 4、撤销订单需要循环撤销,一直撤销成功为止(注意循环次数,建议10次)
  20. *
  21. * 该类是微信支付提供的样例程序,商户可根据自己的需求修改,或者使用lib中的api自行开发,为了防止
  22. * 查询时hold住后台php进程,商户查询和撤销逻辑可在前端调用
  23. *
  24. * @author widy
  25. *
  26. */
  27. class MicroPay
  28. {
  29. /**
  30. *
  31. * 提交刷卡支付,并且确认结果,接口比较慢
  32. * @param WxPayMicroPay $microPayInput
  33. * @throws WxpayException
  34. * @return 返回查询接口的结果
  35. */
  36. public function pay($microPayInput,$sjExtend)
  37. {
  38. //①、提交被扫支付
  39. $config = new WxPayConfig();
  40. $mid = $sjExtend['wxPayMerchantId'] ?? '';
  41. $appId = $sjExtend['miniAppId'] ?? '';
  42. $key = $sjExtend['wxPayKey'] ?? '';
  43. $config->SetAppId($appId);
  44. $config->SetMerchantId($mid);
  45. $config->SetKey($key);
  46. $result = WxPayApi::micropay($config, $microPayInput, 5);
  47. return $result;
  48. }
  49. /**
  50. *
  51. * 查询订单情况
  52. * @param string $out_trade_no 商户订单号
  53. * @param int $succCode 查询订单结果
  54. * @return 0 订单不成功,1表示订单成功,2表示继续等待
  55. */
  56. public function query($out_trade_no, $sjExtend)
  57. {
  58. $queryOrderInput = new WxPayOrderQuery();
  59. $queryOrderInput->SetOut_trade_no($out_trade_no);
  60. $config = new WxPayConfig();
  61. $mid = $sjExtend['wxPayMerchantId'] ?? '';
  62. $appId = $sjExtend['miniAppId'] ?? '';
  63. $key = $sjExtend['wxPayKey'] ?? '';
  64. $config->SetAppId($appId);
  65. $config->SetMerchantId($mid);
  66. $config->SetKey($key);
  67. try{
  68. $result = WxPayApi::orderQuery($config, $queryOrderInput);
  69. return $result;
  70. } catch(Exception $e) {
  71. //Log::ERROR(json_encode($e));
  72. }
  73. }
  74. /**
  75. *
  76. * 撤销订单,如果失败会重复调用10次
  77. * @param string $out_trade_no
  78. * @param 调用深度 $depth
  79. */
  80. public function cancel($out_trade_no, $depth = 0)
  81. {
  82. try {
  83. if($depth > 10){
  84. return false;
  85. }
  86. $clostOrder = new WxPayReverse();
  87. $clostOrder->SetOut_trade_no($out_trade_no);
  88. $config = new WxPayConfig();
  89. $result = WxPayApi::reverse($config, $clostOrder);
  90. //接口调用失败
  91. if($result["return_code"] != "SUCCESS"){
  92. return false;
  93. }
  94. //如果结果为success且不需要重新调用撤销,则表示撤销成功
  95. if($result["result_code"] != "SUCCESS"
  96. && $result["recall"] == "N"){
  97. return true;
  98. } else if($result["recall"] == "Y") {
  99. return $this->cancel($out_trade_no, ++$depth);
  100. }
  101. } catch(Exception $e) {
  102. Log::ERROR(json_encode($e));
  103. }
  104. return false;
  105. }
  106. }