Reputation: 1114
I am wresting with the paypal api and given by all that has been written about it I am not the only one :-). However, I could not find the answer to my issue.
I am trying to setup a chained payment in Ruby, like so:
amount = (self.price * 0.95).round(2) #amount to payout to seller
fee = (self.price * 0.05).round(2) #5% fee for my platform
api = PayPal::SDK::AdaptivePayments.new
pay = api.build_pay({
:actionType => "PAY",
:cancelUrl => "https://my-url/paypal_cancel?purchase_guid=" + self.guid,
:currencyCode => self.currency,
:feesPayer => "EACHRECEIVER",
:ipnNotificationUrl => "http://my-url/paypal_ipn_notify?purchase_guid=" + self.guid,
:receiverList => {
:receiver => [{
:amount => amount,
:email => '[email protected]', #this must be the party that receives the full amount initially
:primary => true
}],
:receiver => [{
:amount => fee,
:email => '[email protected]' #this will be the account of my platform that is a secondary receiver and receives the fee
}]},
:returnUrl => "https://some-url/paypal_success?purchase_guid=" + self.guid + "&verification_key=" + paypal_verification_key })
pay_response = api.pay(pay)
This completes without errors but the payer is only prompted to pay the amount/fee specified at the secondary receiver. The amount specified at the primary receiver gets ignored and is nowhere to be found during the payment process.
This all is probably due to lack of knowledge but the entire api is very opaque.
Any ideas? :-) Thanks!
------ EDIT ------
Here's the hash/yaml of the receiversList object:
--- !ruby/object:PayPal::SDK::AdaptivePayments::DataTypes::ReceiverList
receiver: !ruby/array:PayPal::SDK::Core::API::DataTypes::ArrayWithBlock
internal:
- !ruby/object:PayPal::SDK::AdaptivePayments::DataTypes::Receiver
amount: 7.15
email: !ruby/string:PayPal::SDK::Core::API::DataTypes::SimpleTypes::String [email protected]
ivars:
:@block: !ruby/object:Proc {}
"https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=AP-8VD6912739718553G"
I see only one receiver in there so I guess you are right, the problem is somewhere in there. Any idea what part of my code messes this up?
----- EDIT 2 -----
Ok, I clearup the hash issue like so:
:receiverList => {
:receiver => [{
:amount => amount,
:email => '[email protected],
:primary => true
},
{
:amount => fee,
:email => '[email protected]'
}],
}
This results in the following:
--- !ruby/object:PayPal::SDK::AdaptivePayments::DataTypes::ReceiverList
receiver: !ruby/array:PayPal::SDK::Core::API::DataTypes::ArrayWithBlock
internal:
- !ruby/object:PayPal::SDK::AdaptivePayments::DataTypes::Receiver
amount: 135.78
email: !ruby/string:PayPal::SDK::Core::API::DataTypes::SimpleTypes::String [email protected]
primary: true
- !ruby/object:PayPal::SDK::AdaptivePayments::DataTypes::Receiver
amount: 7.15
email: !ruby/string:PayPal::SDK::Core::API::DataTypes::SimpleTypes::String [email protected]
ivars:
:@block: !ruby/object:Proc {}
However, now I get the following error:
undefined method `to_model' for true:TrueClass
when calling
redirect_to api.payment_url(pay_response)
Looks like the api.payment_url method is not returning a string? Any fresh ideas?
Upvotes: 0
Views: 142
Reputation: 14189
You have two elements called :receiver
in the :receiverList
hash. A hash can only have one element for every unique name, so the latter definition overwrites the former, thus the first receiver is lost.
Instead, add both receivers to one array inside :receiver
, so you'll have :receiverList => {:receiver => [{...}, {...}]}
.
Upvotes: 2