首页 文章

在codeigniter中条带支付网关集成

提问于
浏览
2

我使用stripe.js在codeigniter中使用Stripe支付网关 . 在条带支付过程中首先进行客户端验证,然后通过条带php 2.1.1库进行服务器端验证 . 客户端验证工作准备就绪,但服务器端验证会出错 . 我把条带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然后再次重新提交表单然后它将返回错误,即

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

任何想法给我解决方案 .

3 回答

  • 0

    @rubai是绝对正确的 . 这是你在表格发布时的去法,即你收集卡片信息然后在你在控制器中这样工作的后期行动:

    //码

    public function 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

    请在APPPATH的地方使用这个FCPATH

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

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

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

    然后像这样调用条带库

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

相关问题