Notify.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * 小程序公共API服务
  7. */
  8. namespace app\bestbao\controller\api\v1;
  9. use app\bestbao\controller\api\Base;
  10. use app\bestbao\model\BestbaoAskOrder;
  11. use app\bestbao\model\BestbaoOrder;
  12. use app\common\facade\WechatPay;
  13. use filter\Filter;
  14. use think\facade\Log;
  15. use Exception;
  16. class Notify extends Base{
  17. /**
  18. * 知识库订阅状态
  19. * @return void
  20. */
  21. public function ask(){
  22. try{
  23. $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function ($message,$fail) {
  24. $result = BestbaoAskOrder::where(['paid_at'=>0,'order_no' => Filter::filter_escape($message['out_trade_no'])])->find();
  25. if (empty($result)) {
  26. return true;
  27. }
  28. if ($message['return_code'] === 'SUCCESS') {
  29. if ($message['result_code'] === 'SUCCESS') {
  30. $ispay = WechatPay::doPay($this->miniapp_id)->order->queryByOutTradeNumber($result->order_no);
  31. if ($ispay['return_code'] === 'SUCCESS') {
  32. if ($ispay['result_code'] === 'SUCCESS') {
  33. if ($ispay['trade_state'] === 'SUCCESS') {
  34. $result->paid_at = 1;
  35. $result->paid_time = strtotime($ispay['time_end']);
  36. $result->paid_no = $ispay['transaction_id'];
  37. $result->save();
  38. return true;
  39. }
  40. }
  41. }
  42. }
  43. }
  44. return $fail('通信失败,请稍后再通知我');
  45. });
  46. $response->send();
  47. }catch (Exception $e) {
  48. $this->error('页面不存在');
  49. }
  50. }
  51. /**
  52. * 订单支付
  53. * @return void
  54. */
  55. public function order(){
  56. try{
  57. $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function ($message,$fail) {
  58. $result = BestbaoOrder::where(['state' => 1,'order_no' => Filter::filter_escape($message['out_trade_no'])])->find();
  59. if (empty($result)) {
  60. return true;
  61. }
  62. if ($message['return_code'] === 'SUCCESS') {
  63. if ($message['result_code'] === 'SUCCESS') {
  64. $ispay = WechatPay::doPay($this->miniapp_id)->order->queryByOutTradeNumber($result->order_no);
  65. if ($ispay['return_code'] === 'SUCCESS') {
  66. if ($ispay['result_code'] === 'SUCCESS') {
  67. if ($ispay['trade_state'] === 'SUCCESS') {
  68. $result->state = 2;
  69. $result->save();
  70. return true;
  71. }
  72. }
  73. }
  74. }
  75. }
  76. return $fail('通信失败,请稍后再通知我');
  77. });
  78. $response->send();
  79. }catch (Exception $e) {
  80. $this->error('页面不存在');
  81. }
  82. }
  83. }