-
Notifications
You must be signed in to change notification settings - Fork 95
/
coupons.rb
53 lines (40 loc) · 1.68 KB
/
coupons.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
$: << File.expand_path(File.dirname(__FILE__) + '/../lib')
require 'chargify_api_ares'
# You could load your credentials from a file...
chargify_config = YAML::load_file(File.join(File.dirname(__FILE__), '..', 'chargify.yml'))
Chargify.configure do |c|
c.subdomain = chargify_config['subdomain']
c.api_key = chargify_config['api_key']
end
# Create a new coupon
coupon = Chargify::Coupon.create(
:name => "66% off",
:code => "66OFF",
:description => "66% OFF for life",
:percentage => "66",
:recurring => false,
:product_family_id => 2
)
# Lookup up existing coupon using the product_family_id and the coupon code
coupon = Chargify::Coupon.find_by_product_family_id_and_code(2,"10OFF")
# Looking up all coupons for a product family
coupons = Chargify::Coupon.find_all_by_product_family_id(12345)
# Update coupon information
coupon = Chargify::Coupon.find_by_product_family_id_and_code(2,"10OFF")
coupon.code = "FOO"
coupon.save
# Archive coupon
coupon = Chargify::Coupon.find_by_product_family_id_and_code(2,"10OFF")
coupon.archive
# Inspect coupon usage
coupon = Chargify::Coupon.find_by_product_family_id_and_code(2,"10OFF")
coupon.usage
#>> [{"name"=>"Product 1", "savings"=>0.0, "id"=>8, "signups"=>5, "revenue"=>0.0}, {"name"=>"Product 2", "savings"=>0.0, "id"=>9, "signups"=>0, "revenue"=>0.0}]
# Apply coupon to subscription
subscription = Subscription.find_by_customer_reference('moklett')
subscription.add_coupon('5OFF')
# Remove coupon from subscription
subscription = Subscription.find_by_customer_reference('moklett')
subscription.remove_coupon
# Validate a coupon code (or subcode)
coupon = Chargify::Coupon.validate(coupon_code: "ABC123", product_family_id: 12345)