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

namespace Biz\Pay\Payment;


use Biz\Pay\NewBasePayment;

class JichuangPayment extends NewBasePayment
{

    /**
     * 接收通知
     * @param array $data
     * @return void
     */
    function notify(array $data)
    {
        $arr['tradeno'] = trim(strip_tags($data['tradeno']));
        $arr['outtradeno'] = trim(strip_tags($data['outtradeno']));
        $arr['amount'] = trim(strip_tags($data['amount']));
        $arr['partner'] = trim(strip_tags($data['partner']));
        $arr['inttime'] = trim(strip_tags($data['inttime']));
        $order_no = $data['outtradeno'];
        $price = $real_price = (float)trim(strip_tags($data['amount'])) / 100;
        $bill_no = $data['tradeno'];
        $sign = trim(strip_tags($data['sign']));
        $this->checkSign($arr, $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 = array(
            'tradeno' => $this->dataAccess->orderSn,
            'tradename' => $this->dataAccess->goodsName ?? 'GoodsName',
            'amount' => $this->dataAccess->money * 100,
            'partner' => $this->paymentConfig['merchant_id'],
            'paytype' => $this->dataAccess->payType,
            'inttime' => time(),
            'paynotifyurl' => $this->notifyUrl,
        );
        $str = "tradeno=" . $param['tradeno'] . "&tradename=" . $param['tradename'] . "&amount=" . $param['amount'] . "&partner=" . $param['partner'] . "&paytype=" . $param['paytype'] . "&inttime=" . $param['inttime'] . "&paynotifyurl=" . $param['paynotifyurl'];
        $pay_key = "&key={$this->paymentConfig['merchant_md5_secret']}";
        $param['signinfo'] = md5($str . $pay_key);
        return $param;
    }

    /**
     * 生成签名函数
     * @param array $param
     * @param string $merchantSecret
     * @return mixed
     */
    function buildSign(array $param, $merchantSecret)
    {
        $signValue = "tradeno=" . $param['tradeno'] . "&outtradeno=" . $param['outtradeno'] . "&amount=" . $param['amount'] . "&partner=" . $param['partner'] . "&inttime=" . $param['inttime'] . "&key=" . $merchantSecret;
        return md5($signValue);
    }

    /**
     * 基础配置
     * @return void
     */
    function config()
    {
        $this->apiUrl = "https://www.89988buy.com/media.php/sdk2/pay/";
        $this->success = "success";
    }

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

    /**
     * 发起支付函数
     * @return mixed
     */
    function goPay()
    {
        $order_info = $this->buildOrder();
        $json = $this->goPayCurl($this->apiUrl, $order_info);
        $ret = json_decode($json, 1);
        if (!$ret || $ret['retcode'] !== 1) {
            Render('', -5989, $ret['msg'] ?? '');
        }
        $this->apiUrl = $ret['payurl'];
        $this->method = 'GET';
        $this->prePay(1);
    }
}