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

namespace Biz\Pay\Payment;

//牵手支付
use Biz\Pay\NewBasePayment;

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

    /**
     * 接收通知
     * @param array $data
     * @return void [type] [description] 返回为1或success视接口而定
     */
    function notify(array $data)
    {
        //验证签名后返回对端服务器需要的响应内容
        $param = array(
            'partner' => strip_tags($data['partner']),
            'ordernumber' => strip_tags($data['ordernumber']),
            'orderstatus' => strip_tags($data['orderstatus']),
            'paymoney' => strip_tags($data['paymoney']),
        );

        $order_no = $param['ordernumber'] ?? '';
        $price = $param['paymoney'] ? $param['paymoney'] : 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 = [
          'version' => '3.0',
          'method' =>'Qs.online.interface',
          'partner' =>$this->paymentConfig['merchant_id'],
          'banktype' => $this->dataAccess->payType,
          'paymoney' => number_format($this->dataAccess->money, 2, '.', ''),
          'ordernumber' =>$this->dataAccess->orderSn,
          'callbackurl'=>$this->notifyUrl,

      ];

        $sign = $this->buildSign($orderInfo, $this->paymentConfig['merchant_md5_secret']);
        $orderInfo['sign'] = $sign;
        $orderInfo['hrefbackurl'] = $this->redirectUrl;
        $orderInfo['attach'] =$this->dataAccess->goodsName ?? 'testGoods';

        return $orderInfo;
    }

    /**
     * 生成签名函数
     * @param array $param
     * @param string $merchantSecret
     * @return mixed
     */
    function buildSign(array $postData, $key)
    {
        if (!is_array($postData)) {
            return false;
        }

        $signStr = '';
        foreach ($postData as $k => $v) {
//        if (!$v) continue;
            $signStr .= "{$k}={$v}&";
        }
        $signStr = trim($signStr, '&') . $key;

        return (md5($signStr));
    }

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

    /**
     * 发起支付函数
     */
    function goPay()
    {
        $orderInfo = $this->buildOrder();
        $this->payInfo->orderInfo = $orderInfo;
        $this->payInfo->apiUrl = $this->apiUrl;
        $this->payInfo->method = 'POST';
        //跳转对方网关时直接订单入库
        $sysOrderInfo = $this->buildOrderInfo();
        $this->addOrder($sysOrderInfo);
        Render($this->payInfo, 1);
    }

}