2

I am trying to handle an error from a PFCloud function in Swift with if error != nil. Although neither the Cloud Code or Stripe return an error, Swift responds as if there was one.

Swift

PFCloud.callFunctionInBackground("authorize", withParameters: ["customer": self.customerID!, "cost": self.stripePrice, "type": self.stripeDescription) { (success: AnyObject?, error: NSError?) -> Void in
    print("Authorizing")
    if error != nil {
        print("authorizeError")
        self.displayAlert("Error", message: "There was an error authorizing your payment. Please try again later.")
    } else {
        if type == 1{
            print("one")
            self.performSegueWithIdentifier("one", sender: self)
            self.viewTimer.invalidate()
            self.cancelTimer.invalidate()
        } else {
            self.performSegueWithIdentifier("two", sender: self)
            print("two")
            self.viewTimer.invalidate()
            self.cancelTimer.invalidate()
        }
    }
}

Cloud Code (Node.js)

Parse.Cloud.define("authorize", function(request, response){
    var user = request.user;
    stripe.charges.create({ amount: request.params.cost, currency: "cad", customer: request.params.customer, capture: false}).then(function(charge) {
        user.set("chargeID", charge.id);
        return user.save(null, { useMasterKey: true });
    }).then(function(result) {
        console.log(charge);
        console.log(charge.id);
        response.success(charge); 
    }, function(err) {
        console.log(err);   
        console.log(charge.id)
        response.error(err);
    });
});
toddg
  • 2,863
  • 2
  • 18
  • 33
Dups
  • 201
  • 1
  • 2
  • 12

1 Answers1

1

The problem is that you're trying to return charge in the success callback and charge is null. Change your code as follows:

stripe.charges.create({ amount: request.params.cost, currency: "cad", customer: request.params.customer, capture: false}).then(function(charge) {
    user.set("chargeID", charge.id);
    return user.save(null, { useMasterKey: true });
}).then(function(result) {

    // charge is undefined here.
    console.log(charge);

    // Return something besides charge
    response.success("unused result"); 
}, function(err) {
    console.log(err);   
    console.log(charge.id)
    response.error(err);
});
toddg
  • 2,863
  • 2
  • 18
  • 33
  • Yes there's no error. A charge is successfully created on Stripe's side. – Dups Apr 11 '17 at 18:24
  • Just because a charge is created with Stripe doesn't mean that there's no error. Is the user saved with the proper `chargeId`? What prints to the console when on the line `console.log(charge.id`)? I suspect that `charge.id` is undefined which causes a runtime error and causes `response.success()` to never be executed. – toddg Apr 11 '17 at 20:24
  • Thank you. I removed the charge.id lines but it still acts as if there was an error. What else do you suggest I change? – Dups Apr 11 '17 at 22:32
  • Can you look in the parse-server logs to see what prints to the console when `console.log(charge)` executes? Also try printing out the error in your Swift code. Instead of `print("authorizeError")` try `print("authorizeError: \(error) ")` to see what the error says. – toddg Apr 11 '17 at 22:35
  • @Dups Also, is the user saved with the proper chargeId? – toddg Apr 11 '17 at 23:00
  • Thank you once again! Nothing is printed in the log regarding the charge, however, the user is indeed saved with the proper chargeID. I printed what you suggested for authorizeError and the error says `Optional(Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.})` – Dups Apr 11 '17 at 23:18
  • Rather than returning `charge`, which is most likely undefined since it's out of scope, try returning something else... like this: `response.success("unused response")` – toddg Apr 11 '17 at 23:21
  • Thank you so much! I did that, and removed the line console.log(charge) and it now works! Once again, thank you! – Dups Apr 11 '17 at 23:31
  • Ok, great! I updated my answer to specifically answer your question. Will you click the check mark to mark it as accepted since it solved your problem? – toddg Apr 11 '17 at 23:43
  • Just did it! Thanks again. – Dups Apr 12 '17 at 00:49