Module: Hippo::Payments

Extended by:
Payments
Included in:
Payments
Defined in:
lib/hippo/payments.rb

Instance Method Summary collapse

Instance Method Details

#cancel_subscription(tenant) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/hippo/payments.rb', line 105

def cancel_subscription(tenant)
    result = gateway.subscription.cancel(
        tenant.['bt_plan_id']
    )
    if result.success?
        tenant..delete('bt_plan_id')
        tenant.subscription = nil
        return true
    else
        tenant.subscription.errors.add(:base, result.errors.map(&:message).first)
        return false
    end
end

#create_customer_for_tenant(tenant) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hippo/payments.rb', line 53

def create_customer_for_tenant(tenant)
    return unless gateway.present?
    result = gateway.customer.create(
        company: tenant.name,
        email: tenant.email,
        phone: tenant.phone_number
    )
    if result.success?
        tenant.['bt_customer_id'] = result.customer.id
    end
end

#gatewayObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hippo/payments.rb', line 12

def gateway
    cc = gateway_config
    return nil unless cc.present?
    ::Braintree::Gateway.new(
        ::Braintree::Configuration.new(
            environment: cc['sandbox'] ? :sandbox : :production,
            merchant_id: cc['merchant_id'],
            public_key:  cc['public_key'],
            private_key: cc['private_key'],
            logger:      Hippo.logger
        )
    )
end

#gateway_configObject



8
9
10
# File 'lib/hippo/payments.rb', line 8

def gateway_config
    Hippo.config.secrets.dig(:payments, :braintree) || {}
end

#payment_authorizationObject



26
27
28
29
# File 'lib/hippo/payments.rb', line 26

def payment_authorization
    gw = ::Braintree::ClientTokenGateway.new(gateway)
    gw.generate
end

#set_tenant_payment_method(tenant, subscription, nonce) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hippo/payments.rb', line 65

def set_tenant_payment_method(tenant, subscription, nonce)
    result = if tenant.['payment_method_id']
                 gateway.payment_method.update(
                     tenant.['payment_method_id'],
                     payment_method_nonce: nonce)
             else
                 gateway.payment_method.create(
                     customer_id: tenant.['bt_customer_id'],
                     payment_method_nonce: nonce,
                     options: { make_default: true })
             end

    if result.success?
        tenant.['payment_method_id'] = result.payment_method.token
    else
        subscription.errors.add(:base, result.errors.map(&:message).first)
        return false
    end

    attrs = { plan_id: subscription.subscription_id,
              payment_method_token: tenant.['payment_method_id'] }

    result = if tenant.['bt_plan_id']
                 gateway.subscription.update(
                     tenant.['bt_plan_id'],
                     attrs
                 )
             else
                 gateway.subscription.create(attrs)
             end

    if result.success?
        tenant.['bt_plan_id'] = result.subscription.id
        return true
    else
        subscription.errors.add(:base, result.errors.map(&:message).first)
        return false
    end
end

#sync_plansObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hippo/payments.rb', line 31

def sync_plans
    ::Braintree::PlanGateway.new(gateway).all.each do |bt|
        subscription = Hippo::Subscription.find_or_initialize_by(subscription_id: bt.id)
        subscription.update_attributes(
            name: bt.name, description: bt.description,
            price: bt.price, trial_duration: bt.trial_duration
        )
        subscription.save!
    end
    true
end

#update_customer_for_tenant(tenant) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/hippo/payments.rb', line 43

def update_customer_for_tenant(tenant)
    return unless gateway.present?
    gateway.customer.update(
        tenant.['bt_customer_id'],
        company: tenant.name,
        email: tenant.email,
        phone: tenant.phone_number
    )
end