IEPayAliPay.php 17.0 KB
Newer Older
D
devil 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace payment;

/**
 * IEPay - 支付宝
 * @author   Devil
 * @blog    http://gong.gg/
 * @version 1.0.0
 * @date    2020-09-22
 * @desc    description
 */
class IEPayAliPay
{
    // 插件配置参数
    private $config;

    /**
     * 构造方法
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-17
     * @desc    description
     * @param   [array]           $params [输入参数(支付配置参数)]
     */
    public function __construct($params = [])
    {
        $this->config = $params;
    }

    /**
     * 配置信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-19
     * @desc    description
     */
    public function Config()
    {
        // 基础信息
        $base = [
            'name'          => 'IEPay支付宝',  // 插件名称
            'version'       => '1.0.0',  // 插件版本
            'apply_version' => '不限',  // 适用系统版本描述
            'apply_terminal'=> ['pc', 'h5', 'ios', 'android'], // 适用终端 默认全部 ['pc', 'h5', 'ios', 'android', 'alipay', 'weixin', 'baidu', 'toutiao']
            'desc'          => '适用PC+H5+APP,即时到帐支付方式,买家的交易资金直接打入卖家账户,新西兰货币支付。 <a href="https://www.mypaynz.com/" target="_blank">立即申请</a>',  // 插件描述(支持html)
            'author'        => 'Devil',  // 开发者
            'author_url'    => 'http://shopxo.net/',  // 开发者主页
        ];

        // 配置信息
        $element = [
            [
                'element'       => 'input',
                'type'          => 'text',
                'default'       => '',
                'name'          => 'mid',
                'placeholder'   => '商户ID',
                'title'         => '商户ID',
                'is_required'   => 0,
                'message'       => '请填写商户ID',
            ],
            [
                'element'       => 'input',
                'type'          => 'text',
                'default'       => '',
                'name'          => 'key',
                'placeholder'   => '密钥',
                'title'         => '密钥',
                'is_required'   => 0,
                'message'       => '请填写密钥',
            ],
D
devil 已提交
83 84
            [
                'element'       => 'select',
D
devil 已提交
85 86 87 88
                'title'         => '支付金额转换',
                'desc'          => '默认否(将金额转为原始汇率的金额、仅单个订单发起支付的时候有效)',
                'message'       => '请选择订单金额转换',
                'name'          => 'is_reverse_price',
D
devil 已提交
89 90 91 92 93 94
                'is_multiple'   => 0,
                'element_data'  => [
                    ['value'=>0, 'name'=>'否'],
                    ['value'=>1, 'name'=>'是'],
                ],
            ],
D
devil 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        ];

        return [
            'base'      => $base,
            'element'   => $element,
        ];
    }

    /**
     * 支付入口
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-19
     * @desc    description
     * @param   [array]           $params [输入参数]
     */
    public function Pay($params = [])
    {
        // 参数
        if(empty($params))
        {
            return DataReturn('参数不能为空', -1);
        }
        
        // 配置信息
        if(empty($this->config))
        {
            return DataReturn('支付缺少配置', -1);
        }

D
devil 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        // 金额转换
        if(isset($this->config['is_reverse_price']) && $this->config['is_reverse_price'] == 1 && !empty($params['business_ids']) && is_array($params['business_ids']) && count($params['business_ids']) == 1 && !empty($params['business_type']))
        {
            switch($params['business_type'])
            {
                // 订单
                case 'system-order' :
                    $currency_data = \app\service\OrderCurrencyService::OrderCurrencyGroupList($params['business_ids'][0]);
                    if(isset($currency_data['currency_rate']) && $currency_data['currency_rate'] > 0)
                    {
                        $params['total_price'] /= $currency_data['currency_rate'];
                    }
                    break;
            }
        }

D
devil 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        // 支付参数
        $parameter = [
            'mid'               =>  $this->config['mid'],
            'total_fee'         => (int) (($params['total_price']*1000)/10),
            'goods'             => $params['name'],
            'out_trade_no'      => $params['order_no'],
            'return_url'        => $params['call_back_url'].'?out_trade_no='.$params['order_no'],
            'notify_url'        => $params['notify_url'],
            'pay_type'          => $this->GetPayType(),
            'version'           => 'v1',
        ];

        // 生成签名
        $parameter['sign'] = $this->SignCreated($parameter);

        // 请求post
        $result = $this->HttpRequest('https://a.mypaynz.com/api/online', $parameter);
        if(isset($result['is_success']) && $result['is_success'] == 'TRUE' && !empty($result['extra']) && !empty($result['extra']['pay_url']))
        {
            return DataReturn('处理成功', 0, $result['extra']['pay_url']);
        }
        return DataReturn(empty($result['message']) ? '支付错误' : $result['message'], -100);
    }

    /**
     * 获取支付平台类型
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2020-09-22
     * @desc    description
     * @param   [string]          $client_type [订单客户端类型]
     */
    private function GetPayType($client_type = '')
    {
        // 平台
        if(empty($client_type))
        {
            $client_type = ApplicationClientType();
        }

        // 终端平台
        $client_type_arr = [
            'pc'        => 'IE0012',
            'h5'        => 'IE0013',
            'ios'       => 'IE0015',
            'android'   => 'IE0015',
        ];
        return isset($client_type_arr[$client_type]) ? $client_type_arr[$client_type] : $client_type_arr['pc'];
    }


    /**
     * 支付回调处理
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-19
     * @desc    description
     * @param   [array]           $params [输入参数]
     */
    public function Respond($params = [])
    {
        // 支付参数
        $data = input();
        if(empty($data['out_trade_no']))
        {
            return DataReturn('支付平台返回的订单号为空', -1);
        }

        // 远程查询支付状态
        $parameter = [
            'mid'               => $this->config['mid'],
            'pay_type'          => $this->GetPayType(),
            'out_trade_no'      => $data['out_trade_no'],
            'version'           => 'v1',
        ];

        // 生成签名
        $parameter['sign'] = $this->SignCreated($parameter);

        // 请求post
        $result = $this->HttpRequest('https://a.mypaynz.com/api/check_order_status', $parameter);
        if(isset($result['is_success']) && $result['is_success'] == 'TRUE' && !empty($result['extra']))
        {
            if(isset($result['extra']['order_status']) && $result['extra']['order_status'] == 1)
            {
                return DataReturn('支付成功', 0, $this->ReturnData($result['extra']));
D
devil 已提交
230 231 232 233 234 235 236
            } else {
                $arr = [
                    0 => '未付款',
                    1 => '已付款',
                    2 => '已退款',
                    3 => '已关闭',
                ];
D
devil 已提交
237 238
                $msg = isset($arr[$result['extra']['order_status']]) ? $arr[$result['extra']['order_status']] : '支付失败';
                return DataReturn('平台订单'.$msg, -1);
D
devil 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
            }
        }
        return DataReturn(empty($result['message']) ? '支付失败' : $result['message'], -100);
    }

    /**
     * [ReturnData 返回数据统一格式]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-10-06T16:54:24+0800
     * @param    [array]                   $data [返回数据]
     */
    private function ReturnData($data)
    {
        // 返回数据固定基础参数
        $data['trade_no']       = $data['trade_no'];        // 支付平台 - 订单号
        $data['buyer_user']     = $data['pay_type'];       // 支付平台 - 用户
        $data['out_trade_no']   = $data['out_trade_no'];    // 本系统发起支付的 - 订单号
        $data['subject']        = isset($data['order_status']) ? '状态:'.$data['order_status'] : ''; // 本系统发起支付的 - 商品名称
D
devil 已提交
259
        $data['pay_price']      = $this->RespondReversePrice($data['out_trade_no'], $data['total_fee']);    // 本系统发起支付的 - 总价
D
devil 已提交
260 261 262 263

        return $data;
    }

D
devil 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    /**
     * 金额转换
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2020-09-23
     * @desc    description
     * @param   [string]          $out_trade_no [支付日志订单号]
     * @param   [float]           $total_fee    [平台返回的支付金额]
     */
    private function RespondReversePrice($out_trade_no, $total_fee)
    {
        $total_fee /= 100;

        // 金额转换
        if(isset($this->config['is_reverse_price']) && $this->config['is_reverse_price'] == 1)
        {
            // 获取订单信息
            $log_data = \app\service\PayLogService::PayLogList(['where'=>['log_no'=>$out_trade_no]]);
            if($log_data['code'] == 0 && !empty($log_data['data']) && !empty($log_data['data'][0]) && !empty($log_data['data'][0]['business_list']) && count($log_data['data'][0]['business_list']) == 1)
            {
                switch($log_data['data'][0]['business_type'])
                {
                    // 订单
                    case \app\service\OrderService::$business_type_name :
                        // 获取订单汇率
                        $currency_data = \app\service\OrderCurrencyService::OrderCurrencyGroupList($log_data['data'][0]['business_list'][0]['business_id']);
                        if(isset($currency_data['currency_rate']) && $currency_data['currency_rate'] > 0)
                        {
                            $total_fee *= $currency_data['currency_rate'];
                        }
                        break;
                }
            }
        }

        return PriceNumberFormat($total_fee);
    }

D
devil 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    /**
     * 退款处理
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-05-28
     * @desc    description
     * @param   [array]           $params [输入参数]
     */
    public function Refund($params = [])
    {
        // 参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'order_no',
                'error_msg'         => '订单号不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'trade_no',
                'error_msg'         => '交易平台订单号不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'refund_price',
                'error_msg'         => '退款金额不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'client_type',
                'error_msg'         => '订单客户端类型不能为空',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

D
devil 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
        // 退款金额
        $refund_price = $params['refund_price'];

        // 金额转换
        if(isset($this->config['is_reverse_price']) && $this->config['is_reverse_price'] == 1)
        {
            // 获取订单信息
            $log_data = \app\service\PayLogService::PayLogList(['where'=>['log_no'=>$params['order_no']]]);
            if($log_data['code'] == 0 && !empty($log_data['data']) && !empty($log_data['data'][0]) && !empty($log_data['data'][0]['business_list']) && count($log_data['data'][0]['business_list']) == 1)
            {
                switch($log_data['data'][0]['business_type'])
                {
                    // 订单
                    case \app\service\OrderService::$business_type_name :
                        // 获取订单汇率
                        $currency_data = \app\service\OrderCurrencyService::OrderCurrencyGroupList($log_data['data'][0]['business_list'][0]['business_id']);
                        if(isset($currency_data['currency_rate']) && $currency_data['currency_rate'] > 0)
                        {
                            $refund_price /= $currency_data['currency_rate'];
                        }
                        break;
                }
            }
        }

D
devil 已提交
368 369 370 371 372
        // 远程查询支付状态
        $parameter = [
            'mid'               => $this->config['mid'],
            'pay_type'          => $this->GetPayType($params['client_type']),
            'out_trade_no'      => $params['order_no'],
D
devil 已提交
373
            'refund_amount'     => (int) ((PriceNumberFormat($refund_price)*1000)/10),
D
devil 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
            'version'           => 'v1',
        ];

        // 生成签名
        $parameter['sign'] = $this->SignCreated($parameter);

        // 请求post
        $result = $this->HttpRequest('https://a.mypaynz.com/api/refund', $parameter);
        if(isset($result['is_success']) && $result['is_success'] == 'TRUE')
        {
            // 统一返回格式
            $data = [
                'out_trade_no'  => isset($result['out_trade_no']) ? $result['out_trade_no'] : '',
                'trade_no'      => isset($result['trade_no']) ? $result['trade_no'] : '',
                'buyer_user'    => isset($result['buyer_user_id']) ? $result['buyer_user_id'] : '',
                'refund_price'  => isset($result['refund_fee']) ? $result['refund_fee']/100 : $params['refund_price'],
                'return_params' => $result,
            ];
            return DataReturn('退款成功', 0, $data);
        }
        return DataReturn(empty($result['message']) ? '退款失败' : $result['message'], -100);
    }

    /**
     * [HttpRequest 网络请求]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2017-09-25T09:10:46+0800
     * @param    [string]          $url  [请求url]
     * @param    [array]           $data [发送数据]
     * @return   [mixed]                 [请求返回数据]
     */
    private function HttpRequest($url, $data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $body_string = '';
        if(is_array($data) && 0 < count($data))
        {
            foreach($data as $k => $v)
            {
                $body_string .= $k.'='.urlencode($v).'&';
            }
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $body_string);
        }
        $headers = array('content-type: application/x-www-form-urlencoded;charset=UTF-8');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $reponse = curl_exec($ch);
        if(curl_errno($ch))
        {
            return false;
        } else {
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if(200 !== $httpStatusCode)
            {
                return false;
            }
        }
        curl_close($ch);
        return json_decode($reponse, true);
    }

    /**
     * 签名生成
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-03-15
     * @desc    description
     * @param   [array]          $params [需要签名的参数]
     */
    public function SignCreated($params)
    {
        ksort($params);
        $sign  = '';
        foreach($params as $k=>$v)
        {
            if(!in_array($k, ['sign', 'sign_type']) && $v != '' && $v != null)
            {
                $sign .= "$k=$v&";
            }
        }
        return strtolower(md5(substr($sign, 0, -1).$this->config['key']));
    }
D
devil 已提交
464 465 466 467 468 469 470 471 472 473 474

    /**
     * 自定义成功返回内容
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2020-07-01
     * @desc    description
     */
    public function SuccessReturn()
    {
D
devil 已提交
475
        return 'SUCCESS';
D
devil 已提交
476
    }
D
devil 已提交
477 478
}
?>