<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/5
 * Time: 18:03
 */

namespace Biz\Pay\Payment;


use Biz\Pay\NewBasePayment;

class YiyunPayment extends NewBasePayment
{
    private $paySign = false;

    /**
     * 接收通知
     * @param array $data
     * @return void
     */
    function notify(array $data)
    {
        foreach ($data as $k => $v) {
            $data[$k] = trim(strip_tags($v));
        }
        $order_no = $data['ordernumber'];
        $price = $real_price = $data['paymoney'];
        $bill_no = $data['sysnumber'];
        $sign = trim(strip_tags($data['sign']));
        $this->checkSign($data, $sign, $order_no, $price, $real_price, $is_bill_no = 0, $bill_no);
    }

    /**
     * 返回跳转
     * @param array $data
     * @return void [type] [description]
     */
    function redirect(array $data)
    {
        // TODO: Implement redirect() method.
    }

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

    /**
     * 构建订单提交数组
     * @return array
     */
    function buildOrder(): array
    {
        $arr['version'] = '3.0';
        $arr['method'] = 'yy.online.interface';
        $arr['partner'] = $this->paymentConfig['merchant_id'];
        $arr['ordernumber'] = $this->dataAccess->orderSn;//已存在该变量
        $arr['paymoney'] = $this->dataAccess->money;
        $arr['callbackurl'] = $this->notifyUrl;
        $arr['hrefbackurl'] = $this->redirectUrl;
        $arr['banktype'] = $this->dataAccess->payType;
        $arr['isshow'] == 1;
        $arr['attach'] = $this->dataAccess->goodsName ?? 'GoodsDetail';
        $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 ($this->paySign) {
            return $this->buildPaySign($param, $merchantSecret);
        }
        $partner = trim(strip_tags($param['partner']));
        $ordernumber = trim(strip_tags($param['ordernumber']));
        $paymoney = trim(strip_tags($param['paymoney']));
        $orderstatus = trim(strip_tags($param['orderstatus']));
        $signText = "partner=" . $partner . "&ordernumber=" . $ordernumber . "&orderstatus=" . $orderstatus . "&paymoney=" . $paymoney . $merchantSecret;
        return strtolower(md5($signText));
    }

    function buildPaySign($param, $merchantSecret)
    {
        $version = $param['version'];
        $method = $param['method'];
        $partner = $param['partner'];
        $banktype = $param['banktype'];
        $paymoney = $param['paymoney'];
        $ordernumber = $param['ordernumber'];
        $callbackurl = $param['callbackurl'];
        $signText = "version=" . $version . "&method=" . $method . "&partner=" . $partner . "&banktype=" . $banktype . "&paymoney=" . $paymoney . "&ordernumber=" . $ordernumber . "&callbackurl=" . $callbackurl . $merchantSecret;
        return strtolower(md5($signText));

    }

    /**
     * 基础配置
     * @return void
     */
    function config()
    {
        $this->apiUrl = 'http://open.yyy114.com/online/gateway';
        $this->success='ok';
    }

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

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