<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/28
 * Time: 14:06
 */

namespace Biz\Pay\Payment;

//壹玖支付
use Biz\Pay\NewBasePayment;

class YijiuPayment extends NewBasePayment
{

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

    /**
     * 接收通知
     * @param array $data
     * @return void [type] [description] 返回为1或success视接口而定
     */
    function notify(array $data)
    {
        //验证签名后返回对端服务器需要的响应内容
        $param = array(
            'myid' =>'644089',
            'mykey' =>'4C3BFF6F72D887A51FA11A4962BF91F8',
            'out_trade_no' => strip_tags($data['out_trade_no']),
            'money' => strip_tags($data['money']),
            'coodkey' => strip_tags($data['coodkey']),
            'oktime' => strip_tags($data['oktime']),
            'zt' => strip_tags($data['zt']),
        );

        $order_no = $param['out_trade_no'] ?? '';
        $price = $param['money'] ? $param['money'] : 0;
        $sign = strip_tags(trim($data['sign']));
        $this->checkSign($param, $sign, $order_no, $price);
    }

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

    /**
     * 检查支付信息
     * @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 = [
          'myid' =>$this->paymentConfig['merchant_id'],
          'mykey' =>$this->paymentConfig['merchant_md5_secret'],
          'dingdan' =>$this->dataAccess->orderSn,
          'money' => number_format($this->dataAccess->money, 2, '.', ''),
          'time' => time(),
      ];

        $sign = $this->buildSign($orderInfo, $this->paymentConfig['merchant_md5_secret']);
        $orderInfo['sign'] = $sign;
        $orderInfo['cmd']='data501pay';
        $orderInfo['qudao']=$this->dataAccess->payType;
        $orderInfo['name']='d';
        $orderInfo['returl']=$this->redirectUrl;
        $orderInfo['notifyurl']=$this->notifyUrl;
        return $orderInfo;
    }

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

        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();*/
        $orderInfo = $this->buildOrder();

        $json = $this->goPayCurl($this->apiUrl, $orderInfo);

        $ret = json_decode($json, 1);

        if (!$ret || $ret['data1'] !== 1) {
            Render('', -5989, $ret['msg'] ?? '');
        }
        $this->apiUrl = $ret['data2'];
        $this->method = 'GET';
        $this->prePay(0);
    }

}