$scope.payment object
Closed this issue · 5 comments
Hi,
in your examples there are couple of references to this object.
considering we are using Angular-Stripe in combination with Angular-Credit-Card, how you would initialize that object?
As far as i can see $scope.payment.card is the card object generated by ACC, but not sure about the other fields.
Thanks
Not sure if I understand the question. angular-stripe has no opinion on how you manage your data or forms— that's the big problem with the other Angular + Stripe glue out there. If you want to align your ngModel
properties such that the object that comes out has exactly the schema that Stripe wants, go for it. If you your data model is a bit different than Stripe is expecting, toss a simple function in between that maps the properties.
If you give me an example I might be able to help more.
Stripping down the question,
from your code:
return stripe.card.createToken($scope.payment.card)
.then(function(token) {
console.log('token created for card ending in ', token.card.last4);
var payment = angular.copy($scope.payment);
payment.card = void 0;
payment.token = token.id;
return $http.post('https://yourserver.com/payments', payment);
})
.then(function(payment) {
console.log('successfully submitted payment for $', payment.amount);
})
.catch(function(err) {
if (err.type && /^Stripe/.test(err.type)) {
console.log('Stripe error: ', err.message);
} else {
console.log('Other error occurred, possibly with your API', err.message);
}
});
I have no idea how your $scope.payment is initialized. Is it just an empty object, with a field card?
Initialize however you want. It just needs to match the schema that Stripe expects when you send it.
Perfect!
Worth adding in your snippets maybe a
$scope.payment = {}
ps.: Thanks for the great effort in creating and maintaining this two libraries!
🎉
The reason I don't do that is that ngModel
will set deep properties without issue even if the parent object doesn't exist yet. So in many cases you don't need to explicitly initialize an empty object.