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

namespace Biz\Pay\Payment;


use Biz\Pay\NewBasePayment;

class CypalPayment extends NewBasePayment
{

    /**
     * 接收通知
     * @param array $data
     * @return void
     */
    function notify(array $data)
    {
        $param = [
            'orderNo' => trim(strip_tags($data['orderNo'])),
            'partnerOrderNo' => trim(strip_tags($data['partnerOrderNo'])),
            'status' => trim(strip_tags($data['status'])),
            'timestamp' => trim(strip_tags($data['timestamp'])),
            'attach' => trim(strip_tags($data['attach'])),
            'money' => trim(strip_tags($data['money'])),
            'sign' => trim(strip_tags($data['signature'])),
        ];
        $sign = $param['sign'];
        unset($param['sign']);
        $order_no = $param['partnerOrderNo'];
        $price = $param['money'];
        $real_price = $param['money'];
        $bill_no = $param['orderNo'];
        $this->checkSign($param, $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
    {
        $orderInfo = array(
            'amount' => number_format($this->dataAccess->money, 2, '.', ''),
            'partnerOrderNo' => $this->dataAccess->orderSn,
            'goodsDesc' => $this->dataAccess->goodsName ?? 'TestGoods',
            'attach' => $this->dataAccess->extra ?? 'TestGoodsRemark',
            'timestamp' => time() . '000',
            'returnUrl' => $this->redirectUrl,
            'notifyUrl' => $this->notifyUrl,
            'partnerCode' => $this->paymentConfig['merchant_id'],
            'payType' => $this->dataAccess->payType,
            'userUnqueNo' => '' . time(),
        );
        $sign = $this->buildSign2($orderInfo, $this->paymentConfig['merchant_md5_secret']);
        $orderInfo['signature'] = $sign;
        return $orderInfo;
    }

    /**
     * 生成签名函数
     * @param array $param
     * @param string $merchantSecret
     * @return mixed
     */
    function buildSign(array $param, $merchantSecret)
    {
        if (!is_array($param)) {
            return false;
        }
        $signStr = md5($param['userid'] . $param['timestamp']) . $merchantSecret;
        return strtoupper(md5($signStr));
    }

    /**
     * 发起支付签名函数
     * @param array $param
     * @param $merchantSecret
     * @return bool|string
     */
    function buildSign2(array $param, $merchantSecret)
    {
        if (!is_array($param)) {
            return false;
        }
        $signStr = md5($param['partnerCode'] . $param['timestamp']) . $merchantSecret;
        return strtoupper(md5($signStr));
    }

    /**
     * 基础配置
     * @return void
     */
    function config()
    {
        $this->apiUrl = 'http://www.cypal.cn/open/otc/unify_order';
        $this->setReturnType();//根据客户端类型返回支付接口类型
        $this->success = 'SUCCESS';
    }

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

    /**
     * 发起支付函数
     * @return mixed
     */
    function goPay()
    {
        $this->debug = 0;
        $this->method = "GET";
        $this->prePay();
    }
}