Skip to content

Commit

Permalink
Implement setPaymentOptions call for PayPal
Browse files Browse the repository at this point in the history
  • Loading branch information
marcaltmann committed Aug 23, 2017
1 parent 7b1d0f8 commit 7bd331c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
16 changes: 16 additions & 0 deletions app/models/payments/paypal_payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,29 @@ def after_create_path
PaypalAPI.checkout_url pay_key
end

# totally untested function that really should be part of abacus
def total_tax
total = Money.new(0)

self.business_transactions.each do |bt|
price = bt.article_price - (bt.article_price / (1.0 + bt.article_vat / 100.0))
total += price * bt.quantity_bought
end

total
end

private

# send paypal request on init
def initialize_payment
response = PaypalAPI.new.request_for(self)
if response.success?
self.pay_key = response['payKey']

# here: second call to set_payment_options with pay_key and other data
# e.g. PaypalAPI.new.set_payment_options(self)

true # continue
else
self.error = response.errors.to_json
Expand Down
26 changes: 25 additions & 1 deletion app/objects/service/paypal_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ def request_for payment
'requestEnvelope' => { 'errorLanguage' => 'en_US' },
'currencyCode' => 'EUR',
'receiverList' => {
'receiver' => [{ 'email' => payment.line_item_group_seller_paypal_account, 'amount' => payment.total_price.to_f.to_s }] },
'receiver' => [{
'email' => payment.line_item_group_seller_paypal_account,
'amount' => payment.total_price.to_f.to_s
}]
},
'cancelUrl' => line_item_group_url(payment.line_item_group, paid: false),
'actionType' => 'PAY',
'ipnNotificationUrl' => ipn_notification_url,
Expand All @@ -29,6 +33,26 @@ def request_for payment
end
end

def set_payment_options(payment)
begin
Timeout.timeout(15) do
paypal_client.set_payment_options(
'payKey' => payment.pay_key,
'requestEnvelope' => {
'errorLanguage' => 'en_US'
},
'receiverOptions' => {
'invoiceData' => {
'totalTax' => payment.total_tax
}
}
)
end
rescue Timeout::Error
Struct.new(:success?, :errors).new(false, 'Timeout') # Mock response object
end
end

private

def paypal_client
Expand Down

0 comments on commit 7bd331c

Please sign in to comment.