美文网首页
支付组件源码分析

支付组件源码分析

作者: 勤学会 | 来源:发表于2019-07-13 21:54 被阅读0次

    支付

    不同的支付渠道对于的逻辑也不一样.那么第一步先获取对应渠道的对象
    static::instance->initCharge(channel, $config);
    对象简单的理解就是属性和方法,属性一般保存的就是:
    1.配置信息
    2.数据信息
    3.子对象
    函数方法就是逻辑的封装

        public function initCharge($channel, array $config)
        {
            // 初始化时,可能抛出异常,再次统一再抛出给客户端进行处理
            try {
                switch ($channel) {
                    case Config::ALI_CHANNEL_WAP:
                        $this->channel = new AliWapCharge($config);
                        break;
                    case Config::ALI_CHANNEL_APP:
                        $this->channel = new AliAppCharge($config);
                        break;
                    case Config::ALI_CHANNEL_WEB:
                        $this->channel = new AliWebCharge($config);
                        break;
                    default:
                        throw new PayException('当前仅支持:支付宝  微信 招商一网通');
                }
            } catch (PayException $e) {
                throw $e;
            }
        }
    

    第二步执行.

      $ret = $instance->charge($metadata);
    

    主要是面向接口的开发思维

    返回值的处理

    不同渠道返回的信息,各不一样,逻辑和上面的相似,使用不同的策略把结果处理成一样的数组结构

        /**
         * 获取向客户端返回的数据
         * @param array $data
         *
         * @return array
         * @author helei
         */
        protected function getRetData(array $data)
        {
            // 将金额处理为元
            $totalFee = bcdiv($data['total_fee'], 100, 2);
            $cashFee = bcdiv($data['cash_fee'], 100, 2);
    
            $retData = [
                'bank_type' => $data['bank_type'],
                'cash_fee' => $cashFee,
                'device_info' => $data['device_info'],
                'fee_type' => $data['fee_type'],
                'is_subscribe' => $data['is_subscribe'],
                'buyer_id'   => $data['openid'],
                'order_no'   => $data['out_trade_no'],
                'pay_time'   => date('Y-m-d H:i:s', strtotime($data['time_end'])),// 支付完成时间
                'amount'   => $totalFee,
                'trade_type' => $data['trade_type'],
                'transaction_id'   => $data['transaction_id'],
                'trade_state'   => strtolower($data['return_code']),
                'channel'   => Config::WX_CHARGE,
            ];
    
            // 检查是否存在用户自定义参数
            if (isset($data['attach']) && ! empty($data['attach'])) {
                $retData['return_param'] = $data['attach'];
            }
            return $retData;
        }
    

    最近在做支付功能,果然实践中才有更深体会.事物各有逻辑,用基础点,串成线,这样就清楚很多了,
    感谢开源作者 大愚.这是他的开源代码.集成了支付和微信所有支付方式.
    https://github.com/helei112g/payment

    相关文章

      网友评论

          本文标题:支付组件源码分析

          本文链接:https://www.haomeiwen.com/subject/cefvkctx.html