1

I am new to Meteor and integrating Paypal(Which i never had done).

from Client side in meteor - 

I am calling method on button click.

<MDBBtn onClick={(e) => callPaypal(e)} color="primary" type="submit">
                Add and Continue to PayPal
</MDBBtn>

And this callpaypal() method ->

import { Link as ReactRouterLink } from 'react-router-dom'

  const callPaypal = (e) => {
    e.preventDefault();
    Meteor.call('createPayalPayment', (err, res) => {
      console.log(res[1].href)                                  **FIRST CONSOLE**
      if (res) {
        let link = res[1];
        if (link.href) {
          return <ReactRouterLink to={`${link.href}`} />
        }
      }
    })
  }

Calling createPayalPayment method from server ->

import { Config } from "./paypal_config";

createPayalPayment() {
    var data = {
      "intent": "sale",
      "payer": {
        "payment_method": "paypal"
      },
      "redirect_urls": {
        // "return_url": `${Meteor.absoluteUrl('/execute'), { "replaceLocalhost": "true" }}`,
        "return_url": "http://127.0.0.1:3000/execute",
        "cancel_url": "http://172.20.10.5:3000/cancel"
      },
      "transactions": [{
        "amount": {
          "currency": "USD",
          "total": "1.00"
        },
        "description": "This is the payment description."
      }]
    };

    paypal.configure(Config);
    var ppCreate = Meteor.wrapAsync(paypal.payment.create.bind(paypal.payment));
    var ppExecute = Meteor.wrapAsync(paypal.payment.execute.bind(paypal.payment));
    var response = ppCreate(data);
    if (response.state !== 'created') {
      console.log('not created!!!!!!!!!!!!!!!!')
    }
    else {
    console.log(response);                                             **SECOND CONSOLE**
    return response.links;
    }
  }

And here is my Paypal config ->

export const Config = {
  'mode': 'sandbox',
  'client_id': 'client_Id',
  'client_secret': 'secret'
};

FIRST CONSOLE --> 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-1HR12649X9688931M'

SECOND CONSOLE -->>

{ id: 'PAYID-L2IJO4I8GE24787GF168351L',
   intent: 'sale',
   state: 'created',
   payer: { payment_method: 'paypal' },


   transactions: 
    [ { amount: [Object],
        description: 'This is the payment description.',
        related_resources: [] } ],
   create_time: '2020-04-10T15:57:37Z',

   links: 
    [ { href: 'https://api.sandbox.paypal.com/v1/payments/payment/PAYID-L2IJO4I8GE24787GF168351L',
        rel: 'self',
        method: 'GET' },
      { href: 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-1HR12649X9688931M',
        rel: 'approval_url',
        method: 'REDIRECT' },
      { href: 'https://api.sandbox.paypal.com/v1/payments/payment/PAYID-L2IJO4I8GE24787GF168351L/execute',
        rel: 'execute',
        method: 'POST' } ],


   httpStatusCode: 201 
}

As the links[2].href is the URL, where the paypal should be redirect here and user can login to the account. But It is not redirecting. So I am manually redirecting to this link in callPaypal() method just below the First console. But Still the router is unable to redirect to the link maybe Outer Domain Issue or whatever even it's not showing error. Please Is there any way that the paypal redirect itself to paypal login? I have already wasted my 2 days on this and still have nothing. Thanks.

I added the Redirect URL in my paypal developer account for this project.

Rajat Sharma
  • 27
  • 1
  • 8
  • Hi, I had not the time to dig in deep but as far as your console output shows the right URL when using res[1].href - I guess it is just a React routing issue. Hopefully you find answers here: https://medium.com/@bsangars15/react-button-click-navigate-to-new-page-6af7397ea220 or here: https://stackoverflow.com/questions/44877821/how-to-navigate-on-path-by-button-click-in-react-router-v4 - Cheers, Tom – Tom Freudenberg Apr 10 '20 at 17:00

1 Answers1

0

It looks like you're using an old, redirect-based PayPal integration, so my recommendation is trying the new in-context experience: https://developer.paypal.com/demo/checkout/#/pattern/server

Notice the two fetch calls to '/demo/..' placeholders, which would need to be replaced with actual routes on your server. The first should return a PayID (or newer v2/orders ID), and the second should execute/capture that ID.

This integration is superior because your site stays loaded in the background, and the buyer is able to checkout and pay without 'leaving' it.


On the server side, it looks like you may be using the old deprecated v1 PayPal-node-SDK, which there is no reason to do for a new integration. Instead, use the v2 Checkout-NodeJS-SDK

Preston PHX
  • 15,348
  • 3
  • 16
  • 35
  • Hi Preston , Thanks for the help. Let me tell you the motive of app, I (Admin) just want to pay the money in dollars, to the user who registered to my app, in their paypal account using my paypal via paypal sdk. So, Is this right way to do ? Or I need some another thing? I never used paypal so really confused. – Rajat Sharma Apr 13 '20 at 06:10
  • If you are sending money to someone, you do not need a checkout for this. You can do it manually in www.paypal.com , using the Send Money interface . If you need to do it in large or automated batches you can apply for PayPal Payouts, which has an API https://developer.paypal.com/docs/payouts/integrate/prerequisites/ – Preston PHX Apr 13 '20 at 13:48
  • Well my above code worked. Though I am using the old v1 of sdk but still It is working. The main problem I was facing is It is not redirecting to the paypal login link(i.e. links[2].href in the response) automatically. So, i did it manually by returning this link to client side and using ```windows.location.href = links[2].href``` and it worked. Now It redirect to the paypal login window. Can you suggest me anything better than this step Preston ? So, that in future If anyone facing the same they can have a better solution. Thanks for your help. – Rajat Sharma Apr 14 '20 at 12:15