Getting started with Ruby on RailsRoutingActiveRecordViewsActiveRecord MigrationsRails Best PracticesNaming ConventionsActionCableActiveModelUser Authentication in RailsActiveRecord AssociationsActiveRecord ValidationsActiveRecord Query InterfaceActionMailerRails generate commandsConfigurationI18n - InternationalizationUsing GoogleMaps with RailsFile UploadsCachingActionControllerConfigurationSafe ConstantizeRails 5Authorization with CanCanMongoidGemsChange default timezoneAsset PipelineUpgrading RailsActiveRecord LockingDebuggingConfigure Angular with RailsRails loggerPrawn PDFRails APIDeploying a Rails app on HerokuActiveSupportForm HelpersActiveRecord TransactionsRSpec and Ruby on RailsDecorator patternElasticsearchReact with Rails using react-rails gemRails Cookbook - Advanced rails recipes/learnings and coding techniquesMultipurpose ActiveRecord columnsClass OrganizationShallow RoutingModel states: AASMRails 5 API AutheticationTesting Rails ApplicationsActive JobsRails frameworks over the yearsAdd Admin PanelNested form in Ruby on RailsFactory GirlImport whole CSV files from specific folderTools for Ruby on Rails code optimization and cleanupActiveJobActive Model SerializersRails Engine - Modular RailsSingle Table InheritanceActiveRecord TransactionsTurbolinksFriendly IDSecurely storing authentication keysAuthenticate Api using DeviseIntegrating React.js with Rails Using HyperloopChange a default Rails application enviornmentReserved WordsRails -EnginesAdding an Amazon RDS to your rails applicationPayment feature in railsRails on docker

Payment feature in rails

Other topics

Remarks:

Documentation.

Stripe

Braintree

How to integrate with Stripe

Add Stripe gem to our Gemfile

gem 'stripe'

Add initializers/stripe.rb file. This file contains the necessary keys for connecting with your stripe account.

require 'require_all'

Rails.configuration.stripe = {
    :publishable_key => ENV['STRIPE_PUBLISHABLE_KEY'],
    :secret_key      => ENV['STRIPE_SECRET_KEY']
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

How to create a new customer to Stripe

Stripe::Customer.create({email: email, source: payment_token})

This code creates a new customer on Stripe with given email address and source.

payment_token is the token given from the client-side that contains a payment method like a credit card or bank account. More info: Stripe.js client-side

How to retrieve a plan from Stripe

Stripe::Plan.retrieve(stripe_plan_id)

This code retrieves a plan from Stripe by its id.

How to create a subscription

When we have a customer and a plan we can create a new subscription on Stripe.

Stripe::Subscription.create(customer: customer.id, plan: plan.id)

It will create a new subscription and will charge our User. It's important to know what really happens on Stripe when we subscribe a user to a plan, you will find more info here: Stripe Subscription lifecycle.

How to charge a user with a single payment

Sometimes we want to charge our users just a single time, for do that we will do the next.

Stripe::Charge.create(amount:   amount, customer: customer, currency: currency)

In that case, we are charging our user one time for given amount.

Common errors:

  • The amount must be sent in integer form, that means, 2000 will be 20 units of currency. Check this example

  • You cannot charge a user in two currencies. If the user was charged in EUR at any moment in the past you cannot charge the user in USD.

  • You cannot charge user without source (payment method).

Contributors

Topic Id: 10929

Example Ids: 32710

This site is not affiliated with any of the contributors.