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

namespace Biz\Pay\Payment;


use Biz\Pay\NewBasePayment;

class JubaofuPayment extends NewBasePayment
{

    /**
     * 接收通知
     * @param array $data
     * @return void
     */
    function notify(array $data)
    {
        // TODO: Implement notify() method.
    }

    /**
     * 返回跳转
     * @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
    {
        $data = array(
            'charset' => "UTF-8",
            'merchantCode' => $this->paymentConfig['merchant_id'],
            'orderNo' => $this->dataAccess->orderSn,
            'amount' => $this->dataAccess->money * 100,
            'channel' => 'BANK',
            'bankCode' => $this->dataAccess->payType,
            'remark' => $this->dataAccess->goodsName ?? 'GoodsDetail',
            'notifyUrl' => $this->notifyUrl,
            'returnUrl' => $this->redirectUrl,
            'extraReturnParam' => $this->dataAccess->orderSn,
            'signType' => 'RSA',
        );
        $sign = $this->buildPaySign($data);
        $data['sign'] = $sign;
        return $data;
    }

    /**
     * 生成签名函数
     * @param array $param
     * @param string $merchantSecret
     * @return mixed
     */
    function buildSign(array $param, $merchantSecret)
    {
        $data = $param;
        $signData = "charset={$data['charset']}&merchantCode={$data['merchantCode']}&orderNo={$data['orderNo']}"
            . "&amount={$data['amount']}&channel=BANK&bankCode={$data['bankCode']}&remark={$data['remark']}"
            . "&notifyUrl={$data['notifyUrl']}&returnUrl={$data['returnUrl']}&extraReturnParam={$data['extraReturnParam']}";
        $private = $this->paymentConfig['merchant_pri_key'];
        $pi_key = openssl_pkey_get_private($private);
        openssl_sign($signData, $sign, $pi_key, OPENSSL_ALGO_SHA1);
        $sign = base64_encode($sign);
        $sign = urlencode($sign);
        $sign = str_replace('%2F', '/', $sign);
        $sign = str_replace('%3D', '=', $sign);
        return $sign = str_replace('%2B', '+', $sign);
    }

    /**
     * 生成付款签名函数
     * @param $data
     * @return mixed
     */
    protected function buildPaySign($data)
    {
        $signData = "charset={$data['charset']}&merchantCode={$data['merchantCode']}&orderNo={$data['orderNo']}"
            . "&amount={$data['amount']}&channel=BANK&bankCode={$data['bankCode']}&remark={$data['remark']}"
            . "&notifyUrl={$data['notifyUrl']}&returnUrl={$data['returnUrl']}&extraReturnParam={$data['extraReturnParam']}";
        $private = $this->paymentConfig['merchant_pri_key'];
        $private = $this->formatRsaSecret($private);
        $pi_key = openssl_pkey_get_private($private);
        openssl_sign($signData, $sign, $pi_key, OPENSSL_ALGO_SHA1);
        $sign = base64_encode($sign);
        $sign = urlencode($sign);
        $sign = str_replace('%2F', '/', $sign);
        $sign = str_replace('%3D', '=', $sign);
        return $sign = str_replace('%2B', '+', $sign);
    }

    /**
     * 基础配置
     * @return void
     */
    function config()
    {
        $this->apiUrl = 'https://api.jubaopays.com/gateway/bank';
        $this->success = 'SUCCESS';
    }

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

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

}