<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/31
 * Time: 17:28
 */

namespace Biz\Pay\Payment;

use Biz\Pay\NewBasePayment;
use stdClass;

class XinhuiyidaiPayment extends NewBasePayment
{
    function config()
    {
        $this->apiUrl = 'http://www.xhfpay2018.com/channel/payment';
        $this->setReturnType();//根据客户端类型返回支付接口类型
        $this->success = 'success';
    }

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

//http://www.localdev.com/Payment-Pay/Notify?paymentName=Xinhuiyidai&channel_id=4&mchid=10902&order_id=XC190222163222D150&order_no=XHF20190222163250376693&total_amount=1.00&sign=EDF8083DE10AC714FFE818C30F897164A471B514&
//channel_id=4&mchid=10902&order_id=XC190222163222D150&order_no=XHF20190222163250376693&total_amount=1.00&sign=EDF8083DE10AC714FFE818C30F897164A471B514&
//2019-02-22 16:33:04|para[channel_id=4&mchid=10902&order_id=XC190222163222D150&order_no=XHF20190222163250376693&total_amount=1.00&sign=EDF8083DE10AC714FFE818C30F897164A471B514&]|sign[EDF8083DE10AC714FFE818C30F897164A471B514]|other[EDF8083DE10AC714FFE818C30F897164A471B514]

    function notify(array $data)
    {
        $channel_code = $data['channel_id'];
        $gateway_identifier = $data['paymentName'];
        $param = [
            'channel_id' => strip_tags($data['channel_id']),
            'mchid' => strip_tags($data['mchid']),
            'order_id' => strip_tags($data['order_id']),
            'order_no' => strip_tags($data['order_no']),
            'total_amount' => strip_tags($data['total_amount']),
        ];
        $order_no = $param['order_id'] ?? '';
        $price = $param['total_amount'] ? $param['total_amount'] : 0;
        $real_price = $param['total_amount'] ? $param['total_amount'] : 0;
        $sign = strip_tags(trim($data['sign']));
        $this->checkSign($param, $sign, $channel_code, $gateway_identifier, $order_no, $price, $real_price);
    }

    /**
     * 同步跳转
     * @param array $data
     * @return void [type] [description]
     */
    function redirect(array $data)
    {
        echo 'success';
    }

    /**
     * 检查支付信息
     * @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 = [
            'mchid' => $this->paymentConfig['merchant_id'],
            'order_id' => $this->dataAccess->orderSn,
            'total_amount' => number_format($this->dataAccess->money, 2, '.', ''),
            'channel_id' => $this->dataAccess->payType,
            'return_url' => $this->redirectUrl,
            'notify_url' => $this->notifyUrl,
        ];
        $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, SORT_NATURAL | SORT_FLAG_CASE);
        $signStr = '';
        foreach ($param as $k => $v) {
//        if (!$v) continue;
            $signStr = $signStr . $k . $v;
        }
        // $signStr = trim($signStr, '&') . $merchantSecret;
        $signStr = $signStr . $merchantSecret;

        return (strtoupper(sha1($signStr)));
    }

    /**
     * 设置返回类型
     * @param string $type
     */
    function setReturnType($type = 'HTML')
    {
        $this->returnType = $type;
    }

    /**
     * 发起支付函数
     */
    function goPay()
    {
        $this->debug = 1;
        $orderInfo = $this->buildOrder();
        $json = $this->goPayCurl($this->apiUrl, $orderInfo);
        $ret = json_decode($json, 1);
        if (!$ret || !$ret['status']) {
            Render(new stdClass(), -10086, $ret['msg']??'未知错误');
        }
        $apiUrl = $ret['url'];
        $this->apiUrl = $apiUrl;
        $this->orderInfo = [];
//        $this->method='JSON';
        $this->prePay(1);
    }


}