Class: Killbill::PaypalExpress::PaymentPlugin
- Inherits:
-
Killbill::Plugin::ActiveMerchant::PaymentPlugin
- Object
- Killbill::Plugin::ActiveMerchant::PaymentPlugin
- Killbill::PaypalExpress::PaymentPlugin
- 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
- #add_payment_method(kb_account_id, kb_payment_method_id, payment_method_props, set_default, properties, context) ⇒ Object
- #authorize_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object
- #build_form_descriptor(kb_account_id, descriptor_fields, properties, context) ⇒ Object
- #cancel_pending_transactions(t_info_plugins, only_pending_trx) ⇒ Object
- #capture_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object
- #credit_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object
- #delete_payment_method(kb_account_id, kb_payment_method_id, properties, context) ⇒ Object
- #get_payment_info(kb_account_id, kb_payment_id, properties, context) ⇒ Object
- #get_payment_method_detail(kb_account_id, kb_payment_method_id, properties, context) ⇒ Object
- #get_payment_methods(kb_account_id, refresh_from_gateway, properties, context) ⇒ Object
-
#initialize ⇒ PaymentPlugin
constructor
A new instance of PaymentPlugin.
- #on_event(event) ⇒ Object
- #process_notification(notification, properties, context) ⇒ Object
- #purchase_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object
- #refund_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object
- #reset_payment_methods(kb_account_id, payment_methods, properties, context) ⇒ Object
- #search_payment_methods(search_key, offset, limit, properties, context) ⇒ Object
- #search_payments(search_key, offset, limit, properties, context) ⇒ Object
- #set_default_payment_method(kb_account_id, kb_payment_method_id, properties, context) ⇒ Object
- #void_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, properties, context) ⇒ Object
Constructor Details
#initialize ⇒ PaymentPlugin
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_account_id, 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 = { :skip_gw => true } else # Go to Paypal to get the Payer id (GetExpressCheckoutDetails call) payment_processor_account_id = find_value_from_properties(properties, :payment_processor_account_id) payment_processor_account_id ||= find_payment_processor_id_from_initial_call(kb_account_id, context.tenant_id, token) payer_info = get_payer_info(token, kb_account_id, context.tenant_id, payment_processor_account_id) = { :paypal_express_token => token, :paypal_express_payer_id => payer_info.payer_id, :payment_processor_account_id => payment_processor_account_id } end properties = merge_properties(properties, ) super(kb_account_id, 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 (kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) kb_account_id, 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(kb_account_id, descriptor_fields, properties, context) jcontext = @kb_apis.create_context(context.tenant_id) all_properties = descriptor_fields + properties = properties_to_hash(all_properties) kb_account = ::Killbill::Plugin::ActiveMerchant::Utils::LazyEvaluator.new { @kb_apis.account_user_api.get_account_by_id(kb_account_id, jcontext) } amount = ([:amount] || '0').to_f effective_date = [:effective_date] || Time.now.utc currency = [:currency] || kb_account.currency response = initiate_express_checkout(kb_account_id, amount, currency, all_properties, context) descriptor = super(kb_account_id, descriptor_fields, properties, context) descriptor.form_url = @private_api.to_express_checkout_url(response, context.tenant_id, ) 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(, :create_pending_payment) if create_pending_payment payment_processor_account_id = ::Killbill::Plugin::ActiveMerchant::Utils.normalized(, :payment_processor_account_id) token_expiration_period = ::Killbill::Plugin::ActiveMerchant::Utils.normalized(, :token_expiration_period) custom_props = hash_to_properties(:from_hpp => true, :token => response.token, :payment_processor_account_id => payment_processor_account_id, :token_expiration_period => token_expiration_period) payment_external_key = ::Killbill::Plugin::ActiveMerchant::Utils.normalized(, :payment_external_key) transaction_external_key = ::Killbill::Plugin::ActiveMerchant::Utils.normalized(, :transaction_external_key) kb_payment_method = (@kb_apis.payment_api.get_account_payment_methods(kb_account_id, false, false, [], jcontext).find { |pm| pm.plugin_name == 'killbill-paypal-express' }) auth_mode = ::Killbill::Plugin::ActiveMerchant::Utils.normalized(, :auth_mode) # By default, the SALE mode is used. if auth_mode payment = @kb_apis.payment_api .(kb_account.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(kb_account.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.}\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_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) # Pass extra parameters for the gateway here = { # 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, ) super(kb_account_id, 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_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) # Pass extra parameters for the gateway here = {} properties = merge_properties(properties, ) super(kb_account_id, 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_account_id, kb_payment_method_id, properties, context) # Pass extra parameters for the gateway here = {} properties = merge_properties(properties, ) super(kb_account_id, 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_account_id, 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? = 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, , kb_account_id, 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_account_id, kb_payment_method_id, properties, context) # Pass extra parameters for the gateway here = {} properties = merge_properties(properties, ) super(kb_account_id, 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(kb_account_id, refresh_from_gateway, properties, context) # Pass extra parameters for the gateway here = {} properties = merge_properties(properties, ) super(kb_account_id, 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 = {} properties = merge_properties(properties, ) 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_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) # by default, this call will purchase a payment kb_account_id, 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_account_id, 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 = { :linked_transaction_type => linked_transaction_type } properties = merge_properties(properties, ) super(kb_account_id, 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(kb_account_id, 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 = {} properties = merge_properties(properties, ) 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 = {} properties = merge_properties(properties, ) 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_account_id, 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_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, properties, context) # Pass extra parameters for the gateway here = { # Void the original authorization :linked_transaction_type => :authorize } properties = merge_properties(properties, ) super(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, properties, context) end |