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

namespace Biz\Pay\Payment;


use Biz\Pay\NewBasePayment;

class ShuiyifuPayment extends NewBasePayment
{
    private $paySign = false;

    /**
     * 接收通知
     * @param array $data
     * @return void
     */
    function notify(array $data)
    {
        $arr['orderid'] = trim(strip_tags($data['orderid']));
        $arr['opstate'] = trim(strip_tags($data['opstate']));
        $arr['ovalue'] = trim(strip_tags($data['ovalue']));
        $arr['systime'] = trim(strip_tags($data['systime']));
        $arr['sign'] = trim(strip_tags($data['sign']));
        $arr['type'] = trim(strip_tags($data['type']));
        $arr['sysorderid'] = trim(strip_tags($data['sysorderid']));
        $arr['completiontime'] = trim(strip_tags($data['completiontime']));
        $arr['attach'] = trim(strip_tags($data['attach']));
        $arr['msg'] = trim(strip_tags($data['msg']));
        $order_no = $arr['orderid'];
        $price = $arr['ovalue']; //支付价格
        $bill_no = $arr['sysorderid']; //内部订单号
        $sign = trim(strip_tags($data['sign']));
        $this->checkSign($arr, $sign, $order_no, $price, $real_price = 0, $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
    {
        $orderInfo = [
            'parter' => $this->paymentConfig['merchant_id'],
            'value' => $this->dataAccess->money,
            'orderid' => $this->dataAccess->orderSn,
            'type' => $this->dataAccess->payType,
            'attach' => $this->dataAccess->extra ?? 'testGoods',
            'callbackurl' => $this->notifyUrl,
            'hrefbackurl' => $this->redirectUrl,
        ];
        $this->paySign = true;
        $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)
    {
        $signValue = "orderid={$param['orderid']}&opstate={$param['opstate']}&ovalue={$param['ovalue']}{$merchantSecret}";//异步签名串
        $signStr = "parter={$param['parter']}&type={$param['type']}&value={$param['value']}&orderid={$param['orderid']}&callbackurl={$param['callbackurl']}{$merchantSecret}";
        return $this->paySign ? md5($signStr) : md5($signValue);
    }

    /**
     * 基础配置
     * @return void
     */
    function config()
    {
        $this->apiUrl = 'https://gateway.easyipay.com/interface/AutoBank/index.aspx';
        $this->success = 'opstate=0';
    }

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

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