Skip to content

Commit

Permalink
Integrate using the Braintree GraphQL API instead of the ruby SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
braintreeps committed Dec 2, 2019
1 parent 94a44fb commit ff0760d
Show file tree
Hide file tree
Showing 25 changed files with 552 additions and 302 deletions.
2 changes: 1 addition & 1 deletion .ruby-gemset
Original file line number Diff line number Diff line change
@@ -1 +1 @@
braintree_rails_example
braintree_graphql_rails_example
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM ruby:2.3.1-onbuild

RUN apt-get update && apt-get install -y build-essential nodejs

ENV APP_HOME /braintree_rails_example
ENV APP_HOME /braintree_graphql_rails_example
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

gem 'braintree', '~> 2.87'

gem 'dotenv', '~> 2.0'

gem 'httparty', '~> 0.16.2'

group :development, :test do
# Use sqlite in development and test for ease of setup
gem 'sqlite3'
Expand Down
10 changes: 6 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ GEM
debug_inspector (>= 0.0.1)
bootsnap (1.3.2)
msgpack (~> 1.0)
braintree (2.94.0)
builder (>= 2.0.0)
builder (3.2.3)
byebug (10.0.2)
coffee-rails (4.2.2)
Expand All @@ -68,7 +66,10 @@ GEM
ffi (1.9.25)
globalid (0.4.2)
activesupport (>= 4.2.0)
i18n (1.6.0)
httparty (0.16.2)
concurrent-ruby (~> 1.0)
multi_xml (>= 0.5.2)
i18n (1.7.0)
concurrent-ruby (~> 1.0)
jbuilder (2.8.0)
activesupport (>= 4.2.0)
Expand Down Expand Up @@ -96,6 +97,7 @@ GEM
minitest (5.11.3)
msgpack (1.2.10)
multi_json (1.13.1)
multi_xml (0.6.0)
nio4r (2.3.1)
nokogiri (1.10.5)
mini_portile2 (~> 2.4.0)
Expand Down Expand Up @@ -207,10 +209,10 @@ PLATFORMS

DEPENDENCIES
bootsnap (= 1.3.2)
braintree (~> 2.87)
byebug
coffee-rails (~> 4.2.2)
dotenv (~> 2.0)
httparty (~> 0.16.2)
jbuilder (~> 2.0)
jquery-rails
listen (~> 3.1, >= 3.1.5)
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Braintree Rails Example
# Braintree GraphQL Example in Rails

[![Build Status](https://travis-ci.org/braintree/braintree_rails_example.svg?branch=master)](https://travis-ci.org/braintree/braintree_rails_example)
[![Build Status](https://travis-ci.org/braintree/braintree_graphql_rails_example.svg?branch=master)](https://travis-ci.org/braintree/braintree_graphql_rails_example)

An example Braintree integration for Ruby on Rails.
An example Braintree integration with the [GraphQL API](https://graphql.braintreepayments.com/) using Ruby on Rails. Forked from [braintree/braintree_rails_example](https://github.com/braintree/braintree_rails_example).

## Setup Instructions

Expand Down Expand Up @@ -30,7 +30,7 @@ An example Braintree integration for Ruby on Rails.

You can deploy this app directly to Heroku to see the app live. Skip the setup instructions above and click the button below. This will walk you through getting this app up and running on Heroku in minutes.

[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/braintree/braintree_rails_example&env[BT_ENVIRONMENT]=sandbox)
[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/braintree/braintree_graphql_rails_example&env[BT_VERSION]=2019-11-11)

## Running Tests

Expand All @@ -57,7 +57,7 @@ Sandbox transactions must be made with [sample credit card numbers](https://deve
## Help
* Found a bug? Have a suggestion for improvement? Want to tell us we're awesome? [Submit an issue](https://github.com/braintree/braintree_rails_example/issues)
* Found a bug? Have a suggestion for improvement? Want to tell us we're awesome? [Submit an issue](https://github.com/braintree/braintree_graphql_rails_example/issues)
* Trouble with your integration? Contact [Braintree Support](https://support.braintreepayments.com/) / [email protected]
* Want to contribute? [Submit a pull request](https://help.github.com/articles/creating-a-pull-request)
Expand Down
21 changes: 8 additions & 13 deletions app.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
{
"name": "Braintree Rails Example",
"description": "An example Braintree integration for Ruby on Rails",
"keywords": ["braintree", "ruby", "rails"],
"name": "Braintree GraphQL Example in Rails",
"description": "An example Braintree integration with the GraphQL API using Ruby on Rails",
"keywords": ["braintree", "graphql", "ruby", "rails"],
"website": "https://www.braintreepayments.com",
"repository": "https://github.com/braintree/braintree_rails_example",
"repository": "https://github.com/braintree/braintree_graphql_rails_example",
"logo": "https://avatars1.githubusercontent.com/u/3453",
"success_url": "/",
"env": {
"BT_ENVIRONMENT": {
"description": "Run against Braintree sandbox environment",
"required": true,
"value": "sandbox"
},
"BT_MERCHANT_ID": {
"description": "Your Braintree Merchant ID",
"required": true
},
"BT_PUBLIC_KEY": {
"description": "Your Braintree Public Key",
"required": true
},
"BT_PRIVATE_KEY": {
"description": "Your Braintree Private Key",
"required": true
},
"BT_VERSION": {
"description": "A date in the format YYYY-MM-DD (we recommend using the date you started using the GraphQL API)",
"required": true
}
}
}
8 changes: 4 additions & 4 deletions app/assets/stylesheets/app.css.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2152,10 +2152,10 @@ td {
word-wrap: break-word;
-ms-word-break: break-all;
word-break: break-word;
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;
-ms-hyphens: none;
-moz-hyphens: none;
-webkit-hyphens: none;
hyphens: none;
line-height: 1.3em;
font-size: 14px;
letter-spacing: .02em;
Expand Down
69 changes: 36 additions & 33 deletions app/controllers/checkouts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
class CheckoutsController < ApplicationController
TRANSACTION_SUCCESS_STATUSES = [
Braintree::Transaction::Status::Authorizing,
Braintree::Transaction::Status::Authorized,
Braintree::Transaction::Status::Settled,
Braintree::Transaction::Status::SettlementConfirmed,
Braintree::Transaction::Status::SettlementPending,
Braintree::Transaction::Status::Settling,
Braintree::Transaction::Status::SubmittedForSettlement,
"AUTHORIZED",
"AUTHORIZING",
"SETTLED",
"SETTLEMENT_PENDING",
"SETTLING",
"SUBMITTED_FOR_SETTLEMENT",
]

def new
@client_token = gateway.client_token.generate
@client_token = gateway.client_token.dig("data", "createClientToken", "clientToken")
end

def show
@transaction = gateway.transaction.find(params[:id])
@result = _create_result_hash(@transaction)
begin
@transaction = gateway.node_fetch_transaction(params[:id]).fetch("data", {})["transaction"]
@result = _create_status_result_hash(@transaction)
rescue BraintreeGateway::GraphQLError => error
_flash_errors(error)
redirect_to new_checkout_path
end
end

def create
amount = params["amount"] # In production you should not take amounts directly from clients
nonce = params["payment_method_nonce"]

result = gateway.transaction.sale(
amount: amount,
payment_method_nonce: nonce,
:options => {
:submit_for_settlement => true
}
)
begin
result = gateway.transaction(nonce, amount)
id = result.dig("data", "chargePaymentMethod", "transaction", "id")

if result.success? || result.transaction
redirect_to checkout_path(result.transaction.id)
else
error_messages = result.errors.map { |error| "Error: #{error.code}: #{error.message}" }
flash[:error] = error_messages
if id
redirect_to checkout_path(id)
else
raise BraintreeGateway::GraphQLError.new(result)
end
rescue BraintreeGateway::GraphQLError => error
_flash_errors(error)
redirect_to new_checkout_path
end
end

def _create_result_hash(transaction)
status = transaction.status
def _create_status_result_hash(transaction)
status = transaction["status"]

if TRANSACTION_SUCCESS_STATUSES.include? status
result_hash = {
Expand All @@ -50,21 +52,22 @@ def _create_result_hash(transaction)
}
else
result_hash = {
:header => "Transaction Failed",
:header => "Transaction Unsuccessful",
:icon => "fail",
:message => "Your test transaction has a status of #{status}. See the Braintree API response and try again."
}
end
end

def gateway
env = ENV["BT_ENVIRONMENT"]
def _flash_errors(error)
if error.messages != nil and !error.messages.empty?
flash[:error] = error.messages
else
flash[:error] = ["Error: Something unexpected went wrong! Try again."]
end
end

@gateway ||= Braintree::Gateway.new(
:environment => env && env.to_sym,
:merchant_id => ENV["BT_MERCHANT_ID"],
:public_key => ENV["BT_PUBLIC_KEY"],
:private_key => ENV["BT_PRIVATE_KEY"],
)
def gateway
@gateway ||= BraintreeGateway.new(HTTParty)
end
end
Loading

0 comments on commit ff0760d

Please sign in to comment.