I just started exploring Stripe for handling payments in node.js. I want to create a payment system that goes as following:
I create a workspace and I start a Stripe subscription of 10 dollars / month.
When someone joins my workspace it will cost me 10 dollar / month extra.
So, when I want to add a person to my subscription. How would I handle this? I found the below function, but I was wondering two things:
How do I add one person to this subscription? It now says quantity: 2, but how do I simply increment 1 with every user?
in the example below I use ID 'sub_6OZnwv0DZBrrPt' to retrieve this Stripe subscription. I was wondering from where I can get this ID? I could save this subscription ID in the workspace mongo database document after I created this subscription, but I'm not sure if it is safe to keep it like that in my database? Let me know if you have any suggestions.
this is the function to update a subscription
stripe.subscriptions.update(
'sub_6OZnwv0DZBryPt',
{ quantity: 2 },
(err, subscription) => {
// asynchronously called
}
);
On 1.) you need to retrieve the current subscription based on the stored ID. This can be done as:
stripe.subscriptions.retrieve(
"sub_6OZnwv0DZBryPt",
function(err, subscription) {
// asynchronously called
}
);
The subscription object will have information about the current quantity (see the example response and doc on the subscription object). Which leads to your second question.
To retrieve the subscription you need to store the ID only. It's safe to do so, the ID is meaningless to others unless they have your test / live keys as well. Be sure you secure those keys, and feel free to store IDs like subscription_id, customer_id, etc.
Related
Can any body tell how to filter out Subscription from a particular price id. Any kind of API is present then please share here. (preferred Node.js)
In node.js, you can list subscriptions that contain a specific price ID using the subscription.list() method and passing the price parameter, like this:
const subscriptions = await stripe.subscriptions.list({
price: "{{PRICE_ID}}",
});
I have a webapp where users can join a variable number of chatrooms.
The firestore collection looks like this:
chatrooms: {
"g4HL09vHfkaO3": {
"name": "Chatroom 1",
"messages": ...
},
"lpScgY74gHJ87": {
"name": "Chatroom 2",
"messages": ...
}
}
When the user joins a chatroom, the webapp will attach a listener to the given document in order
to get the messages and receive incoming ones, e.g.:
unsubscribe = firebase
.firestore()
.collection("chatrooms")
.doc("g4HL09vHfkaO3")
.onSnapshot( ... )
and when the user leave the chatroom, the app will detach the listener like this:
unsubscribe()
Now my goal is to display in the app the amount of users currently inside a given chatroom, so the question is:
is it possible to get from firebase the global number of listeners attached to e.g. chatroom g4HL09vHfkaO3?
if not, what is the most straightforward way to achive what I want?
Firestore has no public API that return the number of listeners to a collection. If you want to know the number of people in a chat room, you can either:
Keep a counter in each room document that each user increments and decrements.
Use the Realtime Database for building a presence system for each room. This is described in the documentation as the solution: https://firebase.google.com/docs/firestore/solutions/presence
There is the way where you can get the number of objects attached to the parent.
You listen to the collection chatrooms and you can use the method called numChildren() provided by the firebase to get the total size of the list of users that are using the chatroom.
The link for reference where it is explained in detail is:
reference
I what to create a query for firebase firestore that gives me the "hottest" posts. I want a query like this to work with nodejs:
SELECT Title, LOG10(Likes + 1) * 86400 / .301029995663981 + UNIX_TIMESTAMP(Submitted) AS Hotness
FROM article
ORDER BY Hotness DESC
What is the best way of doing this with firestore?
Credit for this query: Reddit hotness score
One solution is to add a field in each post document with the value of the calculation.
Each time a post doc is updated, you would update this field through a Cloud Function (i.e. from the back-end). Since Cloud Functions code is executed with admin privileges, you can write a security rule that prevents the user directly updating this field (See the doc), and still have the field updated by the Cloud Functions.
To do the calculation, you need the nbr of likes, which you should have and the creation date.
For the creation date, just do do as follows when you create the doc:
....collection('posts').add({foo: 'bar', ..., createdOn: firebase.firestore.FieldValue.serverTimestamp()});
This way, your query is a very simple orderBy query, see the Firestore doc.
I'm having a big deal - the meteor app I've been developing the last weeks is finally online. But, for an update, I need to add a field to my users profile.
I thought that walling a methods with the following code would work :
updateUsrs_ResetHelps: function(){
if(Meteor.users.update({}, {
$set: {
'profile.helps': []
}
}))
console.log("All users profile updated : helps reset");
else
throw new Meteor.Error(500, 'Error 500: updateUsrs_ResetHelps',
'the update couldn\'t be performed');
}
The problem is that my users have the classic Meteor.accounts document, whith emails, _id, services, profile, etc... but, in the profile, they don't have a .helps fields. I need to create it.
For the future users, I've modified the accounts creation function to add this fields when they sign up, but for the 200 users I already got signed up, I do really need a solution.
EDIT : Might it be because of the selector in the update ? Is a simple {} selector valid to update all the users / documents of the collection at once ?
From the Mongo documentation (http://docs.mongodb.org/manual/reference/method/db.collection.update/):
By default, the update() method updates a single document. Set the
Multi Parameter to update all documents that match the query criteria.
If you've already taken care of adding the field for new users and you just need to fix the old ones, why not just do it one time directly in the database?
Run meteor to start your application, then meteor mongo to connect to the database. Then run an update on records where the field doesn't already exist. Something like:
db.users.update({"profile.helps": {"$exists": false}}, {"$set": {"profile.helps": []}}, {multi:true})
The Mongo documentation specifies the multi parameter as:
Optional. If set to true, updates multiple documents that meet the
query criteria. If set to false, updates one document. The default
value is false.
First time working with Stripe API. Implementing it on WordPress using PHP and JS.
Working on a donation form. Donor should be able to choose a suggested amount (radio buttons-25,50,75,100) or pay as he/she wishes (text field after selecting 'other'). I was able to get this working.
There is a check box to set the amount up as a recurring payment. I created recurring payment plans for the fixed options like 25, 50, 100 etc.
How do I set up a recurring payment if the donor chooses a custom amount? Can't find the relevant API. Please help.
Another approach that Stripe suggests is to setup a plan with a recurring amount of $1 (or $0.01 for more flexibility) and then vary the quantity as needed.
e.g. Using the $0.01 plan approach, if I wanted to charge 12.50/month I could adjust the quantity like so:
$customer->subscriptions->create(array("plan" => "basic", "quantity" => "1250"));
Stripe Support
How can I create plans that don't have a fixed price?
Subscription Quantities
First, you'll need to create a new customer.
On submit, you could use the custom amount to create a new plan:
$current_time = time();
$plan_name = strval( $current_time );
Stripe_Plan::create(array(
"amount" => $_POST['custom-amount'],
"interval" => "month",
"name" => "Some Plan Name " . $_POST['customer-name'],
"currency" => "usd",
"id" => $plan_name
)
);
Keep in mind that the 'id' needs to be unique. You could use the customer's name, a time stamp, or some other random method to ensure that this is always the case.
You'd then just create the subscription on the newly-added customer:
$customer = Stripe_Customer::retrieve($customer_just_created);
$customer->subscriptions->create(array("plan" => $plan_name));
You probably will be able to omit the first line above, as you should already have a customer variable assigned from when the customer was actually created.
This is low tech, but the easiest thing I found was to use as little of the Stripe API as possible. Instead of creating subscription plans and products and things like that, you just:
Create a stripe customer.
Charge them with a cron job.
If you already know how to charge somebody, you just have to figure out how to create the customer, and then the rest is good to go.
This means you shift some of Stripe's cleverness to our own infrastructure, but I actually found this easier to think about/maintain than thinking through all of Stripe's API docs and features.