Paypal payment with sandbox accounts, TESTING - php

I implemented a shopping cart to checkout with PayPal. For a single item, it works, but for multiple items, it does not. Pressing the paypal Pay Now! button I got an error like this => "https://www.sandbox.paypal.com/webapps/shoppingcart/error?flowlogging_id=3451c32fea2df&code=LACK_OF_BASIC_PARAMS"
"Things don't appear to be working at the moment. Please try again later."
wow, nice! missing something? showing a little code here, 'checkout.php':
<?php require_once('../resources/includes/initialize.php'); ?>
<?php
$products = Product::find_product_items();
$count = count($products);
$items = 0;
$total = 0;
?>
<?php include(TEMPLATE_FRONT.DS."header.php"); ?>
<!-- Page Content -->
<div class="container">
<!-- /.row -->
<div class="row">
<h1>Checkout</h1>
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<table id="myTable" class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th>Sub-total</th>
</tr>
</thead>
<?php
$i = 0;
foreach($products as $product){
$i = ++$i;
?>
<tbody id="<?php echo "t".$i ?>">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="agardo#business.example.com">
<input type="hidden" name="item_name_<?php echo $i; ?>" value="<?php echo $product->title; ?>">
<input type="hidden" name="item_number_<?php echo $i; ?>" value="<?php echo $product->id; ?>">
<input type="hidden" name="quantity_<?php echo $i; ?>" value="<?php echo $product->quantity; ?>">
<input type="hidden" name="amount_<?php echo $i; ?>" value="<?php echo $product->price; ?>">
<input type="hidden" name="currency_code_<?php echo $i; ?>" value="USD">
<!--<input type="hidden" name="return" value="https://localhost/public/thank_you.php">-->
<tr>
<td><?php echo $product->id; ?></td>
<td><?php echo $product->title; ?></td>
<td id="<?php echo "p".$i ?>"><?php echo "$".format_number($product->price); ?></td>
<td id="<?php echo "q".$i ?>"><?php echo $product->quantity; ?></td>
<td id="<?php echo "s_t".$i ?>"><?php echo "$".format_number($product->price*$product->quantity); ?></td>
<td>
<span class="btn btn-warning glyphicon glyphicon-minus" data-id="<?php echo $product->id; ?>"></span>
<span class="btn btn-success glyphicon glyphicon-plus" data-id="<?php echo $product->id; ?>"></span>
<span class="btn btn-danger glyphicon glyphicon-remove" data-id="<?php echo $product->id; ?>"></span>
</td>
</tr>
</tbody>
<?php $items+=$product->quantity;
$total+=$product->price*$product->quantity;
} ?>
</table>
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
<!-- ***********CART TOTALS*************-->
<div class="col-xs-4 pull-right ">
<h2>Cart Totals</h2>
<table class="table table-bordered" cellspacing="0">
<tr class="cart-subtotal">
<th>Items:</th>
<td><span id="count" class="amount"><?php echo $items; ?></span></td>
</tr>
<tr class="shipping">
<th>Shipping and Handling</th>
<td>Free Shipping</td>
</tr>
<tr class="order-total">
<th>Order Total</th>
<td><strong><span id="total" class="amount"><?php echo "$".format_number($total); ?></span></strong> </td>
</tr>
</tbody>
</table>
</div><!-- CART TOTALS-->
</div><!--Main Content-->
<?php include(TEMPLATE_FRONT.DS."footer.php"); ?>

Any particular reason you are trying to integrate a form post to /cgi-bin/webscr in 2020? Please use https://developer.paypal.com/demo/checkout/#/pattern/client , or the server version there with two server side routes if you need to record the result in a database.
( If you do need to record the result in a database, you'll need a route for 'Set Up Transaction' and one for 'Capture Transaction', documented here: https://developer.paypal.com/docs/checkout/reference/server-integration/ -- and pair these routes with the above JavaScript approval flow )

<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="agardo#business.example.com">
must be outside of the loop! END of it! :)

Related

How to send a form in a foreach loop using PHP

I want to send an Id by the form in foreach loop but when I click on each button it sends me the first form.
It means when I click on the second button it sends me the value of the first form the value of input hidden is right but the first value every time sent.
<?PHP
if ($transactions) {
foreach ($transactions as $transaction) {
?>
<td class="text-left">
<?php echo $transaction['invoice_id']; ?>
</td>
<td class="text-left">
<?php echo $transaction['invoice_type']; ?>
</td>
<td class="text-left">
<?php echo $transaction['ref_id']; ?>
</td>
<td class="text-left">
<?php echo $transaction['gateway']; ?>
</td>
<td class="text-left">
<?php echo $transaction['payment_date']; ?>
</td>
<td class="text-right">
<?php
if($transaction['payment_status'] == 'not paid') {
?>
<form
method="POST"
action="<?php echo $gate_url; ?>"
>
<input type="hidden" name="invoiceId" value="<?php echo $transaction['invoice_id']; ?>">
<button class="btn btn-md btn-primary" type="submit" >
<?php echo $pay; ?>
</button>
</form>
<?php
}
?>
</td>
<?php
}
You have an array of the values, so this:
<input type="hidden" name="invoiceId" value="<?php echo $transaction['invoice_id']; ?>">
replace with:
<input type="hidden" name="invoiceId[]" value="<?php echo $transaction['invoice_id']; ?>">

Update shopping cart quantity PHP

I'm building a test shopping site using PHP / mySQL
here is my code:
the show_product.php:
<form method ="get" action="addtocart.php">
<input type="hidden" name="id_product" value='.$row['id_product'].'>
<tr>
<th colspan="2">'.$row['name'].'</th>
</tr>
<td>
<img align="center" src='.$row['photo'].' alt="">
</td>
<td>'.mb_substr($row['description'],0,200).'....</td>
<tr></tr>
<td>
'.$row['price'].'€<input type="number" name="quantity" value="1" min="1" max="20">
</td>
<td> <button type="submit" class="btn">add to cart</button></form>
the addtocart.php
session_start()
if(isset($_GET) & !empty($_GET)){
$id_product = $_GET['id_product'];
if(isset($_GET['quantity']) & !empty($_GET['quantity'])){ $quant = $_GET['quantity']; }else{ $quant = 1;}
$_SESSION['cart'][$id_product] = array("quantity" => $quant);
header('location: cart.php');
}else{
header('location: cart.php');
}
echo "<pre>";
print_r($_SESSION['cart']);
echo "</pre>";
?>
and some code of the cart.php
foreach ($cart as $key => $value) {
$cartsql = "SELECT * FROM product WHERE id_product=$key";
$cartres = mysqli_query($con, $cartsql);
$row = mysqli_fetch_assoc($cartres);
?>
<tr><form>
<td colspan="2"><?php echo $row['name'] ?></td>
<td><?php echo $row['price'].' €'; ?></td>
**<td><input type="number" class="quantity" name="quantity" value="<?php echo $value['quantity']; ?>"></td>**
<td><?php echo ($row['price']*$value['quantity']).' €'; ?> </td>
<td><img width="30" height="30" src="icons/delete.png" alt=""></td> </tr>
If i send to addcart an item of some quantity it's working.
the problem is I cannot change the quantity inside on cart.php, I know very few about Javascript.
Please any help how to do this ??
i did it with a submit form inside the cart :)
<form class="userform" method='get' name="change" action="addtocart.php">
<input type='hidden' name='id_product' value="<?php echo $row["id_product"]; ?>" />
<td><input size="5" type="number" name="quantity" value="<?php echo $value['quantity'] ?>" min="1" max="20" onchange="this.form.submit()"></td></form>

Add and show list product in cart codeigniter

I have a problem when I add some products to cart, but then when I want to fetch contents of the cart I find it is empty.
this is my controller :
public function add_to_cart(){
$insert_data = array(
'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'price' => $this->input->post('price'),
'qty' => $this->input->post('qty') );
// This function add items into cart.
$this->cart->insert($insert_data);
}
and this is my form to add new product to cart :
<div class="button-group">
<form method="POST" action="<?php echo base_url().'add_to_cart'; ?>">
<div style="display:none">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash();?>" />
</div>
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
<input type="number" name="qty" id="<?php echo $ligneBase->id;?>">
<input type="hidden" name="name" value="<?php echo $ligneBase->name;?>">
<input type="hidden" name="price" value="<?php echo $ligneBase->price;?>">
<input type="hidden" name="id" value="<?php echo $ligneBase->id;?>">
<input class="add_cart" type="submit" value="Add to cart">
<div class="add-to-links">
</form>
</div>
and this is my cart in header.php :
<table class="table">
<tbody>
<div id="text">
<?php $cart_check = $this->cart->contents();
// If cart is empty, this will show below message.
if(empty($cart_check)) {
echo '<h4 align="center">No Product in cart, To add products to your shopping cart click on "Add to Cart" Button</h4>';
} ?>
</div>
<?php
$cart = $this->cart->contents();
foreach($cart as $indice => $ligneBase){
?>
<tr>
<td class="text-center"><img class="img-thumbnail" title="Xitefun Causal Wear Fancy Shoes" alt="Xitefun Causal Wear Fancy Shoes" src="image/product/sony_vaio_1-50x50.jpg"></td>
<td class="text-left"><?php echo $ligneBase->id;?></td>
<td class="text-right">x 1</td>
<td class="text-right"><?php echo $ligneBase->name;?> </td>
<td class="text-center"><button class="btn btn-danger btn-xs remove" title="Remove" onClick="" type="button"><i class="fa fa-times"></i></button></td>
</tr>
<?php
}
?>
</tbody>
</table>
Can I suggest a bit improvements in your code structure. You need to use a model for db interactions
Step-1. Controller Function
public function cart()
{
$data['cart_items']=$this->cart_model->getCartItems();
if($_POST)
{
// Perform Validation
if($this->form_validation->run()==false)
{
$data['errors']=validation_errors();
$this->load->view('cart_view',$data);
}
else
{
$row=$this->cart_model->insertToCart($this->security->xss_clean($_POST));
if($row)
{
$data['success']='Item Added';
$this->load->view('cart_view',$data);
}
else
{
$data['error']='Item Could not be Added';
$this->load->view('cart_view',$data);
}
}
}
else
{
$this->load->view('cart_view',$data);
}
}
Step-2. Model Functions
public function getCartItems()
{
$sql='create query';
return $this->db->query($sql)->result_array();
}
public function insertToCart($data)
{
$item=array(
'field' => $data['index']
);
$this->db->insert('cart',$item);
return $this->db->insert_id();
}
Step-3. View
<div class="button-group">
<form method="POST" action="">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash();?>" />
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
<input type="number" name="qty" id="<?php echo $ligneBase->id;?>">
<input type="hidden" name="name" value="<?php echo $ligneBase->name;?>">
<input type="hidden" name="price" value="<?php echo $ligneBase->price;?>">
<input type="hidden" name="id" value="<?php echo $ligneBase->id;?>">
<input class="add_cart" type="submit" value="Add to cart">
<div class="add-to-links">
</form>
</div>
my workout in your code
my controller
public function workout()
{
if($this->input->post())
{
//echo "<pre>"; print_r($this->input->post());
$insert_data = array(
'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'price' => $this->input->post('price'),
'qty' => $this->input->post('qty') );
$this->cart->insert($insert_data);
echo "<pre>"; print_r($this->cart->contents());
exit();
}
$this->load->view('workout');
}
my view
<form method="POST" action="<?php echo base_url().'welcome/workout'; ?>">
<input type="number" name="qty" id="1002">
<input type="hidden" name="name" value="Mobile">
<input type="hidden" name="price" value="300">
<input type="hidden" name="id" value="1002">
<input class="add_cart" type="submit" value="Add to cart">
<div class="add-to-links">
</form>
and my output is
Array
(
[fba9d88164f3e2d9109ee770223212a0] => Array
(
[id] => 1002
[name] => Mobile
[price] => 300
[qty] => 24
[rowid] => fba9d88164f3e2d9109ee770223212a0
[subtotal] => 7200
)
)
works fine
and your error is in displaying foreach
<table class="table">
<tbody>
<div id="text">
<?php $cart_check = $this->cart->contents();
// If cart is empty, this will show below message.
if(empty($cart_check)) {
echo '<h4 align="center">No Product in cart, To add products to your shopping cart click on "Add to Cart" Button</h4>';
} ?>
</div>
<?php
$cart = $this->cart->contents();
foreach($cart as $indice => $ligneBase){
?>
<tr>
<td class="text-center"><img class="img-thumbnail" title="Xitefun Causal Wear Fancy Shoes" alt="Xitefun Causal Wear Fancy Shoes" src="image/product/sony_vaio_1-50x50.jpg"></td>
<td class="text-left"><?php echo $ligneBase->id;?></td>
<td class="text-right">x 1</td>
<td class="text-right"><?php echo $ligneBase->name;?> </td>
<td class="text-center"><button class="btn btn-danger btn-xs remove" title="Remove" onClick="" type="button"><i class="fa fa-times"></i></button></td>
</tr>
<?php
}
?>
</tbody>
</table>
change it to
<table class="table">
<tbody>
<div id="text">
<?php $cart_check = $this->cart->contents();
// If cart is empty, this will show below message.
if(empty($cart_check)) {
echo '<h4 align="center">No Product in cart, To add products to your shopping cart click on "Add to Cart" Button</h4>';
} ?>
</div>
<?php
$cart = $this->cart->contents();
foreach($cart as $indice => $ligneBase){
?>
<tr>
<td class="text-center"><img class="img-thumbnail" title="Xitefun Causal Wear Fancy Shoes" alt="Xitefun Causal Wear Fancy Shoes" src="image/product/sony_vaio_1-50x50.jpg"></td>
<td class="text-left"><?php echo $ligneBase['id'];?></td>
<td class="text-right">x 1</td>
<td class="text-right"><?php echo $ligneBase['name'];?> </td>
<td class="text-center"><button class="btn btn-danger btn-xs remove" title="Remove" onClick="" type="button"><i class="fa fa-times"></i></button></td>
</tr>
<?php
}
?>
</tbody>
</table>

Validate radio button selected in each group

Here is the code. The "surveys" are generated elsewhere. I need validation that a radio button is clicked in each group of answers. The number of answers and answer groups will be variable.
<div id="main">
<div class="container">
<!-- display a table of surveys -->
<h2><?php echo $survey['title']; ?></h2>
<form class="survey" name="take_survey" action="./take_survey" method="post">
<table class="table table-hover">
<input type="hidden" name="survey" value="<?php echo $survey['id'] ?>" />
<tr>
<th>Question</th>
<th>Response</th>
</tr>
<?php foreach ($questions as $question) : ?>
<tr>
<td><?php echo $question['question']; ?></td>
<td></td>
</tr>
<?php foreach ($answers as $answer) : ?>
<?php if ($answer['question_id'] == $question['id']) : ?>
<tr>
<td> <?php echo $answer['answer']; ?></td>
<td><input type="radio" name="question_<?php echo $question['id']?>" value="<?php echo $answer['id'] ?>"></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
</table>
<div class="eventButtons">
<input class="btn btn-default" type="submit" name="submit" id="submit" value="Save">
<input class="btn btn-default" type="reset" name="reset" id="reset" value="Clear" class="btn">
</div>
</form>
</div>
</div>

pass relevant hidden field values from the form to the controller

Im having a page that shows monthly subscriptions of a user which is created using codeigniter. what i want to do is when a the user clicks on make payment pass the values in the hidden files to the controller.
<?php echo form_open('options/done');?>
<table class="tables">
<thead>
<tr>
<th>Ref Code</th>
<th>Month</th>
<th>Year</th>
<th>action/th>
</tr>
</thead>
<tbody>
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];?>
<input type="hidden" value="<?php echo $month;?>" name="month_<?php echo $s;?>" />
<input type="hidden" value="<?php echo $payment['ref_code'];?>" name="ref_<?php echo $s;?>" />
<tr>
<td><?php echo $payment['ref_code'];?></td>
<td><?php echo $month;?></td>
<td><?php echo $payment['year'];?></td>
<td><input type="submit" value="MAKE PAYMENT" class="red" /></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php echo form_close();?>
so when someone hits the submit button how can i pass only the hidden values which are relevant to that table row?
add form just inside the <tr> elements inside your loop (see below with your code)
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];
?>
<tr>
<form action="target.php" method="post" name="formName_<?php echo $s;?>" >
<input type="hidden" value="<?php echo $month;?>" name="month_<?php echo $s;?>" />
<input type="hidden" value="<?php echo $payment['ref_code'];?>" name="ref_<?php echo $s;?>" />
<td><?php echo $payment['ref_code'];?></td>
<td><?php echo $month;?></td>
<td><?php echo $payment['year'];?></td>
<td><input type="submit" value="MAKE PAYMENT" class="red" /></td>
</form>
</tr>
<?php endforeach; ?>

Categories