<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/31
 * Time: 17:28
 */

namespace Biz\Pay\Payment;

use Biz\Pay\NewBasePayment;
use Biz\Pay\PayInfo;

class QianlaibaoPayment extends NewBasePayment
{

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

    /**
     * 接收通知
     * @param array $data
     * @return void [type] [description] 返回为1或success视接口而定
     */
    function notify(array $data)
    {
        $param = [
            'version' => strip_tags($data['version']),
            'merchant_no' => strip_tags($data['merchant_no']),
            'out_trade_no' => strip_tags($data['out_trade_no']),
            'payment_type' => strip_tags($data['payment_type']),
            'user_account' => strip_tags($data['user_account']),
            'trade_no' => strip_tags($data['trade_no']),
            'trade_status' => strip_tags($data['trade_status']),
            'notify_time' => urldecode(strip_tags($data['notify_time'])),
            'body' => strip_tags($data['body']),
            'total_fee' => strip_tags($data['total_fee']),
            'obtain_fee' => strip_tags($data['obtain_fee']),
        ];
//        dd($param);
        $order_no = $param['out_trade_no'] ?? '';
        $price = $param['total_fee'] ? $param['total_fee'] : 0;
        $real_price = $param['obtain_fee'] ? $param['obtain_fee'] : 0;
        $sign = strip_tags(trim($data['sign']));
        $this->checkSign($param, $sign, $order_no, $price, $real_price, $is_bill_no = 0);
    }

    /**
     * 同步跳转
     * @param array $data
     * @return void [type] [description]
     */
    function redirect(array $data)
    {
        $html = <<<EOT
        <script>setTimeout(function() {
          window.history.go(-2);
        },3000);</script>
EOT;
        echo '支付已受理。如有疑问请联系客服！3秒后返回支付页，请稍候...';
        echo $html;
    }

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

    /**
     * 返回支付类型的数组
     * @return void [type] [description]
     */
    function getPaymentType()
    {
        // TODO: Implement getPaymentType() method.
    }

    /**
     * 构建订单提交数组
     * @return array
     */
    function buildOrder(): array
    {
        $orderInfo = [
            'merchant_no' => $this->paymentConfig['merchant_id'],
            'version' => '1.0',
            'out_trade_no' => $this->dataAccess->orderSn,
            'payment_type' => $this->dataAccess->payType,
            'notify_url' => $this->notifyUrl,
            'page_url' => $this->redirectUrl,
            'total_fee' => number_format($this->dataAccess->money, 2, '.', ''),
            'trade_time' => date('YmdHis'),
            'user_account' => 'UID' . time(),
            'body' => $this->dataAccess->goodsName ?? 'testGoods',
        ];
        $sign = $this->buildSign($orderInfo, $this->paymentConfig['merchant_md5_secret']);
        $orderInfo['sign'] = $sign;
        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')
    {
        // TODO: Implement setReturnType() method.
        $this->returnType = $type;
    }

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

}