0
votes

Hope you can help me. I have a Parse Cloud function in Sashido (Parse Server) to manage a Stripe Subscription as following:

var stripe = require("stripe")("sk_test_CCCCccCCXXXXXXxXXX");
Parse.Cloud.define("crearCargo", function(request, response) {
    var  token = request.params.stripeToken;
    var  mail = request.params.email;
//var mail = request.params.email;

    //Crear Customer
    const customer = stripe.customers.create({
        email: mail,
        source: token,
    }, function(err, customer) {
        // asynchronously called
        if(err){
            response.error("Fallo Customer");
        }else{
           //const{id} = customer;
            id = customer.id;
            stripe.subscriptions.create({

                    customer: id,
                    items: [
                        {
                            plan: "plan_E0jrObw8X7Le2F",

                        },
                    ]
                }, function(err, subscription) {
                    if(err){
                        response.error(err.message);
                    }else{                        
                        response.success(subscription.id);
                    }
                }
            );
        }

    });

});

I call that function from my site via php like this:

$results = ParseCloud::run("crearCargo", ["stripeToken" => "$stripeToken", "email" => "$email"]);

This works fine when credit card is ok, but when I use a declined credit card to deal with the errors, I cant get error message in my php code, eventhough I see the error in the Log in Sashido dashboad. This is the log:

Failed running cloud function crearCargo for user undefined with:
  Input: {"stripeToken":"tok_1DaoDfHWMeJb0DRPDaAgN7rS","email":"[email protected]"}
  Error: {"code":141,"message":"Your card was declined."}
Nov 26, 2018, 12:50:44 -05:00 - ERROR
Error generating response for [POST] /1//functions/crearCargo 
"Your card was declined."

{
  "stripeToken": "tok_1DaoDfHWMeJb0DRPDaAgN7rS",
  "email": "[email protected]"
}

So I havent been able to deal with errors and instead I receeve a HTTP 500 ERROR in my browser, Do you have any clues why?

This is how a deal with the $result and works fine if credit card is valid, I do recieve the subscription code:

try {
    if(substr( $results, 0, 3 ) === "sub"){

       echo $results;

   }
} catch (ParseException $e) {
      echo 'Caught exception: '.$e->getMessage()."\n";
}

So when I use a 4242424242424242 credit card I do recieve subcription code, but when I force an error with credit card number 4100000000000019, I cant get error message back.

This is exactly what I received when I use PHP display error with ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

Error recieved:

Fatal error: Uncaught Parse\ParseException: Your card was declined. in /home/u940759797/domains/powersellapp.com/public_html/web/Modelo/src/Parse/ParseClient.php:357 Stack trace: #0 /home/u940759797/domains/powersellapp.com/public_html/web/Modelo/src/Parse/ParseCloud.php(32): Parse\ParseClient::_request('POST', 'functions/crear...', NULL, '{"stripeToken":...', false) #1 /home/u940759797/domains/powersellapp.com/public_html/web/Modelo/suscripcion.php(28): Parse\ParseCloud::run('crearCargo', Array) #2 {main} thrown in /home/u940759797/domains/powersellapp.com/public_html/web/Modelo/src/Parse/ParseClient.php on line 357

1
If you instruct PHP to show detailed errors (ini_set('error_reporting', E_ALL); error_reporting(E_ALL); ) what errors do you see on your php page when you try with an invalid card (4100000000000019, etc)duck
It shows me an Uncought Parse Exception with error code 141 and the error message Ivalid Credit Card. But I dont know how to catch itA ti te digo
I edited the questios, that is exactly what I recieved. any Ideas?A ti te digo
Try a simple try { $results = ParseCloud::run(... your args ...); } catch(Exception $e) { print_r($e); } does it echo out the exception?duck
Great duck, that was the thing!!!! Tnks bro...A ti te digo

1 Answers

1
votes

I suspect the issue is your ParseCloud::run call is not within your try-catch block. Try something like this:

try {
  $results = ParseCloud::run("crearCargo", ["stripeToken" => "$stripeToken", "email" => "$email"]);
} 
catch(ParseException $e) {
  echo 'Caught exception: '.$e->getMessage()."\n";
}
catch(Exception $e) {
  // do something with other exceptions, for testing we'll just print it out
  print_r($e);
}