<?php

namespace App\Http\Controllers\V1;

use Omnipay\Omnipay;
use App\Helper\LogHelper;
use App\Helper\PayHelper;
use App\Helper\JpushHelper;
use App\Models\User;
use App\Models\order;
use App\Models\AccountLog;
use App\Services\Base\ErrorCode;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;


class PayController extends Controller
{
    use PayHelper, LogHelper,JpushHelper;

    public function alipayNotify() {
        $rawInfo = Request::all();
        Log::info('aliplay callback');
        Log::info($rawInfo);

        $config = Config::get('laravel-omnipay.gateways.alipay');

        $gateway = Omnipay::create($config['driver']);
        print_r($gateway);exit;
        $gateway->setEnvironment($config['options']['environment']);
        $gateway->setAppId($config['options']['appid']);
        $gateway->setEncryptKey($config['options']['encrypt_key']);
        $gateway->setPrivateKey($config['options']['prikey']);
        $gateway->setAlipayPublicKey($config['options']['ali_pubkey']);
        $gateway->setNotifyUrl($config['options']['notify_url']);

        $request = $gateway->completePurchase();
        $request->setParams($rawInfo);//Optional

        try {
            $response = $request->send();

            if($response->isPaid()){
              echo 1;exit;
//                 Log::info('支付成功');

//                 $out_trade_no = $rawInfo['out_trade_no'];
//                 $order = Order::where('transaction_id', '=', $out_trade_no)->first();
//                 if (!$order) {
//                     Log::error('找不到订单' . $out_trade_no);
//                     return 'fail';
//                 }
//                 $master_amount =  $order->number;
//                 $slave_amount = 0;
//                 if (!empty($order->ext_info)) {
//                     $extInfo = json_decode($order->ext_info, true);
//                     if ($extInfo !== null) {
//                         $cc = $extInfo['cc_bonus'];
//                         $master_amount = round(($cc / 100) * $order->number);
//                         $slave_amount = $order->number - $master_amount;
//                     }
//                 }
//                 $tp = '';
// //                $u = null;
//                 if ($order->user_type == Order::USER_TYPE_MERCHANT) {
//                     $tp = 'Merchant';
// //                    $u = Merchant::find($order->user_id);
//                 } elseif ($order->user_type == Order::USER_TYPE_MEMBER) {
//                     $tp = 'Member';
//                     //Not handled here
//                 }
//                 $u = User::find($order->user_id);
//                 if (!$u) {
//                     Log::error('用户不存在' . $order->user_type . ', ' . $order->user_id);
//                     return 'success';
//                 }

//                 DB::beginTransaction();

//                 if ($order->goods_type == Order::GOODS_TYPE_BALANCE||$order->goods_type == Order::GOODS_TYPE_COSUME) {
//                     $cType = AccountLog::TYPE_BALANCE;
// //                    $u->account_balance += $order->number;
//                     $u->balance += $master_amount;
//                 } elseif ($order->goods_type == Order::GOODS_TYPE_CREDIT) {
//                     $cType = AccountLog::TYPE_CREDIT;
// //                    $u->credit_balance += $order->number;
//                     $u->credit += $master_amount;
//                 } else {
//                     Log::error('商品不存在' . $order->goods_type);
//                     return 'success';
//                 }
//                 Log::info('支付金额 '.$master_amount);
//                 if ($u->save()) {

//                     //更新订单状态
//                     $order->status = Order::STATUS_FINISHED;
//                     $order->save();

//                     //记日志
//                     $amount = $rawInfo['total_amount'] * 100;
//                     $_ops = AccountLog::getAllop();
//                     $this->logAccount($tp, $u->id, $u->name,
//                             AccountLog::OP_CHARGE, $cType, $master_amount,
//                             AccountLog::DIRECTION_INC, $cType == AccountLog::TYPE_BALANCE ? $u->balance : $u->credit, AccountLog::CHANNEL_ALIPAY);

//                     Log::info('支付完成');

//                 } else {
//                     DB::rollBack();
//                     Log::error('保存数据失败');
//                 }

//                 DB::commit();

//                 return 'success';
            }else{
                Log::error('支付失败');

                return 'fail';
            }
        } catch (\Exception $e) {
            Log::error('支付异常' . $e->getMessage());

            return 'fail';
        }

    }

    public function wechatpayNotify() {
        Log::debug('wechatpay callback');
//        Log::debug(Request::all());

        $config = Config::get('laravel-omnipay.gateways.wechatpay');

        $gateway = Omnipay::create("WechatPay");
//        $gateway->setEnvironment('sandbox');
        $gateway->setAppId($config['options']['appid']);
        $gateway->setMchId($config['options']['merchant_id']);
        $gateway->setApiKey($config['options']['apikey']);

        $response = $gateway->completePurchase([
            'request_params' => file_get_contents('php://input')
        ])->send();
        try {
            if ($response->isPaid()) {
                //pay success
                // $rawInfo = $response->getRequestData();
                // Log::debug($rawInfo);
                // Log::info('支付成功');

                // $out_trade_no = $rawInfo['out_trade_no'];
                // $order = order::where('transaction_id', '=', $out_trade_no)->first();
                // if (!$order) {
                //     Log::error('找不到订单' . $out_trade_no);
                //     return 'fail';
                // }
                // $u = User::find($order->user_id);
                // if (!$u) {
                //     Log::error('用户不存在' . $order->user_id . ', ' . $order->user_id);
                //     return 'success';
                // }

                // DB::beginTransaction();
                // $u->coin += $order->number;
                // if ($u->save()) {

                //     //更新订单状态
                //     $order->status = order::STATUS_FINISHED;
                //     $order->save();

                //     //记日志
                //     $_types = AccountLog::getAllType();
                //     $_ops = AccountLog::getAllop();
                //     $this->logAccount($_types[AccountLog::TYPE_CASH], $u->id, $u->name,$order->amount,
                //         $_ops[AccountLog::OP_CHARGE],
                //         $_types[AccountLog::TYPE_COIN], $u->id,$u->name,$order->amount,$out_trade_no,'http://miao.beiyuesi.com/base/img/wechat.png');

                //     Log::info('支付完成');

                // } else {
                //     DB::rollBack();
                //     Log::error('保存数据失败');
                // }

                // DB::commit();

                // return 'success';
            }else{
                //pay fail
                Log::error($response->getData());
                return 'fail';
            }

        } catch (\Exception $e) {
            Log::error('支付异常' . $e->getMessage());

            return 'fail';
        }
    }



    /**
     * @api {post} /api/pay/charge 充值
     * @apiDescription 充值（向平台充值）
     * @apiGroup User
     * @apiPermission Passport
     * @apiVersion 0.1.0
     * @apiParam {int}  [goods=1]   充值商品（1：梦想币）
     * @apiParam {int}  number      充值数量，人民币，以分表示
     * @apiParam {int}  type        支付类型（1：支付宝，2：微信支付）
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *     "state": true,
     *     "code": 0,
     *     "message": "",
     *     "data": {
     *         "id": 2,
     *         "code": "ALIPAY_201610231314145719",
     *         "transaction_id": "201610231314145719",
     *         "user_type": 2,
     *         "user_id": 1,
     *         "goods_type": 1,
     *         "price": 1,
     *         "number": 1,
     *         "amount": 1,
     *         "pay_type": 1,
     *         "status": 0,
     *         "created_at": "2016-10-23 13:14:14",
     *         "updated_at": "2016-10-23 13:14:14",
     *         "orderString": "alipay_sdk=lokielse%2Fomnipay-alipay&app_id=2016091201894867&biz_content=%7B%22subject%22%3A%22%5Cu7532%5Cu8c61%5Cu8054%5Cu5408-%5Cu4f59%5Cu989d%5Cu5145%5Cu503c%22%2C%22out_trade_no%22%3A%22201610231314145719%22%2C%22total_amount%22%3A0.01%2C%22product_code%22%3A1%7D&charset=UTF-8&format=JSON&method=alipay.trade.app.pay&notify_url=http%3A%2F%2Fweb%2Fapi%2Fpay%2Falipay%2Fnotify&sign_type=RSA&timestamp=2016-10-23+13%3A14%3A14&version=1.0&sign=oxKM0qGMHLWDlMrXHIiy9%2Fk2BXJq3rC3RKdmcfFwkBJVRXvtG6cBoAYPll6VxJYOMQWeu78Ibfov%2FxIVCuN9yUfzEiokfQrzBoptc94bCQ5k0pNyJcSdgezOUKHB12P5Zmm3Hd6AAbGRDV9UCaLVz0wYkFJPrCyUv1ZfhrM%2BBqc%3D"
     *     }
     * }
     * @apiErrorExample {json} Error-Response:
     * HTTP/1.1 400 Bad Request
     * {
     *     "state": false,
     *     "code": 1000,
     *     "message": "传入参数不正确",
     *     "data": null or []
     * }
     * 可能出现的错误代码：
     * 1000    CLIENT_WRONG_PARAMS             传入参数不正确
     */
    public function charge(Request $request)
    {
        $goodsTypes = order::getAllGoodsTypes();
        $gTypes = array_keys($goodsTypes);
        $payTypes = order::getAllPayTypes();
        $pTypes = array_keys($payTypes);
        $validator = Validator::make($data = $request->all(),
            [
                'number' => 'required|integer|min:1',
                'type' => 'required|in:' . join(',', $pTypes),
                'goods' => 'in:' . join(',', $gTypes),
            ],
            [
                'number.required' => '请输入梦想币数量',
                'number.integer' => '梦想币数量必须为整数',
                'number.min' => '充值梦想币数量至少为1',
                'type.required' => '支付类型必填',
                'type.in' => '支付类型非法',
                'goods.in' => '充值商品非法',
            ]
        );

        if ($validator->fails()) {
            return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
        }

        $user = Auth::guard('api')->user();
        $data['goods_id'] = 1;
        if (!isset($data['goods'])) {//默认充余额
            $data['goods'] = order::GOODS_TYPE_COIN;
        }
        $data['user_id'] = $user->id;
        if ($data['type'] == order::PAY_TYPE_ALIPAY) {
            $result = $this->createAlipayCharge($data);
        } else {
            $result = $this->createWechatpayCharge($data);
        }

        if ($result === false) {
            return $this->error(ErrorCode::ORDER_GENERATE_FAILED);
        }
        //log
//        $data['amount'] = $data['number'];
//        $this->chargeLog('Merchant', $merchant->id, $merchant->name,
//            'balance', $data['amount'], 'balance', $data['amount'],
//            'Merchant', $user->id, $merchant->name);

        return $this->api($result);
    }

    /**
     * @api {post} /api/pay/order_status 订单状态查询
     * @apiDescription 订单状态查询
     * @apiGroup User
     * @apiPermission Passport
     * @apiVersion 0.1.0
     * @apiParam {int}  [goods=1]   充值商品（1：梦想币）
     * @apiParam {int}  number      充值数量，人民币，以分表示
     * @apiParam {int}  type        支付类型（1：支付宝，2：微信支付）
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *     "state": true,
     *     "code": 0,
     *     "message": "",
     *     "data": {
     *         "id": 2,
     *         "code": "ALIPAY_201610231314145719",
     *         "transaction_id": "201610231314145719",
     *         "user_type": 2,
     *         "user_id": 1,
     *         "goods_type": 1,
     *         "price": 1,
     *         "number": 1,
     *         "amount": 1,
     *         "pay_type": 1,
     *         "status": 0,
     *         "created_at": "2016-10-23 13:14:14",
     *         "updated_at": "2016-10-23 13:14:14",
     *         "orderString": "alipay_sdk=lokielse%2Fomnipay-alipay&app_id=2016091201894867&biz_content=%7B%22subject%22%3A%22%5Cu7532%5Cu8c61%5Cu8054%5Cu5408-%5Cu4f59%5Cu989d%5Cu5145%5Cu503c%22%2C%22out_trade_no%22%3A%22201610231314145719%22%2C%22total_amount%22%3A0.01%2C%22product_code%22%3A1%7D&charset=UTF-8&format=JSON&method=alipay.trade.app.pay&notify_url=http%3A%2F%2Fweb%2Fapi%2Fpay%2Falipay%2Fnotify&sign_type=RSA&timestamp=2016-10-23+13%3A14%3A14&version=1.0&sign=oxKM0qGMHLWDlMrXHIiy9%2Fk2BXJq3rC3RKdmcfFwkBJVRXvtG6cBoAYPll6VxJYOMQWeu78Ibfov%2FxIVCuN9yUfzEiokfQrzBoptc94bCQ5k0pNyJcSdgezOUKHB12P5Zmm3Hd6AAbGRDV9UCaLVz0wYkFJPrCyUv1ZfhrM%2BBqc%3D"
     *     }
     * }
     * @apiErrorExample {json} Error-Response:
     * HTTP/1.1 400 Bad Request
     * {
     *     "state": false,
     *     "code": 1000,
     *     "message": "传入参数不正确",
     *     "data": null or []
     * }
     * 可能出现的错误代码：
     * 1000    CLIENT_WRONG_PARAMS             传入参数不正确
     */

    public function orderStatus(Request $request)
    {

        $validator = Validator::make($data = $request->all(),
            [
                'number' => 'required|integer|min:1',
                'type' => 'required|in:' . join(',', $pTypes),
                'goods' => 'in:' . join(',', $gTypes),
            ],
            [
                'number.required' => '请输入梦想币数量',
                'number.integer' => '梦想币数量必须为整数',
                'number.min' => '充值梦想币数量至少为1',
                'type.required' => '支付类型必填',
                'type.in' => '支付类型非法',
                'goods.in' => '充值商品非法',
            ]
        );

        if ($validator->fails()) {
            return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
        }
        $order = new order();
        $order->code            = $data['code'];
        $order->transaction_id  = $data['transaction_id'];
        $order->user_id         = $data['user_id'];
        $order->goods_id        = $data['goods_id'];
        $order->price           = $data['price'];
        $order->number          = $data['number'];
        $order->amount          = $data['amount'];
        $order->pay_type        = $data['pay_type'];
        if (isset($data['ext_info'])) {
            $order->ext_info = $data['ext_info'];
        }

        if ($order->save()) {
            return $order->find($order->id);
        }
        return $this->api('');
    }

    public function orderIos(Request $request)
    {

        $validator = Validator::make($data = $request->all(),
            [
                'number' => 'required',
            ],
            [
                'number.required' => '请输入产品ID',
            ]
        );

        if ($validator->fails()) {
            return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
        }
//        $order = new order();

        Log::info($data['number']);

        $AppleProducts = order::getAllAppleProducts();
        $coin = 0 ;
        if(isset($AppleProducts[$data['number']])){
            $coin = $AppleProducts[$data['number']];
        }
//        $order->code            = $data['code'];
//        $order->transaction_id  = $data['transaction_id'];
//        $order->user_id         = $data['user_id'];
//        $order->goods_id        = $data['goods_id'];
//        $order->price           = $data['price'];
//        $order->number          = $data['number'];
//        $order->amount          = $data['amount'];
//        $order->pay_type        = $data['pay_type'];

        //更新订单状态
//        $order->status = order::STATUS_FINISHED;
//        $order->save();

        $u = Auth::guard('api')->user();
        $u->coin += $coin;
        $u->save();
        //记日志
        $_types = AccountLog::getAllType();
        $_ops = AccountLog::getAllop();
        $this->logAccount($_types[AccountLog::TYPE_CASH], $u->id, $u->name,$coin,
            $_ops[AccountLog::OP_CHARGE],
            $_types[AccountLog::TYPE_COIN], $u->id,$u->name,$coin,$data['number'],'http://miao.beiyuesi.com/base/img/apple.png');

        Log::info('支付完成');

        return $this->api('');
    }
}
