I am a beginner with rails and webdevelopment or design and I want to build a site that sells single digital designs. I want to use stripe checkout for this and I am not clear what I need so that when the sale is done over at stripes I use the payment confirmation to ‘lock’ that sold item and mark it as sold. In broad strokes, what does the rails backend need to have to do that? Do I have to temp lock the item immediately when the sale is underway and if it goes through how do I get the item marked as sold and unavailable. Like I said, newbie here so would love to hear what is involved in implementing that. Do not need a cart - only one item per sale. Thank you and let me know what you need to know to give advice.
And happy new year too
I haven’t used Stripe, but I’ve worked in integration at a payments provider.
I imagine that you’d be integrating Stripe into your site via their API. When you send (POST) a payment to Stripe for processing, there should be a response that you get, most likely in JSON or XML. It would contain information on the status of the payment you sent and, if successful, you can then set a property on the sale item e.g. item.available = false. In your erb template, you’d be using this property to show the item as either available or sold.
thank you, I think the checkout works as a link so the customer is directed to a stripe page and pays there. But you make me realize that I have to read the stripe docs for that solution to know a little what I am talking about. But, even if highly unlikely - in that time the customer is over at stripe completing the sale (or possibly cancelling it) the item must already not be available, right? Can’t sell it twice … … actually stripe will probably not make that possible I would think…
I think that the link used to refer the customer to Stripe for payment will contain a transaction ID. You’d likely be able to query Stripe’s API to get the transaction status. Or, more likely, when you sign up with Stripe for their payments workflow, you’d likely provide a callback URL. This would point to a URL on your server which Stripe can ping with the status of each transaction, enabling you to do further processing based on said status.
I would go ahead and implement a cart, so that when an item is placed in the cart, one can then decrement the number of available units. This could be reversed if the payment fails or if the customer abandons the transaction.
Yes, reading the Stripe docs is imperative. It will provide concrete answers and help with designing the implementation of your payment workflow.
thank you so much, that clears it up some. I will investigate and report back here. Cheers. About the cart - these are single designs there is only ever one unit of something … very rare that someone would want to buy two at once. They would be fine with making that 2 transactions. Oh, you said decrement one, yep - will go and read about the checkout and cart.
You’re welcome. I see… maybe a cart would be overkill. Let’s see how your research goes… I’ll follow your progress and help how I can.
Have a read into the idea of “Pessimistic Locking”. It’s the name of the theory I think you want to run with. If someone enters the payment flow for an item (a one-of-a-kind item) then that item should immediately be locked so that nobody else can enter the payment flow at the same time, be a little bit faster on the draw with their credit card, and snipe it out from under the person who “saw it first”. If the payment is unsuccessful, then the lock should be released so the next person could buy it.
Now that I think about it, this would make an excellent feature to demonstrate how the various Turbo bits work in Rails 7. If someone was on the page showing that item, it could update to show when another customer started paying for it. And they got sticker shock for shipping to Lithuania, then the page could update for other customers in real time to say “it’s available again”.
Walter
Hi Roadie take a look at rockers.de we sell unique records.
Hey thank you for that! Great example to look at. And you list by images like I will have to (I design book covers). That auto magnifying feature looks great (best one I have come across so far). It’s exactly what I need as well - had not even thought about that. You use paypal and ueberweisung - so you have to deal with / store customer data? I was hoping to just need a session token for the whole transaction … but you need a shipping location (unless they pick up the record) and I just an email …
I like that in your mobile view you can just tap on a record to bring up the details but then you can scroll on without having to go back. neat. Since bookcovers have the ‘smartphone aspect ratio’ I want to have scrolling where the cover takes up the whole screen and tap for info overlay that fades out when you scroll on. … I think. Have to see if it works nicely or confuses.
Torsten
So I asked stripe support how I should list my product: cover design = 1 unit product. They are going to get back to me by mail. I would like to have one product as in Family of cover designs. And then have an identifier that goes with each unique cover so I know which one was bought. It is not really covered in the docs (which are excellent). People sell one service a million times or a t-shirt size ‘so and so’ a thousand times. I wonder how art galleries would set up their products on stripe. Would they list on stripe hundreds of products and take each listing down when its sold? That is tedious … . How would the pragmatic programmer do this? You know when you make a payment there is a field that goes through all the way? (verwendungszweck in german) - I wonder if I can have that as an identifier in each stripe ‘buy me button’ link?
I mean - the transaction ID is generated when things happen, right? If all are the same ‘premade cover’ product I need the identifier that says: ‘this one was bought’… I will blog more when stripe support mails me.
Stripe support wrote and say I will have to list each cover as a single product if it can only be sold once. So if I want to go up one level and use ‘cover’ as the product name then rails needs to keep track of which cover it was.
Can I send something uniqe with this button or can/do I pin the specific transaction ID to the ‘cover’ whose button was pushed? …
…or, am I clueless and thinking about this the wrong way?
Are all of your “covers” the same exact price, just unique in terms of being one-of-a-kind? A Stripe transaction ID is returned by Stripe to your app after each successful payment. You could update the individual cover that the person bought with that ID, so you’d have a unique key to indicate both that this cover is sold (don’t release your pessimistic lock after some period of time) and that this is the one that refers to that particular successful purchase.
Or am I under-thinking this…
Walter
Hi Walter, yeah that is right. I have 2 price categories but stripe lets me add multiple prices to one product.
So how does that transaction ID come alive? - on the stripe server I think when the purchase is successful. If the customer is then send back does he carry this transaction ID with him? How does rails know that the client now is the one that made the purchase? What if client looses internet after paying, and comes back later. I don’t want to have them log in to buy something… If I have “one” product - I have “one” buy button cloned across creation and my gallery shop, right? so stripe will tell me “hey, you sold a cover, congrats - good luck figuring out which one it is …” or how does this work?
So I need to have their email before they go and throw money at me I guess. And when stripe says the deal is done and comes back with the transaction number … does it come with the session ID or something so Rails can deduce the email belonging to the client? Or am I overthinking this?
In the last Stripe cart I built, the request is sent (through JavaScript) from my site to Stripe, which responds back with a token if successful.
Here’s what that looks like:
<script type="text/javascript">
Stripe.setPublishableKey("<%= Rails.application.secrets.stripe_publishable_key or fail 'No Stripe publishable key found.' %>")
</script>
<script type="text/javascript">
var stripeResponseHandler = function(status, response) {
var $form = $('#payment_form');
if (response.error) {
$form.find('#stripe_error').text(response.error.message).addClass('alert alert-danger').show();
$form.find('#submit_payment').prop('disabled', false);
} else {
var token = response.id;
$('#payment_stripe_token').val(token);
console.log(response);
$form.get(0).submit();
}
};
jQuery(function($) {
$('#payment_form').submit(function(e) {
var $form = $(this);
$form.find('#submit_payment').prop('disabled', true);
if ( $('#card_number').length ) {
Stripe.card.createToken($form, stripeResponseHandler);
return false;
} else {
return true;
}
});
});
</script>
This is using the Stripe JS API v2, I think. That token is perfectly safe to store in your application – there’s nothing in it to get you yelled at by privacy police or audited by credit card companies.
The trick to this is that the fields you will send to Stripe have no name
attribute, like this:
<div class="form-group">
<%= label_tag :card_code, "Card Security Code" %>
<%= text_field_tag :card_code, nil, name: nil, class: 'form-control', data: { stripe: 'cvc' } %>
</div>
This means they won’t be sent to your server at all. The Stripe JS scoops them up, and POSTs them to its endpoint, including the customer’s billing address, total charge, etc. The response from that request is either a success header and a Stripe token or a failure header with an array of possible reasons why it failed.
So in the callback function above, we look for either the token or the errors, and act accordingly. If you got back a token, then we update the form to include that token as a value in a hidden field, and submit the form to the Rails server. If there are errors, then we show them in an alert div.
If you’re using a different Stripe API, one that creates the form for your customer to fill in their details and doesn’t work the way I’ve sketched out above, then I don’t know where you would look to get that success token. But start looking into the response from the payment form, and see if it is sending you a unique token back. Then figure out how to store that on your database, associated with the item purchased and the person who made the order.
Walter
Hi Torsten, so you did like it?
The magnifying glass is a big show, but also very compilcated.
But you do books? Let’s talk about that.
~eike
Hey Eike, yeah I like the site and the magnifying glass. I do book covers. I mean I design book covers and I want to sell all the good ideas that got rejected as premades so really small site, max 500 premade covers at any time. Here are 2 examples that use the same stock image of the woman:
That looks good thank you - yes, customer goes offsite to pay. I think I am going to use jquery actually. Never going to learn javascript, I am too old.