21

How do I integrate with the PayPal API in an ASP.NET Core app? I have tried various libraries but none of them are compatible with ASP.NET Core... how do I do it?

Serj Sagan
  • 28,927
  • 17
  • 154
  • 183

6 Answers6

8

In case somebody finds this question I'll post an update.

As of now, there is no official release of a .net core sdk for Paypal but looking on github there has been a lot of progress since this question was asked.

The developper have currently released an official beta version of the sdk that is available for fork or download here : https://github.com/paypal/PayPal-NET-SDK/tree/2.0-beta

They even have a migration guide if you were previously using the first version of their sdk.

Louis-Roch Tessier
  • 822
  • 1
  • 13
  • 25
5

I had the same issue with you. Looked for weeks and found there is just no way to get the SDK API working with .Net Core

You have a few options, first recreate your project using 4.6 or something. Secondly using the URL API based call from your app if you are wanting to do single item sales. (Which is what I wanted to do)

How I did it was attaching a javascript to the button click that did the following:

function PayPalPaymentEvent(eventid) {

    var URL = 'https://www.paypal.com/cgi-bin/webscr?';
    var cmd = '_xclick';
    var business = Your Business Email;
    var currency_code = 'AUD';
    var amount = 100;
    var item_name = Name Of Your Item;
    var item_number = Some Identifier;
    var returnurl = 'http://somepage?info=success';
    var cancel_return = 'http://somepage?info=failed';
    var notify_url = 'http://WebFacingSite/API/PayPalReg';
    var tax = (amount * 0.10);

    var fullURL = URL + 'cmd=' + cmd + '&business=' + business + '&currency_code=' + currency_code + '&amount=' + amount + '&tax=' + tax + '&item_name=' + item_name + '&item_number=' + item_number + '&return=' + returnurl + '&cancel_return=' + cancel_return + '&notify_url=' + notify_url;

    ///// this ajax bit I use to record the transaction has started
    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        url: '/API/PaymentStarted?eventid=' + eventid + '&UserID=' + UserID + '&paymentID' + paymentID,
        error: function () {
            SetMessage('error', 'Something has gone horribly, horribly wrong')
        },
        success: function (data) {

            window.location.href = fullURL;

        },
        type: 'POST'
    });


  }

Once you have done this you will need to set up a IPN URL in your paypal account Your account must be a business account, go to your profile, click seller tools and you will see IPN Settings. In there add your web facing URL without a port (Localhost won't work unless you use something like ngrok)

Structuring your Item_code right becomes important here. The IPN will send list of variables back to your exposed API and then you can do some matching and such. This won't suit you but this is how I catch that message and deal with it. My scenario is that I have a user who signs up for an event:

[HttpPost]
    [Route("API/PayPalReg")]
    public JsonResult ReceiveInput(string txn_id, string payment_date,
                                string payer_email, string payment_status, 
                                string first_name, string last_name, 
                                string item_number, string item_name, 
                                string payer_id, string verify_sign)
    {

        var paypaltypes = item_name.Split('-');


        var result = item_number.Split('-');
        var userid = int.Parse(result[1]);
        var TransPaymentString = result[1].ToString() + result[0].ToString();
        var TransPayment = int.Parse(TransPaymentString);
        var user = _context.Person.Include(p => p.Payments).Where(p => p.UserID == userid).Single();
        var payment = user.Payments.Where(p => p.TransPaymentID == TransPayment).Single();

        if (paypaltypes[0] == "Event")
        {
            var eventid = int.Parse(result[0]);

            payment.PaymentReceipt = txn_id;
            payment.PaymentReceived = true;
            payment.PaymentReceivedDate = DateTime.Now;
            payment.PaymentNotes = payer_email + " " + first_name + " " + last_name + " " + item_number + " " + payer_id + " " + verify_sign + " " + item_name;

            _context.Payments.Update(payment);
            _context.SaveChanges();

            var userevent = _context.Person.Include(p => p.EventRegistry).Where(p => p.UserID == userid).Single();
            var eventreg = userevent.EventRegistry.Where(er => er.EventID == eventid).Single();
            eventreg.EventPaid = true;

            _context.EventRegistry.Update(eventreg);
            _context.SaveChanges();
            Response.StatusCode = (int)HttpStatusCode.OK;
            return Json("Json Result");

        }

Hope this helps a bit

Caz

Caz1224
  • 1,539
  • 1
  • 14
  • 37
5

So maybe,it's too late when i am posting this and you've moved out of that problem,but it's for the ease of any person who may need this in future.

So as we knew that PayPal had earlier not provided any nuget package for supporting PayPal payments in .Net Core. But putting an end to this pain, it has finally come out with a solution- by buying BrainTreepayments.com to provide better dev support.

Here's a link for reference.

Praveen
  • 119
  • 3
  • 9
  • So, is it free for me as a developer who wants to integrate a PayPal payment into an app or should I pay to the `BrainTreepayments.com`? – manymanymore May 27 '20 at 19:48
  • The sandbox environment of Braintree is free. I've done that and it works fine. However, I am not sure about the charges for production environment of that. You can confirm about the production account from the link I've provided above. – Praveen May 30 '20 at 07:02
  • Got it. Thank you. – manymanymore May 31 '20 at 08:00
3

I am facing the same issue. I may go for the REST API implementation with no SDK: https://developer.paypal.com/docs/api/

Or I just found that repository, it might be interesting :)

https://github.com/geoperez/PayPalCore

It is a port of the current .Net SDK to .NETCore. I haven't checked the code yet, but if it works it would save a lot of time!

You may also go for an old API option:

http://www.codeproject.com/Articles/42894/Introduction-to-PayPal-for-C-ASP-NET-developers

But as it is old I would not recommend it as PayPal may discontinue it someday. However you can find additional information there:

https://developer.paypal.com/webapps/developer/docs/classic/paypal-payments-standard/integration-guide/formbasics/

Jean
  • 4,911
  • 3
  • 29
  • 50
1

After releasing new version of SDK (2.0) which is in release candidate state for a moment, PayPal SDK will support ASP.NET Core.

Alexej Sommer
  • 2,677
  • 1
  • 14
  • 25
0

There is a Nuget package available for PayPal on ASP.NET Core at this link:

https://www.nuget.org/packages/PayPal.SDK.NETCore/

The package exposes the same API as for classic .NET.

Please note however, this is not an official Nuget package from PayPal.

sboisse
  • 4,860
  • 3
  • 37
  • 48