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

namespace Biz\Pay\Payment;


use Biz\Pay\NewBasePayment;

class WeishaoPayment extends NewBasePayment
{

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

    /**
     * 接收通知
     * @param array $data
     * @return void [type] [description] 返回为1或success视接口而定
     */
    function notify(array $data)
    {

        //验证签名后返回对端服务器需要的响应内容
        $param = array(
            'parter' => strip_tags(trim($data['parter'])),
            'orderid' => strip_tags(trim($data['orderid'])),
            'opstate' => strip_tags(trim($data['opstate'])),
            'ovalue' => strip_tags(trim($data['ovalue'])),
        );
        $order_no = $param['orderid'] ?? '';
        $price = $param['ovalue'] ? $param['ovalue'] : 0;
        $sign = strip_tags(trim($data['sign']));
        $this->checkSign($param, $sign, $order_no, $price, 0, false);
    }

    /**
     * 同步跳转
     * @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 = [
          'parter' => $this->paymentConfig['merchant_id'],
          'value' =>number_format($this->dataAccess->money, 2, '.', '') ,
          'type' =>  $this->dataAccess->payType,
          'orderid' =>  $this->dataAccess->orderSn,
          'notifyurl' => $this->notifyUrl,
          'callbackurl' => $this->redirectUrl,
      ];

        $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 .= 'key='. $merchantSecret;

        return (md5(urldecode($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);
    }

}