Abstract.php 4.83 KB
<?php

/**
 * Created by PhpStorm.
 * User: ziy
 * Date: 14-7-8
 * Time: 下午6:09
 */
abstract class YOHOPay_Package_Abstract implements YOHOPay_Package_Interface
{

    /**
     * 用于存储通知回调的基础url
     * @var string
     */
    public $baseNoticeUrl = "http://www.yhfair.com/pay/";

    /**
     * 测试的回调地址
     * @var string
     */
    public $testBaseNoticeUrl = "http://www.yohomarket.cn/pay/";

    /**
     * 支付配置
     * @var YOHOPay_Package_payConfig
     */
    protected $payConfig;

    /**
     * 通知
     * @param array $notifyData
     * @return string
     */
    function notify(array $notifyData)
    {
        return '';
    }

    /**
     * 获取参数
     * @return $this
     */
    function getParameters()
    {
        /* GET */
        foreach ($_GET as $k => $v) {
            $this->setParameter($k, $v);
        }
        /* POST */
        foreach ($_POST as $k => $v) {
            $this->setParameter($k, $v);
        }
        return $this;
    }


    /**
     * 设置参数值
     * @param $parameter
     * @param $parameterValue
     */
    function setParameter($parameter, $parameterValue)
    {
        $this->parameters[$parameter] = $parameterValue;
    }


    /**
     * 实现多种字符编码方式
     * @param $input 需要编码的字符串
     * @param $_output_charset 输出的编码格式
     * @param $_input_charset 输入的编码格式
     * return 编码后的字符串
     */
    function charsetEncode($input, $_output_charset, $_input_charset)
    {
        if (!isset($_output_charset)) $_output_charset = $_input_charset;
        if ($_input_charset == $_output_charset || $input == null) {
            $output = $input;
        } elseif (function_exists("mb_convert_encoding")) {
            $output = mb_convert_encoding($input, $_output_charset, $_input_charset);
        } elseif (function_exists("iconv")) {
            $output = iconv($_input_charset, $_output_charset, $input);
        } else die("sorry, you have no libs support for charset change.");
        return $output;
    }

    /**
     * 实现多种字符解码方式
     * @param $input 需要解码的字符串
     * @param $_output_charset 输出的解码格式
     * @param $_input_charset 输入的解码格式
     * return 解码后的字符串
     */
    function charsetDecode($input, $_input_charset, $_output_charset)
    {
        if (!isset($_input_charset)) $_input_charset = $_input_charset;
        if ($_input_charset == $_output_charset || $input == null) {
            $output = $input;
        } elseif (function_exists("mb_convert_encoding")) {
            $output = mb_convert_encoding($input, $_output_charset, $_input_charset);
        } elseif (function_exists("iconv")) {
            $output = iconv($_input_charset, $_output_charset, $input);
        } else die("sorry, you have no libs support for charset changes.");
        return $output;
    }

    /**
     * 排序参数
     * @param array $package
     * @return array
     */
    function packageSort(array $package)
    {
        ksort($package);
        reset($package);
        return $package;
    }

    /**
     * 订单通知
     * @param YOHOPay_Package_Response $responseData
     */
    function orderNotify(YOHOPay_Package_Response $responseData)
    {
        if (empty($responseData->orderCode)) {
            return false;
        }
        $checkStatus = YHMOrders_Models_Status_Client::getRepeatStatusByOrderCode($responseData->orderCode, 300);
        if (empty($checkStatus)) {
            YHMOrders_Models_Orders_Client::updateOrderPayInfo($responseData->orderCode, $responseData->paymentID, $responseData->tradeCode, $responseData->buyerUser, $responseData->bankName);
            YHMOrders_Models_Status_Client::setOrderstatus($responseData->orderCode, 300, 0);
            YHMIm_Messages::pay($responseData->orderCode);
        }
    }

    /**
     * 订单通知
     * @param YOHOPay_Package_Response $responseData
     */
    function orderNotifyTodo($orderCode)
    {
        $checkStatus = YHMOrders_Models_Status_Client::getRepeatStatusByOrderCode($orderCode, 300);
        if (empty($checkStatus)) {
            YHMOrders_Models_Orders_Client::updateOrderPayInfo($orderCode, 1, '', '');
            YHMOrders_Models_Status_Client::setOrderstatus($orderCode, 300, 0);
        }
    }

    function makeResponseParameters(array $parametersData)
    {
        $response = new YOHOPay_Package_Response();
        $response->orderCode = $parametersData['out_trade_no'];
        $response->totalFee = $parametersData['total_fee'];
        $response->buyerUser = $parametersData['buyer_email'];
        $response->paymentID = 1;
        $response->paymentTime = empty($parametersData['gmt_payment']) ? 0 : strtotime($parametersData['gmt_payment']);
        $response->tradeCode = $parametersData['trade_no'];
        return $response;
    }
}