<?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\allwin\model\Vip;
use app\allwin\model\Bank;
use app\allwin\model\BankBill;
use app\allwin\model\CouponUser;
use app\allwin\widget\Vip as VipIncome;
use app\common\facade\WechatPay;
use app\common\facade\Inform;
use filter\Filter;
use Exception;

class Vipnotify extends Base{

    public function index(){
        try {
            $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function ($message,$fail) {
                $result = Vip::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'){
                                    //锁定以前的会员
                                    Vip::where(['user_id' => $result->user_id,'state' => 1])->update(['is_lock' => 1]);
                                    //修改当前会员开通成功
                                    Vip::where(['id' => $result->id])->update(['state' => 1,'paid_time' => time(),'paid_no' => $ispay['transaction_id']]);
                                    //领取的优惠券
                                    CouponUser::addUserCoupon($result->coupon_ids,$result->user_id);
                                    //消费日志
                                    BankBill::add(['miniapp_id' => $this->miniapp_id,'store_id' => $result->store_id,'money' => -$result->amount,'uid'=>$result->user_id,'pay_uid' => $result->user_id], '微信支付[会员开通]');
                                    if(!empty($result->from_ucode)){
                                        VipIncome::register($result->user_id,$result->from_ucode);  //增加邀请关系
                                    }
                                    VipIncome::income($result->toArray());
                                    Bank::rePoints($this->miniapp_id, $result->user_id, intval($result->amount));   //分配消费积分
                                    //微信通知到申请者
                                    if(!empty($result->vipcard)){
                                        Inform::sms($result->user_id,$this->miniapp_id,['title' =>'业务进展通知','type' => '身份变更','state' => '成功','content' =>'您的身份已变更为'.$result->vipcard['name']]);
                                    }
                                    return true;
                                }
                            }
                        }
                    }
                } 
                return $fail('通信失败,请稍后再通知我');
            });
            $response->send();
        }catch (Exception $e) {
           $this->error('页面不存在');
        }
    }
}