Dinamyc Reciever "Population" (Chained Payments) #59

Open
Jmlevick opened this Issue Jun 23, 2012 · 1 comment

2 participants

@Jmlevick

Note: I didn't find a better title for this O.o

Humm, What I want to do is to have a Rails app where a visitor can click a button/link to make a "special" chained payment; Currently I have a Users registration form that has one field for the user to enter his/her paypal account email, and as I saw here: http://marker.to/XGg9MR it is possible to specify the primary reciever and the secondary ones by adding such info in a controller action when using your gem in a rails app. The thing is, I don't want to hard code the secondary reciever as I need to specify a different secondary reciever from time to time, (being specific my primary reciever will always be the same, but depending on what button/link the visitor clicks, the secondary one is going to change) and I want that secondary reciever email to be the paypal e-mail account from one of the registered users when the visitor clicks on their specific button/link...

My question is:

Is it possible to create such enviroment functionality in my app using the current implementation of your gem?

Could you point me in the right direction on how to accomplish such thing? I'm really new in the paypal handling universe! XD

Thanks! :)

@rfunduk

Hi there, I'm having some trouble with PayPal at the moment - specifically it wont let me do exactly what you're asking about in the live app, but it does work in the sandbox. So I came across your issue here and while I can't guarantee it works in production (I believe the issue is a configuration problem on paypal's side and not a code problem, but can't be sure), the code is pretty straight forward and might help you. Let's get to it...

You can pretty easily define the receivers dynamically. Here's a class that demonstrates it using hard coded data (warning: untested code extracted from an app which works :))...

class PaypalGateway
  def initialize
    @request = PaypalAdaptive::Request.new
  end

  def pay!
    data = {
      requestEnvelope: { errorLanguage: 'en_US' },
      currencyCode: 'USD',
      feesPayer: 'EACHRECEIVER',
      actionType: 'PAY',
      cancelUrl: 'http://example.com/paypal/cancel',
      returnUrl: 'http://example.com/paypal/return',
      ipnNotificationUrl: 'http://example.com/paypal/ipn',
      memo: "A chained payment",
      receiverList: {
        receiver: [
          { email: 'receiver1@gmail.com', amount: 50, primary: true },
          { email: 'receiver2@gmail.com', amount: 5, primary: false }
        ]
      }
    }

    result = @request.pay(data)
    return result if result.success? && result['paymentExecStatus'] == 'CREATED'
  end
end

So this can be used very simply from a controller/etc like so:

gateway = PaypalGateway.new
redirect = gateway.pay!
redirect_to redirect.approve_paypal_payment_url unless redirect.nil?

If the payment is successful then $50 - $5 will go to receiver1 and $5 will go to receiver2, less paypal fees as usual. I'm sure you can see how this could easily be fleshed out to dynamically create that array of receivers (namely, params to pay, or even the initializer).

HTH, wish me luck with PayPal support on my issue :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment