简体   繁体   English

ASP.NET Core 中的贝宝

[英]PayPal in ASP.NET Core

How do I integrate with the PayPal API in an ASP.NET Core app?如何在ASP.NET Core应用程序中与PayPal API集成? I have tried various libraries but none of them are compatible with ASP.NET Core ... how do I do it?我尝试了各种库,但没有一个与ASP.NET Core兼容……我该怎么做?

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.截至目前,Paypal 还没有正式发布 .net 核心 sdk,但在 github 上查看,自从提出这个问题以来,已经取得了很大进展。

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开发者目前已经发布了 sdk 的官方测试版,可以在此处 fork 或下载: 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.如果您之前使用的是他们 sdk 的第一个版本,他们甚至还有迁移指南。

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找了几个星期,发现没有办法让 SDK API 与 .Net Core 一起工作

You have a few options, first recreate your project using 4.6 or something.您有几个选择,首先使用 4.6 或其他东西重新创建您的项目。 Secondly using the URL API based call from your app if you are wanting to do single item sales.其次,如果您想进行单件商品销售,请从您的应用程序中使用基于 URL API 的调用。 (Which is what I wanted to do) (这是我想做的)

How I did it was attaching a javascript to the button click that did the following:我是如何将 javascript 附加到执行以下操作的按钮单击的:

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.完成此操作后,您需要在您的贝宝帐户中设置 IPN URL 您的帐户必须是企业帐户,请转到您的个人资料,单击卖家工具,您将看到 IPN 设置。 In there add your web facing URL without a port (Localhost won't work unless you use something like ngrok)在那里添加没有端口的面向网络的 URL(除非你使用类似 ngrok 的东西,否则本地主机将无法工作)

Structuring your Item_code right becomes important here.正确构建您的 Item_code 在这里变得很重要。 The IPN will send list of variables back to your exposed API and then you can do some matching and such. IPN 会将变量列表发送回您公开的 API,然后您可以进行一些匹配等操作。 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卡兹

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.所以我们知道 PayPal 早些时候没有提供任何 nuget 包来支持 .Net Core 中的 PayPal 支付。 But putting an end to this pain, it has finally come out with a solution- by buying BrainTreepayments.com to provide better dev support.但是为了结束这种痛苦,它终于提出了一个解决方案——通过购买BrainTreepayments.com来提供更好的开发支持。

Here's a link for reference.这是一个链接供参考。

I am facing the same issue.我面临同样的问题。 I may go for the REST API implementation with no SDK: https://developer.paypal.com/docs/api/我可能会使用没有 SDK 的 REST API 实现: https : //developer.paypal.com/docs/api/

Or I just found that repository, it might be interesting :)或者我刚刚找到了那个存储库,它可能很有趣:)

https://github.com/geoperez/PayPalCore https://github.com/geoperez/PayPalCore

It is a port of the current .Net SDK to .NETCore.它是当前 .Net SDK 到 .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:您也可以选择旧的 API 选项:

http://www.codeproject.com/Articles/42894/Introduction-to-PayPal-for-C-ASP-NET-developers 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.但由于它很旧,我不推荐它,因为 PayPal 可能有一天会停止使用它。 However you can find additional information there:但是,您可以在那里找到更多信息:

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

新版 SDK (2.0) 发布后,暂时处于候选发布状态,PayPal SDK 将支持 ASP.NET Core。

There is a Nuget package available for PayPal on ASP.NET Core at this link:在此链接上有一个可用于ASP.NET Core上的 PayPal 的 Nuget 包:

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

The package exposes the same API as for classic .NET.该包公开了与经典 .NET 相同的 API。

Please note however, this is not an official Nuget package from PayPal.但是请注意,这不是 PayPal 的官方 Nuget 软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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