stripe payment firebase cloud function not updating database correctly











up vote
0
down vote

favorite












I am using stripe payments in my app - trying to implement a user balance.



Currently, I have the following code in my index.js. The key bit for the question is the final .then() block of code. This section writes to my Cloud firestore database. However, it doesn't seem to actually update my database. When I look at my database, the balance doesn't change nor does the charge field get added to the stripe payment object that is saved in the database. I don't get any errors and I am not sure how to debug it.



const functions = require('firebase-functions'); //firebase functions library
const admin = require('firebase-admin'); //firebase admin database
admin.initializeApp(functions.config().firebase);
//initialse stripe with apikey
const stripe = require('stripe')('<stripe test key>')

//name of function is stripe charge
exports.stripeCharge = functions.database
//whenever a new payment is written to the database, the following will be invoked
.ref('/payments/{userId}/payments/{paymentId}')
//event object has the data from the node in the database which is the payment and the token
.onWrite((change, context) => {
const payment = change.after.val();
const userId = context.params.userId;
const paymentId = context.params.paymentId;

let balance = 0;

//return null if there is no payment or if payment already has a charge
if(!payment || payment.charge) return;

//now we chain a promise - this is the user in the database
//this is good for recurring charges or to save customer data in the database
return admin.database()
.ref(`/users/${userId}`)
//once gives a single snapshot of this data
.once('value')
//return the snapshot value
.then(snapshot => {
return snapshop.val();
})

//tell stripe to charge the customer with the payment token
.then(customer => {

balance = customer.balance;

const customerChargeAmount = payment.amount;
const idempotency_key = paymentId; //prevents duplicate charges on customer cards
const source = payment.token.id;
const currency = 'gbp';
const charge = {customerChargeAmount, currency, source};

return stripe.charges.create(charge, {idempotency_key});
})

//wait for stripe to return the actual stripe object which tells us whether the charge succeeded
.then(charge => {

if(!charge) return;

let updates = {};
updates[`/payments/${userId}/payments/${paymentId}/charge`] = charge

if(charge.paid){
balance += charge.amount;
updates[`/users/${userId}/balance`] = balance;
}

admin.database().ref().update(updates);
return true;
})
})


Any help is appreciated.










share|improve this question


















  • 1




    Check your firebase functions log history and check if it has been executed at all. if not, make sure you make an update on your user payment node in the DB
    – TheUnreal
    Nov 11 at 16:49










  • Checked the logs, function is definitely being executed. How can I ensure an update is being made on the user payment node? One part of the index.js code also creates a stripe object and stores it in my DB. But, in the last .then block, nothing is being updated.
    – JonnySprouse
    Nov 11 at 20:37















up vote
0
down vote

favorite












I am using stripe payments in my app - trying to implement a user balance.



Currently, I have the following code in my index.js. The key bit for the question is the final .then() block of code. This section writes to my Cloud firestore database. However, it doesn't seem to actually update my database. When I look at my database, the balance doesn't change nor does the charge field get added to the stripe payment object that is saved in the database. I don't get any errors and I am not sure how to debug it.



const functions = require('firebase-functions'); //firebase functions library
const admin = require('firebase-admin'); //firebase admin database
admin.initializeApp(functions.config().firebase);
//initialse stripe with apikey
const stripe = require('stripe')('<stripe test key>')

//name of function is stripe charge
exports.stripeCharge = functions.database
//whenever a new payment is written to the database, the following will be invoked
.ref('/payments/{userId}/payments/{paymentId}')
//event object has the data from the node in the database which is the payment and the token
.onWrite((change, context) => {
const payment = change.after.val();
const userId = context.params.userId;
const paymentId = context.params.paymentId;

let balance = 0;

//return null if there is no payment or if payment already has a charge
if(!payment || payment.charge) return;

//now we chain a promise - this is the user in the database
//this is good for recurring charges or to save customer data in the database
return admin.database()
.ref(`/users/${userId}`)
//once gives a single snapshot of this data
.once('value')
//return the snapshot value
.then(snapshot => {
return snapshop.val();
})

//tell stripe to charge the customer with the payment token
.then(customer => {

balance = customer.balance;

const customerChargeAmount = payment.amount;
const idempotency_key = paymentId; //prevents duplicate charges on customer cards
const source = payment.token.id;
const currency = 'gbp';
const charge = {customerChargeAmount, currency, source};

return stripe.charges.create(charge, {idempotency_key});
})

//wait for stripe to return the actual stripe object which tells us whether the charge succeeded
.then(charge => {

if(!charge) return;

let updates = {};
updates[`/payments/${userId}/payments/${paymentId}/charge`] = charge

if(charge.paid){
balance += charge.amount;
updates[`/users/${userId}/balance`] = balance;
}

admin.database().ref().update(updates);
return true;
})
})


Any help is appreciated.










share|improve this question


















  • 1




    Check your firebase functions log history and check if it has been executed at all. if not, make sure you make an update on your user payment node in the DB
    – TheUnreal
    Nov 11 at 16:49










  • Checked the logs, function is definitely being executed. How can I ensure an update is being made on the user payment node? One part of the index.js code also creates a stripe object and stores it in my DB. But, in the last .then block, nothing is being updated.
    – JonnySprouse
    Nov 11 at 20:37













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am using stripe payments in my app - trying to implement a user balance.



Currently, I have the following code in my index.js. The key bit for the question is the final .then() block of code. This section writes to my Cloud firestore database. However, it doesn't seem to actually update my database. When I look at my database, the balance doesn't change nor does the charge field get added to the stripe payment object that is saved in the database. I don't get any errors and I am not sure how to debug it.



const functions = require('firebase-functions'); //firebase functions library
const admin = require('firebase-admin'); //firebase admin database
admin.initializeApp(functions.config().firebase);
//initialse stripe with apikey
const stripe = require('stripe')('<stripe test key>')

//name of function is stripe charge
exports.stripeCharge = functions.database
//whenever a new payment is written to the database, the following will be invoked
.ref('/payments/{userId}/payments/{paymentId}')
//event object has the data from the node in the database which is the payment and the token
.onWrite((change, context) => {
const payment = change.after.val();
const userId = context.params.userId;
const paymentId = context.params.paymentId;

let balance = 0;

//return null if there is no payment or if payment already has a charge
if(!payment || payment.charge) return;

//now we chain a promise - this is the user in the database
//this is good for recurring charges or to save customer data in the database
return admin.database()
.ref(`/users/${userId}`)
//once gives a single snapshot of this data
.once('value')
//return the snapshot value
.then(snapshot => {
return snapshop.val();
})

//tell stripe to charge the customer with the payment token
.then(customer => {

balance = customer.balance;

const customerChargeAmount = payment.amount;
const idempotency_key = paymentId; //prevents duplicate charges on customer cards
const source = payment.token.id;
const currency = 'gbp';
const charge = {customerChargeAmount, currency, source};

return stripe.charges.create(charge, {idempotency_key});
})

//wait for stripe to return the actual stripe object which tells us whether the charge succeeded
.then(charge => {

if(!charge) return;

let updates = {};
updates[`/payments/${userId}/payments/${paymentId}/charge`] = charge

if(charge.paid){
balance += charge.amount;
updates[`/users/${userId}/balance`] = balance;
}

admin.database().ref().update(updates);
return true;
})
})


Any help is appreciated.










share|improve this question













I am using stripe payments in my app - trying to implement a user balance.



Currently, I have the following code in my index.js. The key bit for the question is the final .then() block of code. This section writes to my Cloud firestore database. However, it doesn't seem to actually update my database. When I look at my database, the balance doesn't change nor does the charge field get added to the stripe payment object that is saved in the database. I don't get any errors and I am not sure how to debug it.



const functions = require('firebase-functions'); //firebase functions library
const admin = require('firebase-admin'); //firebase admin database
admin.initializeApp(functions.config().firebase);
//initialse stripe with apikey
const stripe = require('stripe')('<stripe test key>')

//name of function is stripe charge
exports.stripeCharge = functions.database
//whenever a new payment is written to the database, the following will be invoked
.ref('/payments/{userId}/payments/{paymentId}')
//event object has the data from the node in the database which is the payment and the token
.onWrite((change, context) => {
const payment = change.after.val();
const userId = context.params.userId;
const paymentId = context.params.paymentId;

let balance = 0;

//return null if there is no payment or if payment already has a charge
if(!payment || payment.charge) return;

//now we chain a promise - this is the user in the database
//this is good for recurring charges or to save customer data in the database
return admin.database()
.ref(`/users/${userId}`)
//once gives a single snapshot of this data
.once('value')
//return the snapshot value
.then(snapshot => {
return snapshop.val();
})

//tell stripe to charge the customer with the payment token
.then(customer => {

balance = customer.balance;

const customerChargeAmount = payment.amount;
const idempotency_key = paymentId; //prevents duplicate charges on customer cards
const source = payment.token.id;
const currency = 'gbp';
const charge = {customerChargeAmount, currency, source};

return stripe.charges.create(charge, {idempotency_key});
})

//wait for stripe to return the actual stripe object which tells us whether the charge succeeded
.then(charge => {

if(!charge) return;

let updates = {};
updates[`/payments/${userId}/payments/${paymentId}/charge`] = charge

if(charge.paid){
balance += charge.amount;
updates[`/users/${userId}/balance`] = balance;
}

admin.database().ref().update(updates);
return true;
})
})


Any help is appreciated.







angular firebase google-cloud-firestore stripe-payments payment






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 16:26









JonnySprouse

797




797








  • 1




    Check your firebase functions log history and check if it has been executed at all. if not, make sure you make an update on your user payment node in the DB
    – TheUnreal
    Nov 11 at 16:49










  • Checked the logs, function is definitely being executed. How can I ensure an update is being made on the user payment node? One part of the index.js code also creates a stripe object and stores it in my DB. But, in the last .then block, nothing is being updated.
    – JonnySprouse
    Nov 11 at 20:37














  • 1




    Check your firebase functions log history and check if it has been executed at all. if not, make sure you make an update on your user payment node in the DB
    – TheUnreal
    Nov 11 at 16:49










  • Checked the logs, function is definitely being executed. How can I ensure an update is being made on the user payment node? One part of the index.js code also creates a stripe object and stores it in my DB. But, in the last .then block, nothing is being updated.
    – JonnySprouse
    Nov 11 at 20:37








1




1




Check your firebase functions log history and check if it has been executed at all. if not, make sure you make an update on your user payment node in the DB
– TheUnreal
Nov 11 at 16:49




Check your firebase functions log history and check if it has been executed at all. if not, make sure you make an update on your user payment node in the DB
– TheUnreal
Nov 11 at 16:49












Checked the logs, function is definitely being executed. How can I ensure an update is being made on the user payment node? One part of the index.js code also creates a stripe object and stores it in my DB. But, in the last .then block, nothing is being updated.
– JonnySprouse
Nov 11 at 20:37




Checked the logs, function is definitely being executed. How can I ensure an update is being made on the user payment node? One part of the index.js code also creates a stripe object and stores it in my DB. But, in the last .then block, nothing is being updated.
– JonnySprouse
Nov 11 at 20:37

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250761%2fstripe-payment-firebase-cloud-function-not-updating-database-correctly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250761%2fstripe-payment-firebase-cloud-function-not-updating-database-correctly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation