Notify.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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\green\controller\api\v1;
  9. use app\green\controller\api\Base;
  10. use app\common\facade\WechatPay;
  11. use app\green\model\GreenOrder;
  12. use app\green\model\GreenUser;
  13. use Exception;
  14. class Notify extends Base{
  15. /**
  16. * 商城购买
  17. * @access public
  18. */
  19. public function shop(){
  20. try {
  21. $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function($message,$fail){
  22. $result = GreenOrder::where(['order_no' => $message['out_trade_no'],'paid_at' => 0])->find();
  23. if (empty($result)){
  24. return true;
  25. }
  26. if ($message['return_code'] === 'SUCCESS') {
  27. if($message['result_code'] === 'SUCCESS'){
  28. $ispay = WechatPay::doPay($this->miniapp_id)->order->queryByOutTradeNumber($result->order_no);
  29. if ($ispay['return_code'] === 'SUCCESS') {
  30. if ($ispay['result_code'] === 'SUCCESS') {
  31. if ($ispay['trade_state'] === 'SUCCESS') {
  32. $result->paid_at = 1;
  33. $result->paid_time = strtotime($ispay['time_end']);
  34. $result->paid_no = $ispay['transaction_id'];
  35. $result->save();
  36. $info = GreenUser::where(['uid' => $result->user_id])->find();
  37. $info->points = ['dec', $result->points];
  38. $info->update_time = time();
  39. $info->save();
  40. return true;
  41. }
  42. }
  43. }
  44. return $fail('通信失败,请稍后再通知我');
  45. }else{
  46. return $fail('通信失败,请稍后再通知我');
  47. }
  48. }else{
  49. return $fail('通信失败,请稍后再通知我');
  50. }
  51. });
  52. $response->send();
  53. }catch (Exception $e) {
  54. $this->error('页面不存在');
  55. }
  56. }
  57. }