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

namespace Biz\Pay\Payment;


use Biz\Pay\NewBasePayment;

class QuanyinPayment extends NewBasePayment
{

    /**
     * 接收通知
     * @param array $data
     * @return void
     */
    function notify(array $data)
    {
        $arr['payKey'] = strip_tags(trim($data['payKey']));
        $arr['productName'] = strip_tags(trim($data['productName']));
        $arr['orderNo'] = strip_tags(trim($data['orderNo']));
        $arr['orderPrice'] = strip_tags(trim($data['orderPrice']));
        $arr['payWayCode'] = strip_tags(trim($data['payWayCode']));
        $arr['payPayCode'] = strip_tags(trim($data['payPayCode']));
        $arr['orderDate'] = strip_tags(trim($data['orderDate']));
        $arr['orderTime'] = strip_tags(trim($data['orderTime']));
        $arr['remark'] = strip_tags(trim($data['remark']));
        $arr['trxNo'] = strip_tags(trim($data['trxNo']));
        $arr['field1'] = strip_tags(trim($data['field1']));
        $arr['tradeStatus'] = strip_tags(trim($data['tradeStatus']));
        $sign = $data['sign'];
        $order_no = $arr['orderNo'];
        $price = $real_price = $arr['orderPrice'];
        $bill_no = $arr['trxNo'];
        if (!$arr['tradeStatus'] == 'SUCCESS') die('ERROR');
        $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
    {
        $arr['payKey'] = $this->paymentConfig['merchant_id'];
        $arr['payWayCode'] = 'ZITOPAY';
        $arr['orderNo'] = $this->dataAccess->orderSn;
        $arr['orderPrice'] = $this->dataAccess->money;
        $arr['productName'] = $this->dataAccess->goodsName ?? 'GoodsName';
        $arr['payTypeCode'] = $this->dataAccess->payType;
        $arr['notifyUrl'] = $this->notifyUrl;
        $arr['returnUrl'] = $this->redirectUrl;
        $arr['orderIp'] = $this->getRealIp();
        $arr['orderDate'] = date('Ymd', time());
        $arr['orderTime'] = date('YmdHis', time());
        $arr['orderPeriod'] = 30;
        $arr['remark'] = $this->dataAccess->goodsName ?? 'GoodsName';
        $arr['field1'] = $this->dataAccess->goodsName ?? 'GoodsName';
        $arr['sign'] = $this->buildSign($arr, $this->paymentConfig['merchant_md5_secret']);
        return $arr;
    }

    /**
     * 生成签名函数
     * @param array $param
     * @param string $merchantSecret
     * @return mixed
     */
    function buildSign(array $param, $merchantSecret)
    {
        ksort($param);
        $a = '';
        foreach ($param as $x => $x_value) {
            if ($x_value) {
                $a = $a . $x . '=' . $x_value . '&';
            }
        }
        return strtoupper(md5($a . 'paySecret=' . $merchantSecret));
    }

    /**
     * 基础配置
     * @return void
     */
    function config()
    {
        $this->apiUrl = "http://api.quanyinzf.com:8050/rb-pay-web-gateway/scanPay/initPayIntf";
    }

    /**
     * 设置返回类型
     * @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['result'] !== 'success' || !$ret['code_url']) {
            Render('', -5989);
        }
        $this->apiUrl = $ret['code_url'];
        $dpgateName = '';
        switch ($this->dataAccess->payType) {
            case 'ZITOPAY_ALI_SCAN':
                $dpgateName = '支付宝';
                break;
            case 'ZITOPAY_WX_SCAN':
                $dpgateName = '微信';
                break;
            case 'ZITOPAY_QQ_SCAN':
                $dpgateName = 'QQ';
                break;
            case 'JPAY_JDPAY':
                $dpgateName = '京东钱包';
                break;
            case 'ZITOPAY_UNION_SCAN':
                $dpgateName = '银联';
                break;
            default:
                $this->prePay(1);
        }

        $this->prePay(1, $this->apiUrl, $dpgateName);
    }
}