Class: Killbill::PaypalExpress::PrivatePaymentPlugin

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

Constant Summary collapse

ONE_HOUR_AGO =
3600
STATUS =
{:CAPTURE   => {:success_status => 'Completed', :type => 'Payment'},
:AUTHORIZE => {:success_status => 'Pending',   :type => 'Authorization'},
:REFUND    => {:success_status => 'Completed', :type => 'Refund'}}

Instance Method Summary collapse

Constructor Details

#initialize(session = {}) ⇒ PrivatePaymentPlugin

Returns a new instance of PrivatePaymentPlugin.



10
11
12
13
14
15
16
# File 'lib/paypal_express/private_api.rb', line 10

def initialize(session = {})
  super(:paypal_express,
        ::Killbill::PaypalExpress::PaypalExpressPaymentMethod,
        ::Killbill::PaypalExpress::PaypalExpressTransaction,
        ::Killbill::PaypalExpress::PaypalExpressResponse,
        session)
end

Instance Method Details

#fix_unknown_transaction(plugin_response, trx_plugin_info, gateway) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/paypal_express/private_api.rb', line 52

def fix_unknown_transaction(plugin_response, trx_plugin_info, gateway)
  status, transaction_id, type, amount, currency = search_transaction(trx_plugin_info.created_date - ONE_HOUR_AGO,
                                                                      gateway,
                                                                      trx_plugin_info.kb_transaction_payment_id)
  return false if status.blank? || transaction_id.blank? || type.blank?

  if type == STATUS[trx_plugin_info.transaction_type][:type] &&
     status == STATUS[trx_plugin_info.transaction_type][:success_status]
    plugin_response.transition_to_success transaction_id, amount, currency
    logger.info("Fixed UNDEFINED kb_transaction_id='#{trx_plugin_info.kb_transaction_payment_id}' to PROCESSED")
    return true
  end

  false
end

#get_external_keys_for_accounts(kb_account_ids, kb_tenant_id) ⇒ Object



47
48
49
50
# File 'lib/paypal_express/private_api.rb', line 47

def get_external_keys_for_accounts(, kb_tenant_id)
  context = kb_apis.create_context(kb_tenant_id)
  .map {|id| kb_apis..(id, context).external_key }
end

#initiate_express_checkout(kb_account_id, kb_tenant_id, amount_in_cents = 0, currency = 'USD', with_baid = true, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/paypal_express/private_api.rb', line 19

def initiate_express_checkout(, kb_tenant_id, amount_in_cents=0, currency='USD', with_baid=true, options = {})
  options[:currency]          ||= currency

  # Required arguments
  options[:return_url]        ||= 'http://www.example.com/success'
  options[:cancel_return_url] ||= 'http://www.example.com/sad_panda'

  if with_baid
    options[:billing_agreement]               ||= {}
    options[:billing_agreement][:type]        ||= options[:billing_agreement_type] || 'MerchantInitiatedBilling'
    options[:billing_agreement][:description] ||= options[:billing_agreement_description] || 'Kill Bill billing agreement'
  end

  # Go to Paypal (SetExpressCheckout call)
   = options[:payment_processor_account_id] || :default
  paypal_express_response      = gateway(, kb_tenant_id).setup_authorization(amount_in_cents, options)
  response, transaction        = save_response_and_transaction(paypal_express_response, :initiate_express_checkout, , kb_tenant_id, )

  response
end

#search_transaction(start_time, gateway, kb_payment_transaction_id) ⇒ Object



68
69
70
71
72
# File 'lib/paypal_express/private_api.rb', line 68

def search_transaction(start_time, gateway, kb_payment_transaction_id)
  options = {:start_date => start_time, :invoice_id => kb_payment_transaction_id}
  response = gateway.transaction_search options
  [response.params['status'], response.authorization, response.params['type'], response.params['gross_amount'], response.params['gross_amount_currency_id']]
end

#to_express_checkout_url(response, kb_tenant_id = nil, options = {}) ⇒ Object



40
41
42
43
44
45
# File 'lib/paypal_express/private_api.rb', line 40

def to_express_checkout_url(response, kb_tenant_id = nil, options = {})
   = options[:payment_processor_account_id] || :default
  gateway                      = gateway(, kb_tenant_id)
  review                       = ::Killbill::Plugin::ActiveMerchant::Utils.normalized(options, :review)
  gateway.redirect_url_for(response.token, :review => review)
end