Class: Killbill::PaypalExpress::PaymentPlugin

Inherits:
Killbill::Plugin::ActiveMerchant::PaymentPlugin
  • Object
show all
Defined in:
lib/paypal_express/api.rb

Constant Summary collapse

UNKNOWN_TRX_CANCEL_PERIOD =
3600
PENDING_TRX_CANCEL_PERIOD =
(3*3600)
UNKNOWN_TRX_DELAY_FIX_PERIOD =
300

Instance Method Summary collapse

Constructor Details

#initializePaymentPlugin

Returns a new instance of PaymentPlugin.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/paypal_express/api.rb', line 9

def initialize
  gateway_builder = Proc.new do |config|
    ::ActiveMerchant::Billing::PaypalExpressGateway.application_id = config[:button_source] || 'killbill_SP'
    ::ActiveMerchant::Billing::PaypalExpressGateway.new :signature => config[:signature],
                                                        :login     => config[:login],
                                                        :password  => config[:password]
  end

  super(gateway_builder,
        :paypal_express,
        ::Killbill::PaypalExpress::PaypalExpressPaymentMethod,
        ::Killbill::PaypalExpress::PaypalExpressTransaction,
        ::Killbill::PaypalExpress::PaypalExpressResponse)

  @ip = ::Killbill::Plugin::ActiveMerchant::Utils.ip
  @private_api = ::Killbill::PaypalExpress::PrivatePaymentPlugin.new
end

Instance Method Details

#add_payment_method(kb_account_id, kb_payment_method_id, payment_method_props, set_default, properties, context) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/paypal_express/api.rb', line 125

def add_payment_method(, kb_payment_method_id, payment_method_props, set_default, properties, context)
  all_properties = (payment_method_props.nil? || payment_method_props.properties.nil? ? [] : payment_method_props.properties) + properties
  token = find_value_from_properties(all_properties, 'token')

  if token.nil?
    # HPP flow
    options = {
        :skip_gw => true
    }
  else
    # Go to Paypal to get the Payer id (GetExpressCheckoutDetails call)
     = find_value_from_properties(properties, :payment_processor_account_id)
     ||= find_payment_processor_id_from_initial_call(, context.tenant_id, token)
    payer_info = get_payer_info(token, , context.tenant_id, )
    options  = {
        :paypal_express_token         => token,
        :paypal_express_payer_id      => payer_info.payer_id,
        :payment_processor_account_id => 
    }
  end

  properties = merge_properties(properties, options)
  super(, kb_payment_method_id, payment_method_props, set_default, properties, context)
end

#authorize_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object



35
36
37
# File 'lib/paypal_express/api.rb', line 35

def authorize_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
  authorize_or_purchase_payment , kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, true
end

#build_form_descriptor(kb_account_id, descriptor_fields, properties, context) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/paypal_express/api.rb', line 190

def build_form_descriptor(, descriptor_fields, properties, context)
  jcontext = @kb_apis.create_context(context.tenant_id)

  all_properties = descriptor_fields + properties
  options = properties_to_hash(all_properties)

   = ::Killbill::Plugin::ActiveMerchant::Utils::LazyEvaluator.new { @kb_apis..(, jcontext) }
  amount = (options[:amount] || '0').to_f
  effective_date = options[:effective_date] || Time.now.utc
  currency = options[:currency] || .currency

  response = initiate_express_checkout(, amount, currency, all_properties, context)

  descriptor = super(, descriptor_fields, properties, context)
  descriptor.form_url = @private_api.to_express_checkout_url(response, context.tenant_id, options)
  descriptor.form_method = 'GET'
  descriptor.properties << build_property('token', response.token)

  # By default, pending payments are not created for HPP
  create_pending_payment = ::Killbill::Plugin::ActiveMerchant::Utils.normalized(options, :create_pending_payment)
  if create_pending_payment
     = ::Killbill::Plugin::ActiveMerchant::Utils.normalized(options, :payment_processor_account_id)
    token_expiration_period = ::Killbill::Plugin::ActiveMerchant::Utils.normalized(options, :token_expiration_period)
    custom_props = hash_to_properties(:from_hpp                     => true,
                                      :token                        => response.token,
                                      :payment_processor_account_id => ,
                                      :token_expiration_period      => token_expiration_period)
    payment_external_key = ::Killbill::Plugin::ActiveMerchant::Utils.normalized(options, :payment_external_key)
    transaction_external_key = ::Killbill::Plugin::ActiveMerchant::Utils.normalized(options, :transaction_external_key)

    kb_payment_method = (@kb_apis.payment_api.(, false, false, [], jcontext).find { |pm| pm.plugin_name == 'killbill-paypal-express' })

    auth_mode = ::Killbill::Plugin::ActiveMerchant::Utils.normalized(options, :auth_mode)
    # By default, the SALE mode is used.
    if auth_mode
      payment = @kb_apis.payment_api
                    .create_authorization(.send(:__instance_object__),
                                          kb_payment_method.id,
                                          nil,
                                          amount,
                                          currency,
                                          effective_date,
                                          payment_external_key,
                                          transaction_external_key,
                                          custom_props,
                                          jcontext)
    else
      payment = @kb_apis.payment_api
                    .create_purchase(.send(:__instance_object__),
                                     kb_payment_method.id,
                                     nil,
                                     amount,
                                     currency,
                                     effective_date,
                                     payment_external_key,
                                     transaction_external_key,
                                     custom_props,
                                     jcontext)
    end

    descriptor.properties << build_property('kb_payment_id', payment.id)
    descriptor.properties << build_property('kb_payment_external_key', payment.external_key)
    descriptor.properties << build_property('kb_transaction_id', payment.transactions.first.id)
    descriptor.properties << build_property('kb_transaction_external_key', payment.transactions.first.external_key)
  end

  descriptor
end

#cancel_pending_transactions(t_info_plugins, only_pending_trx) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/paypal_express/api.rb', line 103

def cancel_pending_transactions(t_info_plugins, only_pending_trx)
  return false unless only_pending_trx && token_expired(t_info_plugins.last)

  begin
    cancel_pending_transaction(t_info_plugins.last).nil?
    logger.info("Cancel pending kb_payment_id='#{t_info_plugins.last.kb_payment_id}', kb_payment_transaction_id='#{t_info_plugins.last.kb_transaction_payment_id}'")
    return true
  rescue => e
    logger.warn("Unexpected exception while canceling pending kb_payment_id='#{t_info_plugins.last.kb_payment_id}', kb_payment_transaction_id='#{t_info_plugins.last.kb_transaction_payment_id}': #{e.message}\n#{e.backtrace.join("\n")}")
  end

  false
end

#capture_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/paypal_express/api.rb', line 39

def capture_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
  # Pass extra parameters for the gateway here
  options = {
      # NotComplete to allow for partial captures.
      # If Complete, any remaining amount of the original authorized transaction is automatically voided and all remaining open authorizations are voided.
      :complete_type => 'NotComplete'
  }

  properties = merge_properties(properties, options)
  super(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
end

#credit_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/paypal_express/api.rb', line 67

def credit_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
end

#delete_payment_method(kb_account_id, kb_payment_method_id, properties, context) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/paypal_express/api.rb', line 150

def delete_payment_method(, kb_payment_method_id, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(, kb_payment_method_id, properties, context)
end

#get_payment_info(kb_account_id, kb_payment_id, properties, context) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/paypal_express/api.rb', line 88

def get_payment_info(, kb_payment_id, properties, context)
  filtered_plugin_info, plugin_info, with_only_pending_trx = get_raw_payment_info(kb_payment_id, context)

  return filtered_plugin_info if filtered_plugin_info.empty?

  options = properties_to_hash(properties)
  # We won't be in a state where we have both a pending and unknown plugin infos; so we just return here.
  if fix_unknown_transactions(kb_payment_id, plugin_info, options, , context) ||
     cancel_pending_transactions(filtered_plugin_info, with_only_pending_trx)
    return get_raw_payment_info(kb_payment_id, context)[0]
  end

  filtered_plugin_info
end

#get_payment_method_detail(kb_account_id, kb_payment_method_id, properties, context) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/paypal_express/api.rb', line 158

def get_payment_method_detail(, kb_payment_method_id, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(, kb_payment_method_id, properties, context)
end

#get_payment_methods(kb_account_id, refresh_from_gateway, properties, context) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/paypal_express/api.rb', line 170

def get_payment_methods(, refresh_from_gateway, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(, refresh_from_gateway, properties, context)
end

#on_event(event) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/paypal_express/api.rb', line 27

def on_event(event)
  # Require to deal with per tenant configuration invalidation
  super(event)
  #
  # Custom event logic could be added below...
  #
end

#process_notification(notification, properties, context) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/paypal_express/api.rb', line 259

def process_notification(notification, properties, context)
  # Pass extra parameters for the gateway here
  options    = {}
  properties = merge_properties(properties, options)

  super(notification, properties, context) do |gw_notification, service|
    # Retrieve the payment
    # gw_notification.kb_payment_id =
    #
    # Set the response body
    # gw_notification.entity =
  end
end

#purchase_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object



51
52
53
54
# File 'lib/paypal_express/api.rb', line 51

def purchase_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
  # by default, this call will purchase a payment
  authorize_or_purchase_payment , kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context
end

#refund_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/paypal_express/api.rb', line 75

def refund_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
  # Cannot refund based on authorizations (default behavior)
  linked_transaction_type = @transaction_model.purchases_from_kb_payment_id(kb_payment_id, context.tenant_id).size > 0 ? :PURCHASE : :CAPTURE

  # Pass extra parameters for the gateway here
  options = {
      :linked_transaction_type => linked_transaction_type
  }

  properties = merge_properties(properties, options)
  super(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
end

#reset_payment_methods(kb_account_id, payment_methods, properties, context) ⇒ Object



186
187
188
# File 'lib/paypal_express/api.rb', line 186

def reset_payment_methods(, payment_methods, properties, context)
  super
end

#search_payment_methods(search_key, offset, limit, properties, context) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/paypal_express/api.rb', line 178

def search_payment_methods(search_key, offset, limit, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(search_key, offset, limit, properties, context)
end

#search_payments(search_key, offset, limit, properties, context) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/paypal_express/api.rb', line 117

def search_payments(search_key, offset, limit, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(search_key, offset, limit, properties, context)
end

#set_default_payment_method(kb_account_id, kb_payment_method_id, properties, context) ⇒ Object



166
167
168
# File 'lib/paypal_express/api.rb', line 166

def set_default_payment_method(, kb_payment_method_id, properties, context)
  # TODO
end

#void_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, properties, context) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/paypal_express/api.rb', line 56

def void_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, properties, context)
  # Pass extra parameters for the gateway here
  options = {
      # Void the original authorization
      :linked_transaction_type => :authorize
  }

  properties = merge_properties(properties, options)
  super(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, properties, context)
end