简体   繁体   中英

Setting up PayPal adaptive payment flow

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 => 'primary@someurl.com', #this must be the party that receives the full amount initially
            :primary => true
        }],
        :receiver => [{
            :amount => fee,
            :email => 'secondary@someurl.com' #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 ch-facilitator@fluxmatix.com
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 => 'ch-seller@domain.com,
        :primary => true
        },
        {
        :amount => fee,
        :email => 'ch-facilitator@fluxmatix.com'
     }],
}

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 ch-seller@paypal.com
primary: true
- !ruby/object:PayPal::SDK::AdaptivePayments::DataTypes::Receiver
amount: 7.15
email:    !ruby/string:PayPal::SDK::Core::API::DataTypes::SimpleTypes::String ch-facilitator@fluxmatix.com
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?

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 => [{...}, {...}]} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM