【问题标题】:Implementing Payment Gataway in Laravel based shop在基于 Laravel 的商店中实现支付网关
【发布时间】:2016-10-11 10:44:41
【问题描述】:

我需要一些帮助来在 Laravel 商店中实施付款度假。 我使用的付款是https://gourl.io/,我不明白如何获取所需信息。所以我已经设置了文件数据库表,数据库连接等等。现在我正在尝试在提交订单后将用户重定向到payment.php页面。这是我的CartController.php orderSubmit 函数

public function orderSubmit() {
    $cart = Session::get(self::CART_SESSION_KEY, array());
    if (count($cart) < 1) {
        return Redirect::to('/');
    }

    $validatorRules = array(
        'captcha' => 'required|captcha',
        'shipping_address' => 'required|min:10',
        'shipping_method' => 'required|in:' . implode(',', [Settings::SETTINGS_SHIPPING_NORMAL, Settings::SETTINGS_SHIPPING_EXPRESS])
    );

    Input::merge(array_map('trim', Input::all()));
    $validator = Validator::make(Input::all(), $validatorRules);

    if ($validator->fails()) {
        return Redirect::to('/cart/order?_token=' . csrf_token())->withErrors($validator->errors())->withInput(Input::except(['captcha']));
    }

    $shipping = array(
        'quantity' => 1,
        'image' => '/img/noimage.png',
        'description' => '',
        'title' => 'FIX ME', // this should never occur,
        'price' => 100000 // this should never occur
    );
    switch (Input::get('shipping_method')) {
        case Settings::SETTINGS_SHIPPING_NORMAL:
            $shipping['title'] = 'Normal Delivery';
            $shipping['price'] = 0;
            break;

        case Settings::SETTINGS_SHIPPING_EXPRESS:
            $shipping['title'] = sprintf('Express Delivery - $%.2f', Settings::getOption('express_shipping_cost'));
            $shipping['price'] = doubleval(Settings::getOption('express_shipping_cost'));
            break;
    }

    $cart['shipping'] = $shipping;
    $order = new Order();
    $order->user_id = self::$user->user_id;
    $order->data = json_encode($cart);
    $order->address = Input::get('shipping_address');
    $order->pgp_key = Input::get('gpgkey');
    $order->info = Input::get('additional_info');
    $order->save();

    Session::put(self::CART_SESSION_KEY, array());
    return Redirect::to('payment.php')->with('message_success', 'Order created! We will contact you shortly to confirm your order and payment details.');
}

这是 payment.php

    require_once( "../cryptobox.class.php" );

/**** CONFIGURATION VARIABLES ****/ 

$userID         = "";               // place your registered userID or md5(userID) here (user1, user7, uo43DC, etc).
                                    // you don't need to use userID for unregistered website visitors
                                    // if userID is empty, system will autogenerate userID and save in cookies
$userFormat     = "";           // save userID in cookies (or you can use IPADDRESS, SESSION)
$orderID        = "";
$amountUSD      = 20;           
$period         = "NOEXPIRY";       
$def_language   = "en";             
$public_key     = "mypublickey"; 
$private_key    = "myprivatekey";



/** PAYMENT BOX **/
$options = array(
        "public_key"  => $public_key,   // your public key from gourl.io
        "private_key" => $private_key,  // your private key from gourl.io
        "webdev_key"  => "",        // optional, gourl affiliate key
        "orderID"     => $orderID,      // order id or product name
        "userID"      => $userID,       // unique identifier for every user
        "userFormat"  => $userFormat,   // save userID in COOKIE, IPADDRESS or SESSION
        "amount"      => 0,             // product price in coins OR in USD below
        "amountUSD"   => $amountUSD,    // we use product price in USD
        "period"      => $period,       // payment valid period
        "language"    => $def_language  // text on EN - english, FR - french, etc
);

// Initialise Payment Class
$box = new Cryptobox ($options);

// coin name
$coinName = $box->coin_name(); 

// Successful Cryptocoin Payment received
if ($box->is_paid()) 
{
    if (!$box->is_confirmed()) {
        $message =  "Thank you for payment (payment #".$box->payment_id()."). Awaiting transaction/payment confirmation";
    }                                           
    else 
    { // payment confirmed (6+ confirmations)

        // one time action
        if (!$box->is_processed())
        {
            // One time action after payment has been made/confirmed

            $message = "Thank you for order (order #".$orderID.", payment #".$box->payment_id()."). We will send soon";

            // Set Payment Status to Processed
            $box->set_status_processed();  
        }
        else $message = "Thank you. Your order is in process"; // General message
    }
}
else $message = "This invoice has not been paid yet";

$languages_list = display_language_box($def_language);

我的问题是如何在 payment.php 中获取正确的信息?如何取userID、userFormat、orderID等?

【问题讨论】:

    标签: php laravel-4 payment


    【解决方案1】:

    首先,我建议你使用 Laravel 作为它的目标框架。在 Laravel 中,您可以定义控制器来处理您的 http 请求。创建一个新的 PaymentController 并将 payment.php 中的代码放入这个控制器中。然后路由到那个控制器方法。

    还将你的配置设置放在 Laravel 配置文件夹中。

    并且require_once( "../cryptobox.class.php" ); 可以被控制器构造函数中的依赖注入替换。

    现在回到你的问题。

    $userID 是你注册 Laravel 用户 ID 的地方。 (如果您没有任何注册用户,请留空)。为什么你应该把你的用户 id 放在这个变量中? - 它有助于跟踪哪些用户完成了哪些付款。如果您想跟踪付款历史记录,可以稍后将此信息保存在数据库中。

    $orderID 这是您放置 内部 订单 ID 的位置。为什么要使用内部订单 ID? - 再次跟踪哪些用户购买了哪些产品。您可以将您的 order-id 与 user-id 和 product-id 一起存储在数据库中,以获取购买历史日志。

    $userFormat这是您希望存储用户信息、会话、cookie 等的方式。因为执行购买时,支付网关需要访问这些信息的方式,因此必须将其存储在会话中或在 cookie 中。

    【讨论】:

    • 好的,我已经制作了新控制器,下订单后我将用户重定向到此页面,但出现错误MethodNotAllowedHttpException... 我想我的控制器有错误
    • MethodNotAllowedHttpException 通常在您尝试使用错误的 http-verb 访问方法时出现。例如,如果您尝试使用 http-POST 调用定义为 GET 的路由
    • 现在我得到exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in /var/www/site/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:148。我的路线是Route::post('/cart/payment', ['uses' =&gt; 'PaymentController@paymentView', 'before' =&gt; 'auth|csrf']); 并正确重定向到/cart/payment
    • 这意味着您正在尝试访问未在您的 routes.php 文件中指定的 URL
    【解决方案2】:

    如果您为您的用户使用会话,我会使用$_SESSION['$value']

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 2023-04-01
      • 2012-08-27
      • 2015-10-26
      • 2017-07-13
      • 1970-01-01
      • 2013-12-09
      相关资源
      最近更新 更多