david david - 1 month ago 11
PHP Question

When submit Stripe form "Cannot charge a customer that has no active card"

I'm trying to get Stripe work with a Symfony2 project. When I submit my form, I got the error


Cannot charge a customer that has no active card


I don't know how to resolve it... :)
My controller

<?php

namespace L3O1\ProjetBundle\Controller;

use L3O1\ProjetBundle\Entity\Payment;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Model\UserInterface;

class PaymentController extends Controller {

public function chargeAction(Request $request, $id) {
$token = $request->request->get('stripeToken');
$email = $request->request->get('stripeEmail');
$amount = $request->request->get('amount');

$em = $this->getDoctrine()->getManager();
$projet = $em->getRepository('L3O1ProjetBundle:Projet')->find($id);

$projet->setSommeDepart($projet->getSommeDepart()+$amount);
$em->persist($projet);

if($this->getUser() != null) {
$payment = new Payment();
$payment->setUserId($this->getId());
$payment->setProjetId($projet->getId());
$payment->setAmount($amount);
$payment->setDate(new \Datetime());
$em->persist($payment);
}

$em->flush();

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

$customer = \Stripe\Customer::create(array(
'email' => $email,
'source' => $token
));

$charge = \Stripe\Charge::create(array(
//amount in cent
'amount' => $amount*100,
'currency' => 'eur',
'customer' => $customer->id,
));

return $this->render('L3O1ProjetBundle:Payments:charge.html.twig');
}
}


My form

{# Stripe payment #}
<script src="https://checkout.stripe.com/checkout.js"></script>
<form id="chargeForm" methode="POST" action="{{ path('l3o1_projet_charge', { 'id': app.request.get('id') }) }}">
<input type="number" name="amount" id="custom-donation" placeholder="Enter an amount" min="1.00" step "10.00"/>
<input type="hidden" name="stripeToken" id="stripeToken" value="">
<input type="hidden" name ="stripeEmail" id="stripeEmail" value="">
<button id="customButton" class="btn btn-lg"> Back this project </button>

<script>
var handler = StripeCheckout.configure({
key:'mykey',
//image
token: function(token, args) {
document.getElementById("stripeToken").value = token.id;
document.getElementById("stripeEmail").value = token.email;
document.getElementById("chargeForm").submit();
}
})

document.getElementById('customButton').addEventListener('click', function(e) {
handler.open({
name: '{{ projet.titre }}',
description: '{{ projet.shortdesc }}',
amount: document.getElementById('custom-donation').value*100,
currency: 'eur',
});
e.preventDefault();
});
</script>


Thanks for your help !

Answer

Ok, so I'm tired. My error was very simple, i writted "methode="POST"" instead of "method="POST""... My controller couldn't get what was in the URL because it can't find any POST form method.

Comments