<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 储值活动
 */
namespace app\allwin\controller\api\v4;
use app\allwin\controller\api\Base;
use app\common\facade\WechatPay;
use app\allwin\model\CardUser;
use app\allwin\model\CardUserOrder;
use app\allwin\model\CouponUser;
use think\facade\Log;
use filter\Filter;
use Exception;

class Cardnotify extends Base{
   
    /**
     * 付费领取优惠券
     */
    public function index(){
        try{
            $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function ($message,$fail) {
                $result = CardUserOrder::where(['state' => 0,'order_no' => Filter::filter_escape($message['out_trade_no'])])->find();
                if (empty($result)) {
                    return true;
                }
                if ($message['return_code'] === 'SUCCESS') {
                    if ($message['result_code'] === 'SUCCESS') {
                        $sub_mchid  = $message['sub_mch_id'] ?? '';
                        $ispay = WechatPay::doPay($this->miniapp_id,false,$sub_mchid)->order->queryByOutTradeNumber($result->order_no);
                        if ($ispay['return_code'] === 'SUCCESS') {
                            if ($ispay['result_code'] === 'SUCCESS') {
                                if ($ispay['trade_state'] === 'SUCCESS'){
                                    $coupon = CouponUser::createUserCoupon($result->coupon_id,$result->user_id);
                                    if($coupon){
                                        $data['member_miniapp_id'] = $result->member_miniapp_id;
                                        $data['store_id']          = $result->store_id;
                                        $data['user_coupon_id']    = $coupon->id;
                                        $data['card_id']           = $result->card_id;
                                        $data['user_id']           = $result->user_id;
                                        $data['amount']            = $result->amount;
                                        $data['update_time']       = time();
                                        $data['create_time']       = time();
                                        $carduser = CardUser::create($data);
                                        if($carduser){
                                            CouponUser::addUserCoupon($result->coupon_ids,$result->user_id);  //领取的优惠券
                                            $result->user_card_id = $carduser->id;
                                        }
                                    }
                                    $result->state        = 1;
                                    $result->paid_time    = strtotime($ispay['time_end']);
                                    $result->save();
                                    return true;
                                }
                            }
                        }
                    }
                } 
                return $fail('通信失败,请稍后再通知我');
            });
            $response->send();
        }catch (Exception $e) {
            $this->error('页面不存在');
        }
    }
}