<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/23
 * Time: 13:45
 */

namespace Biz\Pay\Payment;

use Biz\Pay\NewBasePayment;
use Biz\Pay\PayInfo;
use stdClass;

class QwjPayment extends NewBasePayment
{

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

    /**
     * 接收通知
     * @param array $data
     * @return void [type] [description] 返回为1或success视接口而定
     */
    function notify(array $data)
    {
        $param = [
            'retCode' => strip_tags($data['retCode']),
            'userId' => strip_tags($data['userId']),
            'orderNo' => strip_tags($data['orderNo']),
            'transNo' => strip_tags($data['transNo']),
            'payAmt' => strip_tags($data['payAmt']),
            'sign' => strip_tags($data['sign']),
        ];
        $order_no = $param['orderNo'] ?? '';
        $bill_no = $param['transNo'] ?? '';
        $price = $param['payAmt'] ? $param['payAmt'] : 0;
        $real_price = $param['payAmt'] ? $param['payAmt'] : 0;
        $sign = strip_tags(trim($param['sign']));
        $this->checkSign($param, $sign, $order_no, $price, $real_price,false,$bill_no);
    }

    /**
     * 同步跳转
     * @param array $data
     * @return void [type] [description]
     */
    function redirect(array $data)
    {
        $html = <<<EOT
        <script>setTimeout(function() {
          window.history.go(-2);
        },3000);</script>
EOT;
        echo '支付已受理。如有疑问请联系客服！3秒后返回支付页，请稍候...';
        echo $html;
    }

    /**
     * 检查支付信息
     * @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 = [
            'userId' => $this->paymentConfig['merchant_id'],
            'orderNo' => $this->dataAccess->orderSn,
            'tradeType' => $this->dataAccess->payType,
            'payAmt' => $this->dataAccess->money,
            'goodsName' => $this->dataAccess->goodsName ?? 'testGoods',
            'notifyUrl' => $this->notifyUrl,
            'returnUrl' => $this->redirectUrl,
        ];
        $sign = $this->create_sign($orderInfo, $this->paymentConfig['merchant_md5_secret']);
        $orderInfo['sign'] = $sign;
        return $orderInfo;
    }
    /**
     * 生成签名函数
     * @param array $param
     * @param string $merchantSecret
     * @return mixed
     */
    function create_sign($postData, $key) {
        $sign_str = '';
        $sign_str = $sign_str . 'notifyUrl=' . $postData['notifyUrl'];
        $sign_str = $sign_str . '&orderNo=' . $postData['orderNo'];
        $sign_str = $sign_str . '&payAmt=' . $postData['payAmt'];
        $sign_str = $sign_str . '&returnUrl=' . $postData['returnUrl'];
        $sign_str = $sign_str . '&tradeType=' . $postData['tradeType'];
        $sign_str = $sign_str . '&userId=' . $postData['userId'];
        $sign_str = $sign_str . '&key=' . $key;
        return (md5($sign_str));
    }
    //回调签名函数
    function buildSign(array $param, $merchantSecret)
    {
        $signStr = '';
        $signStr = $signStr . 'orderNo=' . $param['orderNo'];
        $signStr = $signStr . '&payAmt=' . $param['payAmt'];
        $signStr = $signStr . '&retCode=' . $param['retCode'];
        $signStr = $signStr . '&transNo=' . $param['transNo'];
        $signStr = $signStr . '&userId=' . $param['userId'];
        $signStr = $signStr . '&key=' . $merchantSecret;
        return (md5($signStr));
    }

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

    /**
     * 发起支付函数
     */
    function goPay()
    {
        $this->debug = 0;
        $orderInfo = $this->buildOrder();
        $header = array('Content-Type: application/x-www-form-urlencoded');
        $json = $this->goPayCurl($this->apiUrl, $orderInfo,$header);
        $ret = json_decode($json, 1);
        if (!$ret || $ret['retCode'] != '0') {
            Render(new stdClass(), '-1', $ret['retMsg']);
        }
        $apiUrl = $ret['payUrl'];
        $this->apiUrl = $apiUrl;
        $this->method = 'GET';
        $this->prePay(1);
    }


}
