Notify.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 支付回调通知
  7. */
  8. namespace app\citys\controller\api\v1;
  9. use app\citys\controller\api\Base;
  10. use app\citys\model\Citys;
  11. use app\citys\model\CitysConfig;
  12. use app\citys\model\CitysPhone;
  13. use app\citys\model\CitysOrder;
  14. use app\common\facade\WechatPay;
  15. use filter\Filter;
  16. use Exception;
  17. class Notify extends Base{
  18. /**
  19. * 发布同城通知
  20. * @return void
  21. */
  22. public function index(){
  23. try {
  24. $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function($message,$fail){
  25. $order = Citys::where(['is_pay'=>0,'order_no' => Filter::filter_escape($message['out_trade_no'])])->find();
  26. if (empty($order)) {
  27. return true;
  28. }
  29. if ($message['return_code'] === 'SUCCESS') {
  30. if($message['result_code'] === 'SUCCESS'){
  31. $sub_mchid = $message['sub_mch_id'] ?? '';
  32. $ispay = WechatPay::doPay($this->miniapp_id,false,$sub_mchid)->order->queryByOutTradeNumber($order->order_no);
  33. if ($ispay['return_code'] === 'SUCCESS') {
  34. if ($ispay['result_code'] === 'SUCCESS') {
  35. if ($ispay['trade_state'] === 'SUCCESS') {
  36. $order->is_pay = 1;
  37. $order->is_top = empty($order->top_money)?0:1;
  38. $order->is_lock = 0;
  39. $order->is_pay_time = strtotime($ispay['time_end']);
  40. $order->save();
  41. return true;
  42. }
  43. }
  44. }
  45. }
  46. }
  47. return $fail('通信失败,请稍后再通知我');
  48. });
  49. $response->send();
  50. }catch (Exception $e) {
  51. $this->error('页面不存在');
  52. }
  53. }
  54. /**
  55. * 订购
  56. * @return void
  57. */
  58. public function order(){
  59. try {
  60. $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function($message,$fail){
  61. $order = CitysOrder::where(['paid_at'=>0,'order_no' => Filter::filter_escape($message['out_trade_no'])])->find();
  62. if (empty($order)) {
  63. return true;
  64. }
  65. if ($message['return_code'] === 'SUCCESS') {
  66. if($message['result_code'] === 'SUCCESS'){
  67. $sub_mchid = $message['sub_mch_id'] ?? '';
  68. $ispay = WechatPay::doPay($this->miniapp_id,false,$sub_mchid)->order->queryByOutTradeNumber($order->order_no);
  69. if ($ispay['return_code'] === 'SUCCESS') {
  70. if ($ispay['result_code'] === 'SUCCESS') {
  71. if ($ispay['trade_state'] === 'SUCCESS') {
  72. $order->paid_at = 1;
  73. $order->paid_time = strtotime($ispay['time_end']);
  74. $order->paid_no = $ispay['transaction_id'];
  75. $order->save();
  76. return true;
  77. }
  78. }
  79. }
  80. }
  81. }
  82. return $fail('通信失败,请稍后再通知我');
  83. });
  84. $response->send();
  85. }catch (Exception $e) {
  86. $this->error('页面不存在');
  87. }
  88. }
  89. /**
  90. * 订购
  91. * @return void
  92. */
  93. public function showPhone(){
  94. try {
  95. $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function($message,$fail){
  96. $result = CitysPhone::where(['paid_at'=>0,'order_no' => Filter::filter_escape($message['out_trade_no'])])->find();
  97. if (empty($result)) {
  98. return true;
  99. }
  100. if ($message['return_code'] === 'SUCCESS') {
  101. if($message['result_code'] === 'SUCCESS'){
  102. $sub_mchid = $message['sub_mch_id'] ?? '';
  103. $ispay = WechatPay::doPay($this->miniapp_id,false,$sub_mchid)->order->queryByOutTradeNumber($result->order_no);
  104. if ($ispay['return_code'] === 'SUCCESS') {
  105. if ($ispay['result_code'] === 'SUCCESS') {
  106. if ($ispay['trade_state'] === 'SUCCESS') {
  107. $result->paid_at = 1;
  108. $result->paid_time = strtotime($ispay['time_end']);
  109. $result->paid_no = $ispay['transaction_id'];
  110. $result->save();
  111. return true;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. return $fail('通信失败,请稍后再通知我');
  118. });
  119. $response->send();
  120. }catch (Exception $e) {
  121. $this->error('页面不存在');
  122. }
  123. }
  124. }