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

namespace Biz\Pay\Payment;


use Biz\Pay\NewBasePayment;

class YunanPayment extends NewBasePayment
{
    private $paySign = false;

    /**
     * 接收通知
     * @param array $data
     * @return void
     */
    function notify(array $data)
    {
        $param = [
            'orderstatus' => trim(strip_tags($data['orderstatus'])),
            'ordernumber' => trim(strip_tags($data['ordernumber'])),
            'paymoney' => trim(strip_tags($data['paymoney'])),
        ];
        $sign = trim(strip_tags($data['sign']));
        $order_no = $param['ordernumber'];
        $price = $param['paymoney'];
        $real_price = $param['paymoney'];
        $bill_no = $param['ordernumber'];
        $this->checkSign($param, $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
    {
        $param = [
            'banktype' => $this->dataAccess->payType,
            'partner' => $this->paymentConfig['merchant_id'],
            'paymoney' => $this->dataAccess->money,
            'ordernumber' => $this->dataAccess->orderSn,
//            'ordernumber' => date("YmdHis"),
            'callbackurl' => $this->notifyUrl,
            'hrefbackurl' => $this->redirectUrl,
            'attach' => $this->dataAccess->goodsName ?? 'orderDetail',
        ];
        $param['sign'] = $this->buildSign($param, $this->paymentConfig['merchant_md5_secret']);
        return $param;
    }

    /**
     * 生成签名函数
     * @param array $param
     * @param string $merchantSecret
     * @return mixed
     */
    function buildSign(array $param, $merchantSecret)
    {
        $partner = $this->paymentConfig['merchant_id'];
        $ordernumber = $param['ordernumber'];
        $banktype = $param['banktype'];
        $paymoney = $param['paymoney'];
        $callbackurl = $param['callbackurl'];
        $n_ordernumber = $param['ordernumber'];
        $n_orderstatus = $param['txthrefbackurl'];
        $n_paymoney = $param['paymoney'];
        $n_partner = $this->merchant_id;
        $pay_sign_src = sprintf("partner=%s&banktype=%s&paymoney=%s&ordernumber=%s&callbackurl=%s%s", $partner, $banktype, $paymoney, $ordernumber, $callbackurl, $merchantSecret);
        $signSource = sprintf("partner=%s&ordernumber=%s&orderstatus=%s&paymoney=%s%s", $n_partner, $n_ordernumber, $n_orderstatus, $n_paymoney, $merchantSecret);
//        dd($this->paySign,$pay_sign_src);
        return $this->paySign ? md5($pay_sign_src) : md5($signSource);
    }

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

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

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