欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

php paypal支付/微信支付/支付宝支付封装及订单查询封装接口

程序员文章站 2022-06-12 19:38:50
...
class OrderController extends Controller
{
    protected $alipay, $wxpay, $rsa, $Paypal;

    public function __construct()
    {
        parent::__construct();
        vendor('Rsa.Rsa');
        vendor('Alipay.Alipay');
        vendor('Wxpay.Wxpay');
        vendor('Paypal.Paypal');
        $this->wxpay  = new \Wxpay();
        $this->Paypal = new \Paypal();

        $this->users_model          = D("Common/Member");
        $this->member_behavior      = D("Common/MemberBehavior");
        $this->member_order_model   = D("Common/MemberOrder");
        $this->member_product_price = D("Common/ProductPrice");
    }

    /**
     * 下单并获取支付二维码
     */
    public function pay()
    {
        try {
            $request = I('request.'); // 获取主体参数
            $pay_type    = intval($request['pay_type']);
            $result = ''; //添加订单等业务逻辑
            if ($result !== false) {
                if ($pay_type == 1) { //支付宝扫码支付
                    // 组装支付宝下单参数
                    $order_data = [
                        "out_trade_no" => $trade_order_no,
                        "total_amount" => $money,
                        "subject"      => $product_info['name'],
                        "body"         => $data['body'],
                    ];
                    // 调用支付宝预下单接口
                    $result = \Alipay::f2fPay(C('ALIPAY_CONFIG'), $order_data);

                    if ($result['status'] == 'SUCCESS' && $result['response']->code == '10000') {
                        $return['code']    = 100100;
                        $return['message'] = lang('SUCCESS');
                        $rsaData           = [
                            'qrcode'         => U('Papi/Order/qrcode', ['param' => base64_encode($result['response']->qr_code)], false, true),
                            'trade_order_no' => $trade_order_no,
                        ];
                        $return['data']    = $this->rsa->publicEncrypt(json_encode($rsaData));
                    } else {
                        throw new \Exception($result['response']->msg, $result['response']->code);
                    }

                } elseif ($pay_type == 2) { //微信支付
                    // 组装微信下单参数
                    $order_data = [
                        "body"         => $product_info['name'],
                        "attach"       => $result,
                        "out_trade_no" => $trade_order_no,
                        "money"        => $money * 100, // 正式金额单位:分
                        "notify_url"   => C("WXPAY_NOTIFY_URl"),
                        "product_id"   => $product_id,
                    ];
                    // 调用微信下单接口
                    $param             = $this->wxpay->native($order_data);
                    $return['code']    = 100100;
                    $return['message'] = lang('SUCCESS');
                    $rsaData           = [
                        'qrcode'         => U('Papi/Order/qrcode', ['param' => base64_encode($param)], false, true),
                        'trade_order_no' => $trade_order_no,
                    ];
                    $return['data']    = $this->rsa->publicEncrypt(json_encode($rsaData));
                } elseif ($pay_type == 3) {
                    //todo paypal 支付
                    $this->Paypal->pay_amount     = round($money / $exchange_rate, 2);
                    $this->Paypal->trade_order_no = $trade_order_no;
                    $this->Paypal->product_name   = $data['body'];
                    $url                          = $this->Paypal->pay();
                    $return['code']               = 100100;
                    $return['message']            = lang('SUCCESS');
                    $rsaData                      = [
                        'qrcode'         => U('Papi/Order/qrcode', ['param' => base64_encode($url)], false, true),
                        'trade_order_no' => $trade_order_no,
                    ];
                    $return['data']               = $this->rsa->publicEncrypt(json_encode($rsaData));
                } else {
                    throw new \Exception(lang('NO_SUPPORT_PAY'), 100125);
                }
            } else {
                throw new \Exception(lang('SYSTEM_BUSY'), 100126);
            }
        } catch (\Exception $e) {
            $return['code']    = $e->getCode();
            $return['message'] = lang('SYSTEM_BUSY');
            $return['data']    = "";
        }
        custom_returnJson($return);
    }

    /**
     * 根据url转化二维码图片
     */
    public function qrcode()
    {
        $return = [];
        try {
            $param = base64_decode(I('request.param')); // 获取主体参数
            return $this->wxpay->qrcode($param);
        } catch (\Exception $e) {
            $return['code']    = $e->getCode();
            $return['message'] = $e->getMessage();
            $return['data']    = "";
        }
        custom_returnJson($return);
    }

    /**
     * 支付订单查询接口
     */
    public function query()
    {
        $return = [];
        // 必填参数
        $required       = [
            'trade_order_no', 'sign', 'ts',
        ];
        $request        = I('request.');
        $trade_order_no = $request['trade_order_no'];
        try {
            $check = custom_check_api($request, $required);
            if ($check['code'] == 100100) {
                $order = $this->member_order_model->where(['trade_order_no' => $trade_order_no])->find();
                if ($order) {
                    $now = date('Y-m-d H:i:s');
                    $url = ($now > C("DRAGON_START")) && ($now < C("DRAGON_END")) ? C("DRAGON_URL") : '';
                    if ($order['pay_type'] == 1) {
                        $result = \Alipay::f2fQueryOrder(C('ALIPAY_CONFIG'), $trade_order_no);
                        if ($result['status'] == 'Success' || $result['response']->code == '10000') {
                            $return['code']    = 100100;
                            $return['message'] = lang('QUERY_SUCCESS');
                            $return['data']    = json_encode([
                                'trade_state' => transferTradeState($result['response']->trade_status),
                                'return_msg'  => $result['response']->msg,
                                'url'         => $url,
                            ]);
                        } else {
                            throw new Exception($result['response']->msg, $result['response']->code);
                        }
                    } elseif ($order['pay_type'] == 2) {
                        $response = $this->wxpay->orderquery(['out_trade_no' => $trade_order_no]);
                        if ($response !== false) {
                            $return['code']    = 100100;
                            $return['message'] = lang('QUERY_SUCCESS');
                            $return['data']    = (json_encode([
                                'trade_state' => $response['trade_state'],
                                'return_msg'  => $response['return_msg'],
                                'url'         => $url,
                            ]));

                        } else {
                            throw new Exception($response['return_msg'], $response['trade_state']);
                        }
                    } elseif ($order['pay_type'] == 3 && $order['payment_id']) {
                        $response = $this->Paypal->query($order['payment_id']);
                        if (isset($response['state']) && (strtolower($response['state']) == 'approved' || strtolower($response['state']) == 'COMPLETED')) {
                            $return['code']    = 100100;
                            $return['message'] = lang('QUERY_SUCCESS');
                            $return['data']    = json_encode([
                                'trade_state' => 'SUCCESS',
                                'return_msg'  => lang('PAY_SUCCESS'),
                                'url'         => $url,
                            ]);
                        } else {
                            throw new Exception(lang('PAY_ERROR'), 100125);
                        }
                    } else {
                        throw new Exception(lang('NO_SUPPORT_PAY'), 100125);
                    }
                } else {
                    throw new Exception(lang('ORDER_NOT_EXIST') . '->' . $trade_order_no, 100127);
                }
            } else {
                throw new Exception($check['message'], $check['code']);
            }

        } catch (\Exception $e) {
            $return['code']    = $e->getCode();
            $return['message'] = lang('SYSTEM_BUSY');
            $return['data']    = "";
        }
        custom_returnJson($return);
    }
    
}
相关标签: 支付 php