<?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\AllwinStore;
use app\allwin\model\Worker;
use app\allwin\model\WorkerStoreOrder;
use app\allwin\model\BankBill;
use app\common\event\User as CommonUser;
use app\common\facade\Inform;
use app\common\facade\WechatPay;
use app\common\model\SystemMemberSms;
use Exception;

class Partnershipnotify extends Base{

    /**
     * 支付回调
     */
    public function workerNotify(){
        $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function($message, $fail) {
            $order = Worker::where(['order_no' => $message['out_trade_no'],'pay_state' => 0])->find();
            if (empty($order)) {
                return true;
            }
            if ($message['return_code'] === 'SUCCESS') {
                if ($message['result_code'] === 'SUCCESS') {
                    $sub_mchid  = $message['sub_mch_id'] ?? '';
                    $ispay = WechatPay::doPay($this->miniapp_id,true,$sub_mchid)->order->queryByOutTradeNumber($message['out_trade_no']);
                    if ($ispay['return_code'] === 'SUCCESS' && $ispay['trade_state'] === 'SUCCESS') {
                        $data = [
                            'member_miniapp_id' => $this->miniapp_id,
                            'city_id'           => 0,
                            'store_id'          => 0,
                            'user_id'           => $order->uid,
                            'pay_uid'           => $order->uid,
                            'message'           => '开通合伙人费用',
                            'update_time'       => time()
                        ];
                        if($ispay['total_fee'] == $order->price * 100){
                            Worker::where(['id' => $order->id])->update(['pay_state' => 1,'paid_time' => time(),'wechat_order' =>$ispay['transaction_id']]);
                            $data['money'] =  $order->price;
                        }else{
                            $data['money'] = $ispay['total_fee'] / 100;
                        }
                        BankBill::create($data);
                        //微信申请合伙人审核通知
                        SystemMemberSms::sms($this->miniapp_id,'您有一条合伙人待审核',url('allwin/user/worker'));
                        //通知申请者到微信
                        Inform::sms($order['uid'],$this->miniapp_id,['title' =>'业务进展通知','type' => '合伙人申请','content' =>'您的合伙人申请正在审核中']);
                        //通知到后台管理的微信
                        Inform::sms(CommonUser::isFounder($this->miniapp_id)->user_id,$this->miniapp_id,['title' =>'业务进展通知','type' => '合伙人申请','content' =>'您有一条新的合伙人申请待审核']);
                        return true;
                    } else {
                        return $fail('通信失败,请稍后再通知我');
                    }
                } else {
                    return $fail('通信失败,请稍后再通知我');
                }
            } else {
                return $fail('通信失败,请稍后再通知我');
            }
        });
        $response->send();
    }

    public function onStoreNotify(){
        $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function($message, $fail) {
            $order = WorkerStoreOrder::where(['order_no' => $message['out_trade_no'], 'pay_state' => 0])->find();
            if (empty($order)) {
                return true;
            }
            if ($message['return_code'] === 'SUCCESS') {
                if ($message['result_code'] === 'SUCCESS') {
                    $sub_mchid  = $message['sub_mch_id'] ?? '';
                    $ispay = WechatPay::doPay($this->miniapp_id,true,$sub_mchid)->order->queryByOutTradeNumber($message['out_trade_no']);
                    if ($ispay['return_code'] === 'SUCCESS' && $ispay['trade_state'] === 'SUCCESS') {
                        $store = AllwinStore::where(['id' => $order->store_id])->find();
                        $data = [
                            'member_miniapp_id' => $this->miniapp_id,
                            'city_id'           => 0,
                            'store_id'          => 0,
                            'user_id'           => $order->uid,
                            'pay_uid'           => $order->uid,
                            'message'           => '开通好店费用',
                            'update_time'       => time()
                        ];
                        if($ispay['total_fee'] == $order->price * 100){
                            WorkerStoreOrder::where(['id' => $order->id])->update(['pay_state' => 1, 'paid_time' => time(), 'wechat_order' =>$ispay['transaction_id']]);
                            $data['money'] =  $order->price;
                        }else{
                            $data['money'] = $ispay['total_fee'] / 100;
                        }
                        BankBill::create($data);
                        //好店审核通知
                        SystemMemberSms::sms($this->miniapp_id,'您有一条好店信息待审核',url('allwin/store/index'));
                        //通知申请者到微信
                        Inform::sms($order['uid'],$this->miniapp_id,['title' =>'业务进展通知','type' => '好店申请','content' =>'您提交的好店申请正在审核中']);
                        //通知到后台管理的微信
                        Inform::sms(CommonUser::isFounder($this->miniapp_id)->user_id,$this->miniapp_id,['title' =>'业务进展通知','type' => '好店申请','content' =>'您有一条新的好店申请待审核']);
                        return true;
                    } else {
                        return $fail('通信失败,请稍后再通知我');
                    }
                } else {
                    return $fail('通信失败,请稍后再通知我');
                }
            } else {
                return $fail('通信失败,请稍后再通知我');
            }
        });
        $response->send();
    }
}