<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/4
 * Time: 18:40
 */

namespace Biz\Pay\Payment;


use Biz\Pay\NewBasePayment;

class QuanmeiPayment extends NewBasePayment
{

    /**
     * 接收通知
     * @param array $data
     * @return void
     */
    function notify(array $data)
    {
        $param = array(
            'extras' => trim(strip_tags($data['extras'])),
            'merchant_code' => trim(strip_tags($data['merchant_code'])),
            'order_amount' => trim(strip_tags($data['order_amount'])),
            'order_no' => trim(strip_tags($data['order_no'])),
            'order_time' => trim(strip_tags($data['order_time'])),
            'trade_no' => trim(strip_tags($data['trade_no'])),
            'trade_status' => trim(strip_tags($data['trade_status'])),
            'trade_time' => trim(strip_tags($data['trade_time'])),
        );
        $sign = trim(strip_tags($data['sign']));
        $order_no = $param['orderNo'];
        $price = $param['order_amount'];
        $real_price = $param['order_amount'];
        $bill_no = $param['trade_no'];
        $this->checkSign($param, $sign, $order_no, $price, $real_price, $is_bill_no = 0, $bill_no);
    }

    /**
     * 返回跳转
     * @param array $data
     * @return void [type] [description]
     */
    function redirect(array $data)
    {
        // TODO: Implement redirect() method.
    }

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

    /**
     * 构建订单提交数组
     * @return array
     */
    function buildOrder(): array
    {
        $param = array(
            'notify_url' => $this->notifyUrl, // 服务器异步通知地址
            'return_url' => $this->redirectUrl, //页面同步跳转通知地址
            'trade_type' => $this->dataAccess->payType,//1 微信扫码，2 支付宝扫码，3 QQ扫码(暂无) 4微信H5(暂无) 5支付宝H5 6QQ扫码H5(暂无)
            'merchant_code' => $this->paymentConfig['merchant_id'],
            'order_no' => $this->dataAccess->orderSn,
            'amount' => number_format($this->dataAccess->money, 2, '.', ''),
            'source_ip' => $this->getRealIp(), // 商户ip
            'create_time' => date('Y-m-d H:i:s'), //订单时间
            'extras' => $this->dataAccess->extra ?? 'OrderDetail' . time(), // 附加参数
        );
        $param['sign'] = $this->buildSign($param, $this->paymentConfig['merchant_md5_secret']);
        return $param;
    }

    /**
     * 生成签名函数
     * @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) {
                $signStr .= "{$k}={$v}&";
            }
        }
        $signStr .= "key=" . $merchantSecret;
        return md5($signStr);
    }

    /**
     * 基础配置
     * @return void
     */
    function config()
    {
        $this->apiUrl = 'https://api.hpay88.com/gateway';
        $this->setReturnType();//根据客户端类型返回支付接口类型
//        $this->success = 'success';
    }

    /**
     * 设置返回类型
     * @param string $type
     */
    function setReturnType($type = 'HTML')
    {
        // TODO: Implement setReturnType() method.
    }

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