Cardnotify.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\allwin\controller\api\v4;
  9. use app\allwin\controller\api\Base;
  10. use app\common\facade\WechatPay;
  11. use app\allwin\model\CardUser;
  12. use app\allwin\model\CardUserOrder;
  13. use app\allwin\model\CouponUser;
  14. use think\facade\Log;
  15. use filter\Filter;
  16. use Exception;
  17. class Cardnotify extends Base{
  18. /**
  19. * 付费领取优惠券
  20. */
  21. public function index(){
  22. try{
  23. $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function ($message,$fail) {
  24. $result = CardUserOrder::where(['state' => 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. $sub_mchid = $message['sub_mch_id'] ?? '';
  31. $ispay = WechatPay::doPay($this->miniapp_id,false,$sub_mchid)->order->queryByOutTradeNumber($result->order_no);
  32. if ($ispay['return_code'] === 'SUCCESS') {
  33. if ($ispay['result_code'] === 'SUCCESS') {
  34. if ($ispay['trade_state'] === 'SUCCESS'){
  35. $coupon = CouponUser::createUserCoupon($result->coupon_id,$result->user_id);
  36. if($coupon){
  37. $data['member_miniapp_id'] = $result->member_miniapp_id;
  38. $data['store_id'] = $result->store_id;
  39. $data['user_coupon_id'] = $coupon->id;
  40. $data['card_id'] = $result->card_id;
  41. $data['user_id'] = $result->user_id;
  42. $data['amount'] = $result->amount;
  43. $data['update_time'] = time();
  44. $data['create_time'] = time();
  45. $carduser = CardUser::create($data);
  46. if($carduser){
  47. CouponUser::addUserCoupon($result->coupon_ids,$result->user_id); //领取的优惠券
  48. $result->user_card_id = $carduser->id;
  49. }
  50. }
  51. $result->state = 1;
  52. $result->paid_time = strtotime($ispay['time_end']);
  53. $result->save();
  54. return true;
  55. }
  56. }
  57. }
  58. }
  59. }
  60. return $fail('通信失败,请稍后再通知我');
  61. });
  62. $response->send();
  63. }catch (Exception $e) {
  64. $this->error('页面不存在');
  65. }
  66. }
  67. }