<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/21
 * Time: 18:24
 */

namespace Biz\Pay\Payment;

use Biz\Pay\NewBasePayment;

class LvbaoPayment extends NewBasePayment
{

    /**
     * 基础配置
     * @return void
     */
    function config()
    {
        switch ($this->paymentConfig['gateway_code']) {
            case 'alipay':
            default:
                $this->apiUrl = 'http://pay.teamoe.cn/pay/pay/htmlalipaysubmit';
                break;
            case 'wxpay':
                $this->apiUrl = 'http://pay.teamoe.cn/pay/pay/htmlweixinsubmit';
                break;
        }

        $this->setReturnType();//根据客户端类型返回支付接口类型
    }

    /**
     * 接收通知
     * @param array $data
     * @return void [type] [description] 返回为1或success视接口而定
     */
    function notify(array $data)
    {
        //验证签名后返回对端服务器需要的响应内容
        $param = array(
            'out_trade_no' => strip_tags(trim($data['out_trade_no'])),
            'total_fee' => strip_tags(trim($data['total_fee'])),
            'trade_status' => strip_tags(trim($data['trade_status'])),
            'custom' => strip_tags(trim($data['custom'])),
        );
        $order_no = $param['out_trade_no'] ?? '';
        $price = $param['total_fee'] ? $param['total_fee'] : 0;
        $real_price = $param['total_fee'] ? $param['total_fee'] : 0;
        $sign = strip_tags(trim($data['sign']));
        $this->checkSign($param, $sign, $order_no, $price, $real_price, false);
    }

    /**
     * 同步跳转
     * @param array $data
     * @return void [type] [description]
     */
    function redirect(array $data)
    {
        echo 'success';
    }

    /**
     * 检查支付信息
     * @return void [type]            0=失败，1=成功
     */
    function check()
    {
        // TODO: Implement check() method.
    }

    /**
     * 构建订单提交数组
     * @return array
     */
    function buildOrder(): array
    {
        $orderInfo = [
            'partner' => $this->paymentConfig['merchant_id'],
            'out_trade_no' => $this->dataAccess->orderSn,
            'exter_invoke_ip' => $this->getRealIp(),
            'total_fee' => $this->dataAccess->money,
            'custom' => $this->dataAccess->remark,
            'notify_url' => $this->notifyUrl,
            'return_url' => $this->redirectUrl,
        ];

        $sign = $this->buildSign($orderInfo, $this->paymentConfig['merchant_md5_secret']);
        $orderInfo['sign'] = $sign;
//        dd(var_export($orderInfo),$this->apiUrl);
        return $orderInfo;
    }

    /**
     * 生成签名函数
     * @param array $param
     * @param string $merchantSecret
     * @return mixed
     */
    function buildSign(array $param, $merchantSecret)
    {
        if (!is_array($param)) {
            return false;
        }
        ksort($param);
        $signStr = '';
        foreach ($param as $k => $v) {
            if (!$v) continue;
            $signStr .= "{$k}={$v}&";
        }
        $signStr = trim($signStr, '&') . $merchantSecret;
        return (md5($signStr));
    }

    /**
     * 设置返回类型
     * @param string $type
     */
    function setReturnType($type = 'HTML')
    {
        $this->returnType = $type;
    }

    /**
     * 发起支付函数
     */
    function goPay()
    {
        $this->debug = 0;
        $this->method = "POST";
        $this->prePay();
    }

}
