Skip to content
This repository has been archived by the owner on Apr 19, 2022. It is now read-only.

Commit

Permalink
Add Ruby Sinatra server (#54)
Browse files Browse the repository at this point in the history
* Adding a ruby backend

* Updating README

* Fix shipping_change endpoint.

* Fixing nits, README and removing API Version hack

* Load skus separately.

* Update main README.
  • Loading branch information
mikeshaw-stripe authored and thorsten-stripe committed Apr 3, 2019
1 parent a5ce738 commit 2d0df99
Show file tree
Hide file tree
Showing 8 changed files with 511 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ The [Sources API](https://stripe.com/docs/sources) provides a single integration

## Getting Started with Node

There are a couple server implementations in the [`server`](/server) directory. Instructions for running the Node.js server in [`server/node`](/server/node) are below, but if you’re more comfortable with Python you can find a README explaining how to run a Flask server in the [`server/python`](/server/python) directory. Both servers have the same endpoints to handle requests from the frontend and interact with the [Stripe libraries](https://stripe.com/docs/libraries).
Instructions for running the Node.js server in [`server/node`](/server/node) are below. You can find alternative server implementations in the [`server`](/server) directory:

- Node, Express: [`server/node`](/server/node)
- Python, Flask: [`server/python`](/server/python)
- Ruby, Sinatra: [`server/ruby`](/server/ruby)

All servers have the same endpoints to handle requests from the frontend and interact with the [Stripe libraries](https://stripe.com/docs/libraries).

### Requirements

Expand Down
19 changes: 18 additions & 1 deletion public/javascripts/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,30 @@ class Store {
}
}

// Retrieve a SKU for the Product where the API Version is newer and doesn't include them on v1/product
async loadSkus(product_id) {
try {
const response = await fetch(`/product/${product_id}/skus`);
const skus = await response.json();
this.products[product_id].skus = skus;
} catch (err) {
return {error: err.message};
}
}

// Load the product details.
loadProducts() {
if (!this.productsFetchPromise) {
this.productsFetchPromise = new Promise(async resolve => {
const productsResponse = await fetch('/products');
const products = (await productsResponse.json()).data;
products.forEach(product => (this.products[product.id] = product));
// Check if we have SKUs on the product, otherwise load them separately.
for (const product of products) {
this.products[product.id] = product;
if (!product.skus) {
await this.loadSkus(product.id);
}
}
resolve();
});
}
Expand Down
8 changes: 8 additions & 0 deletions server/ruby/GemFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source 'https://rubygems.org/'

gem 'sinatra'
gem 'sinatra-reloader'
gem 'stripe'
gem 'dotenv'
gem 'json'
gem 'ruby-debug-ide'
54 changes: 54 additions & 0 deletions server/ruby/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
GEM
remote: https://rubygems.org/
specs:
backports (3.12.0)
connection_pool (2.2.2)
dotenv (2.7.1)
faraday (0.15.4)
multipart-post (>= 1.2, < 3)
json (2.2.0)
multi_json (1.13.1)
multipart-post (2.0.0)
mustermann (1.0.3)
net-http-persistent (3.0.0)
connection_pool (~> 2.2)
rack (2.0.6)
rack-protection (2.0.5)
rack
rake (12.3.2)
ruby-debug-ide (0.6.1)
rake (>= 0.8.1)
sinatra (2.0.5)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.5)
tilt (~> 2.0)
sinatra-contrib (2.0.5)
backports (>= 2.8.2)
multi_json
mustermann (~> 1.0)
rack-protection (= 2.0.5)
sinatra (= 2.0.5)
tilt (>= 1.3, < 3)
sinatra-reloader (1.0)
sinatra-contrib
sorbet (0.0.1.pre.prealpha)
stripe (4.9.0)
faraday (~> 0.13)
net-http-persistent (~> 3.0)
tilt (2.0.9)

PLATFORMS
ruby

DEPENDENCIES
dotenv
json
ruby-debug-ide
sinatra
sinatra-reloader
sorbet (~> 0.0.1.pre.prealpha)
stripe

BUNDLED WITH
2.0.1
64 changes: 64 additions & 0 deletions server/ruby/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Stripe Payments Demo - Ruby Server

This demo uses a simple [Sinatra](http://sinatrarb.com/) application as the server.

## Payments Integration

- [`app.rb`](app.rb) contains the routes that interface with Stripe to create charges and receive webhook events.
- [`setup.rb`](setup.rb) a simple setup script to make some fake Products and SKUs for our Stripe store.
- [`inventory.rb`](inventory.rb) a minimal wrapper over the Stripe Python SDK that handles creating/fetching products and caluclating payment amounts from SKUs. You can override this class with your own product and order management system code.

## Requirements

You’ll new the following:

- [Ruby 2.X](https://www.ruby-lang.org/en/downloads/)
- Modern browser that supports ES6 (Chrome to see the Payment Request, and Safari to see Apple Pay).
- Stripe account to accept payments ([sign up](https://dashboard.stripe.com/register) for free!)

## Getting Started

Before getting started check that you have ruby installed

```
ruby --version
```

Copy the example environment variables file `.env.example` from the root of the repo into your own environment file called `.env`:

```
cp .env.example .env
```

User `bundler` to install the required gems by navigating to ./server/ruby and running:

```
bundle install
```

Run the Sinatra application

```
bundle exec ruby app.rb
```

You should now see it running on [`http://localhost:4567/`](http://localhost:4567/)

### Testing Webhooks

If you want to test [receiving webhooks](https://stripe.com/docs/webhooks), we recommend using ngrok to expose your local server.

First [download ngrok](https://ngrok.com) and start your Sinatra application.

[Run ngrok](https://ngrok.com/docs). Assuming your Sinatra application is running on the default port 4567, you can simply run ngrok in your Terminal in the directory where you downloaded ngrok:

```
ngrok http 4567
```

ngrok will display a UI in your terminal telling you the new forwarding address for your Sinatra app. Use this URL as the URL to be called in your developer [webhooks panel.](https://dashboard.stripe.com/account/webhooks)

Don't forget to append `/webhook` when you set up your Stripe webhook URL in the Dashboard. Example URL to be called: `https://75795038.ngrok.io/webhook`.

## Credits
- Code: [Mike Shaw](https://www.linkedin.com/in/mandshaw/)
Loading

0 comments on commit 2d0df99

Please sign in to comment.