Class: EasyPost::Beta::Referral

Inherits:
Resource show all
Defined in:
lib/easypost/beta/referral.rb

Overview

Referral objects are User objects created from a Partner user.

Instance Attribute Summary

Attributes inherited from EasyPostObject

#api_key, #name, #parent, #unsaved_values

Class Method Summary collapse

Methods inherited from Resource

class_name, #delete, each, #refresh, retrieve, #save, url, #url

Methods inherited from EasyPostObject

#[], #[]=, #as_json, construct_from, #deconstruct_keys, #each, #id, #id=, #initialize, #inspect, #keys, #refresh_from, #to_hash, #to_json, #to_s, #values

Constructor Details

This class inherits a constructor from EasyPost::EasyPostObject

Class Method Details

.add_credit_card(referral_api_key, number, expiration_month, expiration_year, cvc, priority = 'primary') ⇒ Object

Add credit card to a referral user. This function requires the Referral User’s API key.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/easypost/beta/referral.rb', line 31

def self.add_credit_card(referral_api_key, number, expiration_month, expiration_year, cvc, priority = 'primary')
  easypost_stripe_api_key = retrieve_easypost_stripe_api_key

  begin
    stripe_credit_card_token = create_stripe_token(
      number,
      expiration_month,
      expiration_year,
      cvc,
      easypost_stripe_api_key,
    )
  rescue StandardError
    raise EasyPost::Error.new('Could not send card details to Stripe, please try again later.')
  end

  response = create_easypost_credit_card(referral_api_key, stripe_credit_card_token, priority)
  EasyPost::Util.convert_to_easypost_object(response, referral_api_key)
end

.all(params = {}, api_key = nil) ⇒ Object

Retrieve a list of referral users. This function requires the Partner User’s API key.



25
26
27
28
# File 'lib/easypost/beta/referral.rb', line 25

def self.all(params = {}, api_key = nil)
  response = EasyPost.make_request(:get, '/beta/referral_customers', api_key, params)
  EasyPost::Util.convert_to_easypost_object(response, api_key)
end

.create(params = {}, api_key = nil) ⇒ Object

Create a referral user. This function requires the Partner User’s API key.



6
7
8
9
# File 'lib/easypost/beta/referral.rb', line 6

def self.create(params = {}, api_key = nil)
  response = EasyPost.make_request(:post, '/beta/referral_customers', api_key, { user: params })
  EasyPost::Util.convert_to_easypost_object(response, api_key)
end

.create_easypost_credit_card(referral_api_key, stripe_object_id, priority = 'primary') ⇒ Object

Submit Stripe credit card token to EasyPost.



87
88
89
90
91
92
93
94
95
96
# File 'lib/easypost/beta/referral.rb', line 87

def self.create_easypost_credit_card(referral_api_key, stripe_object_id, priority = 'primary')
  wrapped_params = {
    credit_card: {
      stripe_object_id: stripe_object_id,
      priority: priority,
    },
  }
  response = EasyPost.make_request(:post, '/beta/credit_cards', referral_api_key, wrapped_params)
  EasyPost::Util.convert_to_easypost_object(response, referral_api_key)
end

.create_stripe_token(number, expiration_month, expiration_year, cvc, easypost_stripe_token) ⇒ Object

Get credit card token from Stripe.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/easypost/beta/referral.rb', line 57

def self.create_stripe_token(number, expiration_month, expiration_year, cvc, easypost_stripe_token)
  headers = {
    # This Stripe endpoint only accepts URL form encoded bodies.
    Authorization: "Bearer #{easypost_stripe_token}",
    'Content-type': 'application/x-www-form-urlencoded',
  }

  credit_card_hash = {
    card: {
      number: number,
      exp_month: expiration_month,
      exp_year: expiration_year,
      cvc: cvc,
    },
  }

  form_encoded_params = EasyPost::Util.form_encode_params(credit_card_hash)

  uri = URI.parse('https://api.stripe.com/v1/tokens')
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri.request_uri, headers)
  query = URI.encode_www_form(form_encoded_params)

  response = http.request(request, query)
  response_json = JSON.parse(response.body)
  response_json['id']
end

.retrieve_easypost_stripe_api_keyObject

Retrieve EasyPost’s Stripe public API key.



51
52
53
54
# File 'lib/easypost/beta/referral.rb', line 51

def self.retrieve_easypost_stripe_api_key
  response = EasyPost.make_request(:get, '/beta/partners/stripe_public_key', @api_key)
  response['public_key']
end

.update_email(email, user_id, api_key = nil) ⇒ Object

Update a referral user. This function requires the Partner User’s API key.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/easypost/beta/referral.rb', line 12

def self.update_email(email, user_id, api_key = nil)
  wrapped_params = {
    user: {
      email: email,
    },
  }
  EasyPost.make_request(:put, "/beta/referral_customers/#{user_id}", api_key, wrapped_params)

  # return true if API succeeds, else an error is throw if it fails.
  true
end