【问题标题】:Stripe payment gateway integration in codeigniterCodeigniter 中的 Stripe 支付网关集成
【发布时间】:2015-03-07 06:01:56
【问题描述】:

我在 codeigniter 中使用 Stripe.js 使用 Stripe 支付网关。在条带支付过程中,首先进行客户端验证,然后通过条带 php 2.1.1 库进行服务器端验证。客户端验证工作就绪,但服务器端验证出错。我将stripe php 2.1.1 文件夹放在codeigniter 库文件夹中。

我参考这个链接http://code.tutsplus.com/tutorials/how-to-accept-payments-with-stripe--pre-80957

代码:

    <?php
$success = "";
$error = "";

require_once(APPPATH.'libraries/stripe/lib/Stripe.php');

if ($_POST) {
  Stripe::setApiKey("dm_wsdst_yJQB5mrfjpfQX2uMQHf3CbD");
  $error = '';
  $success = '';
  try {
    if (!isset($_POST['stripeToken']))
      throw new Exception("The Stripe Token was not generated correctly");
    Stripe_Charge::create(array("amount" => 11,
                                "currency" => "usd",
                                "card" => $_POST['stripeToken']));
    $success = 'Your payment was successful.';
  }
  catch (Exception $e) {
    $error = $e->getMessage();
  }
}

echo "sucess". isset($success) ? $success : "";
echo "error".isset($error) && $success ? $error : "";

?>



<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script>
    Stripe.setPublishableKey('es_iksl_l5li2xygPS9cJE5MMWE8GSr');
</script> 

<div class="content_white register_wrapper">
    <div class="content_40" style="margin-left:350px;">
            <div class="content_gray" style="margin-top: 50px;">
                    <form method="post" name="payment_form" id="payment_form">
                            <div class="error" id="payment_error"></div>
                            <div class="box" style="padding: 15px 0;">    
                                    <label>Card Number</label>
                                    <input type="text" class="text_box" placeholder="Card Number" data-stripe="number" id="card_number" name="card_number" style="height: 40px;" />
                                    <div class="error" id="form_fname_error"></div>
                            </div>

                            <div class="box" style="padding: 15px 0;">    
                                    <label>Card CVC No</label>
                                    <input type="text" class="text_box" placeholder="CVC Number" data-stripe="cvc" id="cvc_number" name="cvc_number" style="height: 40px;" />
                                    <div class="error" id="form_lname_error"></div>
                            </div>
                            <br>
                            <div class="box" style="padding: 0px">
                                <label>Card Expiration Month/Year (MM/YYYY)</label>
                            </div>
                            <div class="box" style="padding: 15px 0; width: 30%;float: left;">
                                <input type="text" class="text_box" placeholder="Month-(MM)" data-stripe="exp-month" id="exp_month" name="exp_month" style="height: 40px;" />
                                    <div class="error" id="email_error1"></div>
                            </div>

                            <div class="box" style="padding: 15px 0; width: 50%;float: left;" >
                                <input type="text" class="text_box" placeholder="Year-(YYYY)" data-stripe="exp-year" id="exp_year" name="exp_year" style="height: 40px;" />
                                    <div class="error" id="form_password_error"></div>
                            </div>
                            <div class="clear"></div>
                            <div class="box" style="text-align:center;">
                                <div class="submit_btn">
                                    <input type="submit" class="btn_blue" id="submit_btn" value="Pay $20"/>
                                </div>
                            </div>
                            <div class="clear"></div>
                    </form>
            </div>
    </div> 
    <div class="clear"></div>
</div>
<script>
    $(document).ready(function(){


        $('#payment_form').submit(function(event) {
        console.log("start");
        var $form = $(this);
        // Disable the submit button to prevent repeated clicks
        $form.find('#submit_btn').prop('disabled', true);
         Stripe.createToken({
                        number: $('#card_number').val(),
                        cvc: $('#cvc_number').val(),
                        exp_month: $('#exp_month').val(),
                        exp_year: $('#exp_year').val()
                    }, stripeResponseHandler);

        // Prevent the form from submitting with the default action
        return false;
      });

        });
        // Call back function for stripe response.
            function stripeResponseHandler(status, response) {

                console.log("ststus"+status);
                if (response.error) {
                    // Re-enable the submit button
                    $('#submit_btn').removeAttr("disabled");
                    // Show the errors on the form
//                    stripeErrorDisplayHandler(response);
                    $('#payment_error').text(response.error.message);
//                    $('.subscribe_process').hide();
                } else {
                    var form = $("#payment_form");
                    // Getting token from the response json.

                    $('<input>', {
                            'type': 'hidden',
                            'name': 'stripeToken',
                            'value': response.id
                        }).appendTo(form);

                    // Doing AJAX form submit to your server.
                    form.get(0).submit();
                    return false;
                }
            }
</script>

当我运行这个文件然后运行准备好,当我点击支付按钮然后检查卡数据和到期月份和年份如果没问题然后返回 stripeToken 然后再次重新提交表单然后它会返回错误是

致命错误:未找到“Stripe”类 D:\xampp\htdocs\moviesaints\application\views\user\payment_precess.php 在第 12 行

任何想法给我解决方案。

【问题讨论】:

    标签: php codeigniter payment-gateway stripe-payments


    【解决方案1】:

    @rubai 是绝对正确的。这是发布表单时的操作方式,即您收集卡片信息,然后在发布操作中您在控制器中像这样工作:

    //代码

    公共函数 capture_payment(){

    require_once APPPATH."third_party/stripe/init.php";
    \Stripe\Stripe::setApiKey("sk_test_bB6syzsfoOqdQ4R57m7zE5Sp");      
    $token = Stripe\Token::create(array(
      "card" => array(
      "number" => $this->input->post("credit_card_no"),
      "exp_month" => $this->input->post("expiry_month"),
      "exp_year" => $this->input->post("expiry_year"),
      "cvc" => $this->input->post("cvv")
        )
    ));
    $customer = \Stripe\Customer::create(array(
          "source" => $token,
          "description" =>$this->session->userdata('doctor_name'))
    );
    
    // Charge the Customer instead of the card
    $charge = \Stripe\Charge::create(array(
       "amount" => 2000, // amount in cents, again
       "currency" => "usd",
       "customer" => $customer->id)
    );
    

    }

    【讨论】:

      【解决方案2】:

      请用这个 FCPATH 代替 APPPATH 之类的

      require_once(FCPATH.'libraries/stripe/lib/Stripe.php');
      

      【讨论】:

        【解决方案3】:

        您可能需要包含 init.php 而不是 stripe.php

        require_once(APPPATH.'libraries/stripe/lib/Stripe.php');
        

        然后像这样调用条带库

        \Stripe\Stripe::setApiKey('dm_wsdst_yJQB5mrfjpfQX2uMQHf3CbD');
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-08-07
          • 2016-01-04
          • 2020-09-24
          • 2019-05-04
          • 2017-04-26
          • 2021-03-25
          • 2017-02-28
          • 1970-01-01
          相关资源
          最近更新 更多