Hi
I have a project to build a payment system by check and paypal. I’m using Cartkit and Uniform plugin (both amasing btw). With Cartkit, the user can choose product, refresh the cart, and pay by PAYPAL. With Uniform, it could be possible to pay by CHECK, sending by mail the products and customer information, delivery adress, etc.
Separately, both plugin are working very well. But now, I’m trying to include uniform to the cart.php from Cartkit. And here is the issue, I get an error saying The CSRF token was invalid
.
I send here a reduced code of the cart.php, after copying the uniform into the cart.
<?php $cart = cart_logic(get_cart()) ?>
<?php $products = $pages->find('bookstore')->children()->visible() ?>
<?php snippet('header') ?>
<?php if(count($cart) == 0): ?>
<main id="cart" class="main black" role="main">
<?php if($page->sandbox() != 'true'): ?>
<form method="post" action="https://www.sandbox.paypal.com/cgi-bin/webscr">
<?php else: ?>
<form method="post" action="https://www.paypal.com/cgi-bin/webscr">
<?php endif ?>
// MY CODE WHEN THE CART IS EMPTY
</form>
</main>
<?php else: ?>
// MY CODE WHEN THE CART IS FULL
<main id="cart" class="main" role="main">
<?php if($page->sandbox() != 'true'): ?>
<form method="post" action="https://www.sandbox.paypal.com/cgi-bin/webscr">
<?php else: ?>
<form method="post" action="https://www.paypal.com/cgi-bin/webscr">
<?php endif ?>
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="<?php echo $site->email() ?>">
<input type="hidden" name="currency_code" value="<?php echo $site->currency_code() ?>">
<input type="hidden" name="cbt" value="Return to <?php echo $site->title() ?>">
<input type="hidden" name="cancel_return" value="<?php echo url('cart') ?>">
<input type="hidden" name="return" value="<?php echo url('cart/paid') ?>">
<table cellpadding="6" rules="GROUPS" frame="BOX">
<thead>
<tr>
<th><?php echo $site->produit()->html() ?></th>
<th></th>
<th><?php echo $site->quantite()->html() ?></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th><?php echo $site->price()->html() ?></th>
</tr>
</thead>
<tbody>
<?php $i=0; $count = 0; $total = 0; ?>
<?php foreach($cart as $id => $quantity): ?>
<?php if($product = $products->findByURI($id)): ?>
<?php $i++; ?>
<?php $count += $quantity ?>
<tr>
<td class="image">
<div>
<input type="hidden" name="item_name_<?php echo $i ?>" value="<?php echo $product->title() ?>" />
<input type="hidden" name="amount_<?php echo $i ?>" value="<?php echo $product->price() ?>" />
<a href="<?php echo $product->url() ?>">
<?php if($image = $product->images()->sortBy('sort', 'asc')->first()): ?>
<?php echo thumb($image, array('width' => 640, 'height' => 412, 'crop' => true, 'quality' => 100)); ?>
<?php endif ?>
</a>
</div>
</td>
<td class="product">
<dl>
<dt><span><?php echo $site->titreprojet()->html() ?></span><span><?php echo $product->title()->html() ?></span></dt>
<dt><span><?php echo $site->auteur()->html() ?></span><span><?php echo $product->auteur()->html() ?></span></dt>
<dt><span><?php echo $site->langue()->html() ?></span><span><?php echo $product->langue()->html() ?></span></dt>
<dt><span><?php echo $site->format()->html() ?></span><span><?php echo $product->format()->html() ?></span></dt>
<dt><span><?php echo $site->pagination()->html() ?></span><span><?php echo $product->pages()->html() ?></span></dt>
<dt><span><?php echo $site->technique()->html() ?></span><span><?php echo $product->technique()->html() ?></span></dt>
<dt><span><?php echo $site->tirage()->html() ?></span><span><?php echo $product->tirage()->html() ?></span></dt>
<dt><span>ISBN</span><span><?php echo $product->isbn()->html() ?></span></dt>
</dl>
</td>
<td >
<input data-id="<?php echo $product->uid() ?>" data-quantity="<?php echo $quantity ?>" pattern="[0-9]*" class="quantity" type="hidden" name="quantity_<?php echo $i ?>" min="1" value="<?php echo $quantity ?>">
<?php echo $quantity ?>
</td>
<td class="add">
<a class="btn" href="<?php echo url('cart') ?>?action=add&id=<?php echo $product->uid() ?>">+</a>
</td>
<td class="sous">
<?php if ($quantity > 1): ?>
<a class="btn" href="<?php echo url('cart') ?>?action=remove&id=<?php echo $product->uid() ?>">-</a>
<?php endif ?>
<?php $prodtotal = floatval($product->price()->value)*$quantity ?>
</td>
<td></td>
<td class="retirer"><a class="btn" href="<?php echo url('cart') ?>?action=delete&id=<?php echo $product->uid() ?>"><?php echo $site->retirer()->html() ?></a></td>
<td><?php printf('%0.2f', $prodtotal) ?> <?php echo $site->currency_symbol() ?></td>
</tr>
<?php $total += $prodtotal ?>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<td class="empty"></td>
<td class="margin"><?php echo $site->soustotal()->html() ?></td>
<td class="empty"></td>
<td class="empty"></td>
<td class="empty"></td>
<td class="empty"></td>
<td class="empty"></td>
<td><?php printf('%0.2f', $total) ?> <?php echo $site->currency_symbol() ?></td>
</tr>
<tr>
<?php $postage = cart_postage($total) ?>
<td></td>
<td class="margin"><?php echo $site->fraisdeport()->html() ?></td>
<td class="empty"></td>
<input type="hidden" name="shipping_<?php echo $i ?>" value="<?php printf('%0.2f', $postage) ?>" />
<td class="empty"></td>
<td class="empty"></td>
<td class="empty"></td>
<td class="empty"></td>
<td><?php printf('%0.2f', $postage) ?> <?php echo $site->currency_symbol() ?></td>
</tr>
<tr>
<th class="empty"></th>
<th class="margin"><?php echo $site->montanttotal()->html() ?></th>
<th></th>
<td class="empty"></td>
<td class="empty"></td>
<th class="empty"></th>
<th class="empty"></th>
<th><?php printf('%0.2f', $total+$postage) ?> <?php echo $site->currency_symbol() ?></th>
</tr>
<tr>
<th><div class="submit">
<div>Etape 1/3</div></div>
</th>
<th colspan="8"><a class="btn" href="<?php echo page('forumulaire')->url() ?>">Paser à l'étape 3</a></th>
</tr>
</tfoot>
</table>
</form>
<section class="text">
// THE UNIFORM HERE
<div class="head">
<span><?php echo l::get('infocli') ?></span><span><?php echo l::get('adressefac') ?></span><span><?php echo l::get('adresseliv') ?></span>
</div>
<form action="<?php echo $page->url() ?>" method="POST">
<div>
<span><?php echo l::get('email') ?></span><input name="email_cli" placeholder="..." type="email" value="<?php echo $form->old('email_cli'); ?>">
<span><?php echo l::get('civilite') ?></span>
<div>
<?php $value = $form->old('civilite_cli') ?>
<label>
<!-- Set this as default -->
<input type="radio" name="civilite_cli" value="Mme"<?php e($value=='mme', ' checked')?>/> Mme
</label>
<label>
<input type="radio" name="civilite_cli" value="M"<?php e($value=='m', ' checked')?>/> M
</label>
</div>
<span><?php echo l::get('prenom') ?></span><input class="prenom civ" name="prenom_cli" placeholder="..." value="<?php echo $form->old('prenom_cli'); ?>">
<span><?php echo l::get('nom') ?></span><input class="nomun border" name="nom_cli" placeholder="..." value="<?php echo $form->old('nom_cli'); ?>">
</div>
<?php echo csrf_field(); ?>
<?php echo honeypot_field(); ?>
<div class="submit">
<div>Etape 2/3</div>
<input type="submit" value="Passer à l'étape 3">
</div>
</form>
<?php if ($form->success()): ?>
<div class="uniform-errors"><?php echo l::get('msgsuccess') ?></div>
<?php else: ?>
<?php snippet('uniform/errors', ['form' => $form]); ?>
<?php endif; ?>
</section>
</main>
<?php endif; ?>
<?php snippet('footer') ?>
And the controller from uniform is quite simple (controllers/cart.php)
<?php
use Uniform\Form;
return function ($site, $pages, $page)
{
$form = new Form([
'email_cli' => [
'rules' => ['required', 'email'],
'message' => "Vous n'avez pas rentrer d'email (Informations client)",
],
'prenom_cli' => [
'rules' => ['required'],
'message' => "Vous n'avez pas rentrer de prénom (Informations client)",
],
'civilite_cli' => [
'rules' => ['required' => [['mme', 'm']]],
'message' => "Vous n'avez pas rentrer de civilité (Informations client)",
],
'nom_cli' => [
'rules' => ['required'],
'message' => "Vous n'avez pas rentrer de nom (Informations client)",
],
]);
if (r::is('POST')) {
$form->emailAction([
'to' => 'mymail@gmail.com',
'from' => 'mailer@press.org',
'service' => 'html-mail',
'subject' => 'Nouvelle commande ',
'snippet' => 'emails/email',
])
->emailAction([
'to' => $form->data('email_cli'),
'from' => 'mailer@press.org',
'service' => 'html-mail',
'replyTo' => 'mymail@gmail.com',
'subject' => 'Merci pour votre commande',
'snippet' => 'emails/success',
]);
}
return compact('form');
};