0.01
No commit activity in last 3 years
No release in over 3 years
Adyen's Client Side Encryption (CSE) library for Ruby.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

Gem Version Build Status

Adyen CSE for Ruby

Adyen's Client Side Encryption (CSE) library for Ruby.

This is a port of Adyen's Android CSE library, packaged as a Ruby gem.

Check out the Python version here.

Installation

Add this line to your application's Gemfile:

gem 'adyen-cse-ruby'

And then execute:

$ bundle

Or install it yourself as:

$ gem install adyen-cse-ruby

Usage

require 'adyen_cse'

cse = AdyenCse::Encrypter.new(public_key)

encrypted_card = cse.encrypt!(
  holder_name: "Adyen Shopper",
  number: 4111_1111_1111_1111.to_s,
  expiry_month: "08",
  expiry_year: "2018",
  cvc: "737"
)

Optionally, you may pass a custom generation_time to mock expired data.

encrypted_card = cse.encrypt!(
  ...,
  generation_time: Time.new(2015, 10, 21)
)

factory_bot integration

Example of an :adyen_test_card factory with factory_bot.

# ./spec/factories/adyen_test_card.rb

require 'adyen_cse'

class AdyenTestCard
  attr_reader :holder_name, :number, :expiry_month, :expiry_year, :cvc

  PUBLIC_KEY = "your_public_key_here"

  def initialize(params = {})
    @holder_name  = params[:holder_name]
    @number       = params[:number]
    @expiry_month = params[:expiry_month]
    @expiry_year  = params[:expiry_year]
    @cvc          = params[:cvc]
  end

  def nonce
    AdyenCse::Encrypter.new(PUBLIC_KEY).encrypt!(
      holder_name: holder_name,
      number: number,
      expiry_month: expiry_month,
      expiry_year: expiry_year,
      cvc: cvc
    )
  end
end

FactoryBot.define do
  factory :adyen_test_card do
    sequence(:holder_name) { |n| "Shopper #{n}" }
    expiry_month { '08' }
    expiry_year { '2018' }
    cvc { '737' }

    visa

    trait :visa do
      number { 4111_1111_1111_1111.to_s }
    end

    trait :mastercard do
      number { 5555_5555_5555_4444.to_s }
    end

    trait :amex do
      number { 3451_779254_88348.to_s }
      cvc { '7373' }
    end

    skip_create
    initialize_with { new(attributes) }
  end
end

Example usage:

RSpec.describe ExamplePaymentService do
  let(:credit_card) { FactoryBot.build(:adyen_test_card) }
  ...
end