Skip to content

Instantly share code, notes, and snippets.

@jbmyid
Created September 18, 2014 07:41
Show Gist options
  • Save jbmyid/01e7c89bc09efca3edd0 to your computer and use it in GitHub Desktop.
Save jbmyid/01e7c89bc09efca3edd0 to your computer and use it in GitHub Desktop.
module PayPal::SDK
module REST
module DataTypes
Payer.class_eval do
object_of :merchant_id, String
end
class ChargeModel < Base
object_of :id, String
object_of :type, String
object_of :amount, Currency
end
class PaymentDefination < Base
object_of :name, String
object_of :type, String #UNLIMITED or FIXED
object_of :frequency_interval, String
object_of :frequency, String # DAY, WEEK, MONTH or YEAR.
object_of :cycles, Integer
object_of :amount, Currency
object_of :id, String
array_of :charge_models, ChargeModel
end
class MerchantPreference < Base
object_of :id, String
object_of :cancel_url, String
object_of :return_url, String
object_of :setup_fee, Currency
object_of :max_fail_attempts, Integer
object_of :auto_bill_amount, Currency
object_of :initial_fail_amount_action, Currency
object_of :create_time, DateTime
object_of :update_time, DateTime
end
class BillingPlans < Base
object_of :total_count, Integer
array_of :BillingPlan, Invoice
end
class BillingPlan < Base
# {
# name: "Plan Name", type: "INFINITE", description: "Any description",
# payment_definitions: {name: "Name", type: "REGULAR", frequency_interval: 1, amount: {value: 2.3, currency: "USD"}, frequency: "MONTH"},
# merchant_preferences: {cancel_url: "http://google.com", return_url: "http://google.com", setup_fee: {value: 0, currency: "USD"}}
# }
object_of :id, String
object_of :name, String
object_of :description, String
object_of :type, String
array_of :payment_definitions, DataTypes::PaymentDefination
object_of :merchant_preferences, MerchantPreference
object_of :state, String
array_of :links, DataTypes::Links
object_of :payee, DataTypes::Payer
object_of :create_time, DateTime
object_of :update_time, DateTime
def self.load_members
object_of :name, String
object_of :description, String
end
# def self.all(options={})
# path = "vi/payments/billing-plans"
# BillingPlans.new(api.get(path, options, BillingPlan.new.http_header))
# end
include RequestDataType
def create
path = "v1/payments/billing-plans"
response = api.post(path, self.to_hash)
self.merge!(response)
success?
end
def self.find(resource_id)
raise ArgumentError.new("id required") if resource_id.to_s.strip.empty?
path = "v1/payments/billing-plans/#{resource_id}"
self.new(api.get(path))
end
def update(attrs)
path = "v1/payments/billing-plans/#{self.id}"
attrs = attrs.blank? ? [{path: "/", value: {state: "ACTIVE"}, op: "replace"}] : attrs
response = api.patch(path, attrs)
self.merge!(response)
success?
end
def activate!
update([{path: "/", value: {state: "ACTIVE"}, op: "replace"}])
end
end
class AgreementDetails < Base
object_of :outstanding_balance, Currency
object_of :cycles_remaining, Integer
object_of :cycles_completed, Integer
object_of :final_payment_date, DateTime
object_of :failed_payment_count, Integer
end
class AgreeMent < Base
object_of :id, String
object_of :name, String
object_of :description, String
object_of :start_date, DateTime
object_of :payer, Payer
object_of :plan, BillingPlan
array_of :links, DataTypes::Links
object_of :override_merchant_preferences, MerchantPreference
object_of :state, String
object_of :agreement_details, AgreementDetails
include RequestDataType
def create
path = "v1/payments/billing-agreements"
response = api.post(path, self.to_hash)
self.merge!(response)
success?
end
def approval_url
links.select{|link| link.rel=="approval_url"}[0].try :href
end
def execute_url
links.select{|link| link.rel=="execute"}[0].try :href
end
def execute!(token)
response = api.post("v1/payments/billing-agreements/#{token}/agreement-execute")
self.merge!(response)
success?
end
def suspend
path = "v1/payments/billing-agreements/#{id}/suspend"
response = api.post(path, {note: "Suspending the subscription"})
self.merge!(response)
success?
end
def reactivate
path = "v1/payments/billing-agreements/#{id}/re-activate"
response = api.post(path, {note: "Reactivating the agreement."})
self.merge!(response)
success?
end
def cancel
path = "v1/payments/billing-agreements/#{id}/cancel"
response = api.post(path, {note: "Cencelling the agreement."})
self.merge!(response)
success?
end
# def update_plan(plan_id)
# path = "v1/payments/billing-agreements/#{id}"
# response = api.patch(path, [{op: "replace", path: "/", value: {plan: {id: plan_id}}}])
# self.merge!(response)
# success?
# end
def self.find(resource_id)
raise ArgumentError.new("id required") if resource_id.to_s.strip.empty?
path = "v1/payments/billing-agreements/#{resource_id}"
self.new(api.get(path))
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment