Ahmed Reza Siddique
Ahmed Reza Siddique

Reputation: 423

Simple coupon generator in rails

I have working on this from quite a long time but didn't get anything useful, all i want to know is how to add a simple random coupon generator. I have a rails app where users can check offers of restaurant, salon etc now i want to add a system so that users can generate a coupon and show that coupon to avail offers.

Upvotes: 1

Views: 843

Answers (2)

Lukasz Wiktor
Lukasz Wiktor

Reputation: 20422

If you don't mind building software from cloud services then you can try to use Voucherify. They provide a library for Ruby: https://github.com/rspective/voucherify-ruby-sdk

require "voucherify"

voucherify = Voucherify.new({
  "applicationId" => "YOUR-APPLICATION-ID",
  "clientSecretKey" => "YOUR-CLIENT-SECRET-KEY"
})

code = nil # for an automatically generated string

# single-use voucher with 10% off discount that is valid throughout the whole 2016
voucher = {
  category: "Restaurant",
  discount: {
    percent_off: 10.0,
    type: "PERCENT"
  },
  start_date: "2016-01-01T00:00:00Z",
  expiration_date: "2016-12-31T23:59:59Z",
  redemption: {
    quantity: 1
  }
}

voucherify.create(code, voucher)

Full disclosure: I'm a developer of Voucherify.

Upvotes: 1

Ben Hawker
Ben Hawker

Reputation: 949

You don't mention what sort of coupon format you require and I am sure there are a bunch of gems that do similar things. I guess one approach is to use a unique code that you can generate and then tag a user_id to the end of it to ensure uniqueness across many codes.

def generate_coupon_code(user_id)
  characters = %w(A B C D E F G H J K L M P Q R T W X Y Z 1 2 3 4 5 6 7 8 9)
  code = ''

  4.times { code << characters.sample }
  code << user_id.to_s

  code
end

Upvotes: 1

Related Questions