<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/7
 * Time: 14:48
 */

namespace Biz\Pay\Payment;


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

class P686Payment extends NewBasePayment
{

    /**
     * 接收通知
     * @param array $data
     * @return void
     */
    function notify(array $data)
    {
        $param = array(
            'outOrderNo' => strip_tags($data['outOrderNo']),
            'goodsClauses' => strip_tags($data['goodsClauses']),
            'tradeAmount' => strip_tags($data['tradeAmount']),
            'shopCode' => strip_tags($data['shopCode']),
            'code' => strip_tags($data['code']),
            'nonStr' => strip_tags($data['nonStr']),
            'msg' => strip_tags($data['msg']),
        );
        $sign = trim(strip_tags($data['sign']));
        $order_no = $data['outOrderNo'];
        $price = $data['tradeAmount'];
        $real_price = $data['tradeAmount'];
        $bill_no = $data['outOrderNo'];
        $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)
    {

    }

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

    }

    /**
     * 构建订单提交数组
     * @return array
     */
    function buildOrder(): array
    {
        $arr = array(
            'code' => $this->paymentConfig['merchant_id'],
            'tradeAmount' => $this->dataAccess->money,
            'payCode' => $this->dataAccess->payType,
            'notifyUrl' => $this->notifyUrl,
            'goodsClauses' => $this->dataAccess->goodsName ?? 'GoodsName',
            'outOrderNo' => $this->dataAccess->orderSn,
        );
        $arr['sign'] = $this->buildSign($arr, $this->paymentConfig['merchant_md5_secret']);
        return $arr;
    }

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

    /**
     * 重写基类跳转函数，若直接跳转收银台类接口不需要重写本函数
     * @param PayInfo $pay_info
     */
    protected function jump(PayInfo $pay_info)
    {
        $json = $this->goPayCurl($this->apiUrl, $pay_info->orderInfo);
        $ret = json_decode($json, 1);
        if (!$ret || $ret['payState'] === 'fail') {
            die($ret['message'] ?? '请求失败！请重试！');
        }
        $dpgateName = '';//自定义二维码页面扫码方式中文名称
        switch ($pay_info->orderInfo['payCode']) {
            case 'wxh5':
            case 'alipay_h5':
            case "wxpay_wap":
                header('Location:' . $linkUrl = $ret['url']);//H5跳转
                break;
            case 'alipay':
                $dpgateName = '支付宝';
                break;
            case 'wxpay':
                $dpgateName = '微信';
                break;
        }
        $qrcodeUrl = $ret['qrCodeUrl'];
        $this->prePay(1, $qrcodeUrl, $dpgateName);
    }

    /**
     * 基础配置
     * @return void
     */
    function config()
    {
        $this->apiUrl = "http://120.77.165.254/Tips.php/686cz/trade/pay";
        $this->success = "SUCCESS";
    }

    /**
     * 设置返回类型
     * @param string $type
     */
    function setReturnType($type = 'HTML')
    {

    }

    /**
     * 发起支付函数
     * @return mixed
     */
    function goPay()
    {
        return $this->prePay();
    }
}