<?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\Bank;
use app\allwin\model\BankBill;
use app\allwin\model\AllwinInfo;
use app\allwin\model\AllwinInfoOrder;
use app\allwin\model\AllwinConfig;
use app\common\facade\WechatPay;
use filter\Filter;
use Exception;

class Infonotify extends Base{

     /**
     * 发布同城通知
     * @return void
     */
    public function index(){
        try {
            $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function($message,$fail){
                $order = AllwinInfo::where(['is_pay'=>0,'order_no' => Filter::filter_escape($message['out_trade_no'])])->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,false,$sub_mchid)->order->queryByOutTradeNumber($order->order_no);
                        if ($ispay['return_code'] === 'SUCCESS') {
                            if ($ispay['result_code'] === 'SUCCESS') {
                                if ($ispay['trade_state'] === 'SUCCESS') {
                                    //修改发布状态
                                    AllwinInfo::where(['id' => $order->id])->update(['is_pay' => 1,'is_top' => 1,'is_pay_time' => strtotime($ispay['time_end'])]);
                                    //开通收益计算
                                    BankBill::add(['miniapp_id' => $this->miniapp_id,'store_id' => 0,'money' => -money($order->top_money/100),'uid' => $order->user_id,'pay_uid' => $order->user_id], '发布信息');
                                    //积分奖励
                                    Bank::rePoints($this->miniapp_id, $order->user_id, intval($order->top_money/100));   //增加积分
                                    //分享给推荐人
                                    AllwinInfo::sendinfo($order);
                                    return true;
                                }
                            }
                        }
                    }
                }
                return $fail('通信失败,请稍后再通知我');
            });
            $response->send();
        }catch (Exception $e) {
            $this->error('页面不存在');
        }
    }

    /**
     * 订购
     * @return void
     */
    public function order(){
        try {
            $response = WechatPay::doPay($this->miniapp_id)->handlePaidNotify(function($message,$fail){
                $order = AllwinInfoOrder::where(['paid_at'=>0,'order_no' => Filter::filter_escape($message['out_trade_no'])])->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,false,$sub_mchid)->order->queryByOutTradeNumber($order->order_no);
                        if ($ispay['return_code'] === 'SUCCESS') {
                            if ($ispay['result_code'] === 'SUCCESS') {
                                if ($ispay['trade_state'] === 'SUCCESS') {
                                    $setting = AllwinConfig::getConfig($this->miniapp_id);
                                    $default_charges  = $setting->default_charges/1000;                   //默认手续费
                                    $wechat_charges_amout = money(abs($order->amount*$default_charges));  //微信扣除的服务费(厘四舍五入到分)
                                    $amount = money($order->amount-$wechat_charges_amout);
                                    Bank::recharge($this->miniapp_id,$order->uid,$amount);   //增加收益
                                    BankBill::add(['miniapp_id' => $this->miniapp_id,'store_id' => 0,'money' => $amount,'uid' => $order->uid,'pay_uid' => $order->uid], '城市号用户下单');
                                    AllwinInfoOrder::where(['id' => $order->id])->update(['paid_at' => 1,'paid_no' => $ispay['transaction_id'],'paid_time' => strtotime($ispay['time_end'])]);
                                    return true;
                                }
                            }
                        }
                    }
                }
                return $fail('通信失败,请稍后再通知我');
            });
            $response->send();
        }catch (Exception $e) {
            $this->error('页面不存在');
        }
    }
}