首页 文章

Codeigniter:如何获得paypal付款的购物车 Value

提问于
浏览
0
<?php echo form_open('member/update_cart'); ?>

<script>
function clear_cart() {
    var result = confirm('Are you sure want to clear all bookings?');

    if(result) {
        window.location = "<?php echo base_url(); ?>member/cart_remove/all";
    }else{
        return false; // cancel button
    }
}
</script>
<table cellpadding="6" cellspacing="1" style="width:100%" border="1">

<div style="margin:0px auto; width:600px;" >
    <div style="padding-bottom:10px">
        <h1 align="center">Your Shopping Cart</h1>
        <input type="button" value="Continue Shopping" onclick="window.location='<?php echo base_url(); ?>member/book'" />
    </div>
    <table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%">
        <?php if ($cart = $this->cart->contents()): ?>
        <tr bgcolor="#FFFFFF" style="font-weight:bold">
            <th>Item No</th>
            <th>Book Name</th>
            <th>Price</th>
            <th>Quantity</th>
            <th>Sub-Total</th>
            <th>Remove</th>
        </tr>
        <?php
        echo form_open('cart/update_cart');
        $grand_total = 0; $i = 1;

        foreach ($cart as $item):
            echo form_hidden('cart['. $item['id'] .'][id]', $item['id']);
            echo form_hidden('cart['. $item['id'] .'][rowid]', $item['rowid']);
            echo form_hidden('cart['. $item['id'] .'][name]', $item['name']);
            echo form_hidden('cart['. $item['id'] .'][price]', $item['price']);
            echo form_hidden('cart['. $item['id'] .'][qty]', $item['qty']);
        ?>
        <tr bgcolor="#FFFFFF">
            <td>
                <?php echo $i++; ?>
            </td>
            <td>
                <?php echo $item['name']; ?>
            </td>
            <td>
                $ <?php echo number_format($item['price'],2); ?>
            </td>
            <td>
                <?php echo form_input('cart['. $item['id'] .'][qty]', $item['qty'], 'maxlength="3" size="1" style="text-align: right"'); ?>
            </td>
            <?php $grand_total = $grand_total + $item['subtotal']; ?>
            <td>
                $ <?php echo number_format($item['subtotal'],2) ?>
            </td>
            <td>
                <a href="<?php echo base_url(); ?>cart/remove/<?php echo $item['rowid'] ?>" style="color:#000000;">Remove</a>
            </td>
            <?php endforeach; ?>
        </tr>
        <tr>
            <td><b>Grand Total: $<?php echo number_format($grand_total,2); ?></b></td>
            <td colspan="5" align="right"><input type="button" value="Clear Cart" onclick="clear_cart()">
                    <input type="submit" value="Update Cart">
                    <?php echo form_close(); ?>

                    <input type="button" value="Checkout" onclick="window.location='<?php echo base_url(); ?>payment/checking'"></td>
        </tr>
        <?php endif; ?>
    </table>
</div>

这是paypal控制器代码:

public function __construct(){
    parent::__construct();
    $this->load->library('session');
    $this->load->library('cart');
    $this->load->helper('url');
    $this->load->helper('form');
    if (! $this->session->userdata('is_logged_in')){
        redirect('main/restricted');
    } else {
    $privilege = $this->session->userdata('privilege');
    if ($privilege == 'admin'){
        redirect('main/restricted');
    }
    }
    $this->load->helper("string");
}

public function do_purchase(){
    $config['business']             = 'phptesting2015-facilitator@gmail.com';
    $config['cpp_header_image']     = ''; //Image header url [750 pixels wide by 90 pixels high]
    $config['return']               = 'https://snt.website/ignite_project/payment/notify_purchase';
    $config['cancel_return']        = 'https://snt.website/ignite_project/payment/cancel_purchase';
    $config['notify_url']           = 'process_payment.php'; //IPN Post
    $config['production']           = FALSE; //Its false by default and will use sandbox
    //$config['discount_rate_cart']     = 20; //This means 20% discount
    $config["invoice"]              = random_string('numeric', 8); //The invoice id

    $this->load->library('paypal',$config);

    #$this->paypal->add(<name>,<price>,<quantity>[Default 1],<code>[Optional]);

    //$this->paypal->add('T-shirt',2.99,6); //First item
    //$this->paypal->add('Pants',40);     //Second item
    //$this->paypal->add('Blowse',10,10,'B-199-26'); //Third item with code

    $this->paypal->pay(); //Proccess the payment
}

public function notify_purchase(){
    $received_data = print_r($this->input->post(), TRUE);

    echo "<pre>".$received_data."</pre>";
}

public function cancel_purchase(){
    echo "Cancel Purchase";

}

}

我想知道如何从购物车中获取数据,以便我能够在paypal中添加值,以便用户可以继续使用paypal?请帮忙!

2 回答

  • 0

    根据控制器中的代码判断,您可以在 do_purchase() 函数中使用paypal(我假设的库)来循环浏览购物车内容来添加产品:

    foreach ($this->cart->contents() as $item) {
        $this->paypal->add($item['name'], $item['price'], $item['qty']);
    }
    
    $total = $this->cart->total();
    

    您可以找到有关CodeIgniter的购物车类in the docs的更多信息 .

    在完全不相关的注释中,您可以使用 base_url() 作为函数而不是向其追加字符串 . 例如 <?=base_url('cart/remove/' . $item['rowid']);?><?php echo base_url(); ?>cart/remove/<?php echo $item['rowid'] ?> 相比更具可读性

  • 0

    使用CI Cart Library方法

    $this->cart->total()
    

    您可以获得总金额(价格),如果您想对此进行折扣,则需要为此设置一个独有的方法 .

    [编辑]

    当您获得总金额解析Paypal库和Curl(根据Paypal文档),您可以使用卷曲方法解析值

    curl -v -X 'POST' 'https://api.sandbox.paypal.com/v1/invoicing/invoices' \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer <Access-Token>' \
    -d '{
      "merchant_info": {
        "email": "dennis@sample.com",
        "first_name": "Dennis",
        "last_name": "Doctor",
        "business_name": "Medical Professionals, LLC",
        "phone": {
          "country_code": "001",
          "national_number": "5032141716"
        },
        "address": {
          "line1": "1234 Main St.",
          "city": "Portland",
          "state": "OR",
          "postal_code": "97217",
          "country_code": "US"
        }
      },
      "billing_info": [
      {
        "email": "example@example.com"
      }
      ],
      "items": [
      {
        "name": "Sutures",
        "quantity": 100,
        "unit_price": {
        "currency": "USD",
        "value": 5
        }
      }
      ],
      "note": "Medical Invoice 16 Jul, 2013 PST",
      "payment_term" :{
          "term_type": "NET_45"
      },
      "shipping_info": {
        "first_name": "Sally",
        "last_name": "Patient",
        "business_name": "Not applicable",
        "address": {
        "line1": "1234 Broad St.",
        "city": "Portland",
        "state": "OR",
        "postal_code": "97216",
        "country_code": "US"
        }
      }
    }'
    

    (来源:CodeIgniter文档和PayPal API文档)

相关问题