Reputation: 5758
I'm having a bit of trouble processing payments using Stripe Connect
. For some reason as soon as I submit my form I get this error:
A network error has occurred, and you have not been charged. Please try again
They way I have set up my system is that a user can sign in with Stripe and this gets the following details sent back from Stripe which I save into the database along with the users id.
On my payments page, I have this script:
Stripe.setPublishableKey('<?= $publishable_key; ?>');
var stripeResponseHandler = function(status, response) {
var $form = $('#payment-form');
$form.find('.form-error').text("")
$form.find('.error').removeClass("error")
validate = validateFields();
if (response.error) {
error = 0;
// Show the errors on the form
if (response.error.message == "This card number looks invalid"){
error = error + 1;
$form.find('.card_num').text(response.error.message);
$('#dcard_num').addClass("error");
}
if (response.error.message == "Your card number is incorrect."){
error = error + 1;
$form.find('.card_num').text(response.error.message);
$('#dcard_num').addClass("error");
}
if (response.error.message == "Your card's expiration year is invalid."){
error = error + 1;
$form.find('.exp').text(response.error.message);
$('#dexp').addClass("error");
}
if (response.error.message == "Your card's expiration month is invalid."){
error = error + 1;
$form.find('.exp').text(response.error.message);
$('#dexp').addClass("error");
}
if (response.error.message == "Your card's security code is invalid."){
error = error + 1;
$form.find('.cvc').text(response.error.message);
$('#dcvc').addClass("error");
}
if (error == 0){
$form.find('.payment-errors').text(response.error.message);
}
$form.find('button').prop('disabled', false);
} else {
if (validate == 1){
// token contains id, last4, and card type
var token = response.id;
// Insert the token into the form so it gets submitted to the server
$form.append($('<input type="hidden" name="stripeToken" />').val(token));
// and re-submit
$form.get(0).submit();
}
}
};
For some reason, the validation never happens, and I do not get a token for the card details. As a result the next part of my code, where I actually charge the user does not get run:
global $wpdb;
$author_id = get_the_author_meta('id');
$stripe_connect_account = $wpdb->get_row("SELECT * FROM wp_stripe_connect WHERE wp_user_id = $author_id", ARRAY_A);
if($stripe_connect_account != null){
$publishable_key = $stripe_connect_account['stripe_publishable_key'];
$secret_key = $stripe_connect_account['stripe_access_token'];
}
$charging = chargeWithCustomer($secret_key, $amountToDonate, $currency_stripe, $stripe_usr_id);
This is the chargeWithCustomer
function:
function chargeWithCustomer($secret_key, $amountToDonate, $currency, $customer) {
require_once('plugin/Stripe.php');
Stripe::setApiKey($secret_key);
$charging = Stripe_Charge::create(array("amount" => $amountToDonate,
"currency" => $currency,
"customer" => $customer,
"description" => ""));
return $charging;
}
If anyone could help me on this issue I'd appreciate it. I'm confused as to where I am going wrong and I Can't find an answer in Stripes documentation.
Upvotes: 4
Views: 4593
Reputation: 598
In case you haven’t read the whole series, or don’t know the secret sauce, Stripe.js sends the payment information directly to Stripe and gets an associated, unique token in return. That token then gets submitted to your server and is used to actually charge the customer.
if you are wondering how a charge attempt could still fail, then truth be told, you should know that the Stripe.js process really does only two things:
1)Gets the payment information to Stripe in a secure way (limiting your liability) 2)Verifies that the payment information looks usable
**Handling a declined card is a bit more complicated, because you need to find out why the card was declined and provide that information to the customer so he or she can correct the problem. The goal is to get the specific reason for the decline from the exception. This is a multistep process:
1)Get the total response, in JSON format, from the exception
2)Get the error body from the response
3)Get the specific message from the error body**
require_once('path/to/lib/Stripe.php');
try {
Stripe::setApiKey(STRIPE_PRIVATE_KEY);
$charge = Stripe_Charge::create(array(
'amount' => $amount, // Amount in cents!
'currency' => 'usd',
'card' => $token,
'description' => $email
));
} catch (Stripe_CardError $e) {
}
Knowing what kinds of exceptions might occur, you can expand this to watch for the various types, from the most common (Stripe_CardError) to a catch-all (Stripe_Error):
require_once('path/to/lib/Stripe.php');
try {
Stripe::setApiKey(STRIPE_PRIVATE_KEY);
$charge = Stripe_Charge::create(array(
'amount' => $amount, // Amount in cents!
'currency' => 'usd',
'card' => $token,
'description' => $email
));
} catch (Stripe_ApiConnectionError $e) {
// Network problem, perhaps try again.
} catch (Stripe_InvalidRequestError $e) {
// You screwed up in your programming. Shouldn't happen!
} catch (Stripe_ApiError $e) {
// Stripe's servers are down!
} catch (Stripe_CardError $e) {
// Card was declined.
}
Hope this helps...!
Upvotes: 4