From 242cb92852af98f0d3ba99ce303c57589b56081d Mon Sep 17 00:00:00 2001 From: jldelatorre Date: Thu, 27 Oct 2022 17:15:02 -0400 Subject: [PATCH 1/3] Replac PayPal Express with the new Rest API integration --- .../Accounts/StoreSettingsPayPal.cs | 12 + .../ReceivePaypalExpressPayments.cs | 2 +- .../OrderTasks/StartPaypalExpressCheckout.cs | 110 ++++--- .../Hotcakes.Commerce.csproj | 6 + .../Orders/OrderPaymentManager.cs | 1 + .../Payment/Methods/PaypalExpress.cs | 127 +++----- .../Utilities/PaypalExpressUtilities.cs | 48 +-- Libraries/Hotcakes.Commerce/app.config | 16 +- Libraries/Hotcakes.Commerce/packages.config | 2 + .../Hotcakes.PaypalWebServices.csproj | 46 +++ .../Properties/AssemblyInfo.cs | 1 - .../RestPaypalApi.cs | 288 ++++++++++++++++++ .../packages.config | 12 + .../App_LocalResources/Edit.ascx.resx | 22 +- .../PaymentMethods/PaypalExpress/Edit.ascx | 21 +- .../PaymentMethods/PaypalExpress/Edit.ascx.cs | 12 +- .../PaypalExpress/Edit.ascx.designer.cs | 80 ++--- .../PayPalExpressCheckoutController.cs | 82 +++-- .../Hotcakes/Hotcakes.Modules.csproj | 10 +- .../DesktopModules/Hotcakes/packages.config | 2 + 20 files changed, 562 insertions(+), 338 deletions(-) create mode 100644 Libraries/Hotcakes.PaypalWebServices/RestPaypalApi.cs create mode 100644 Libraries/Hotcakes.PaypalWebServices/packages.config diff --git a/Libraries/Hotcakes.Commerce/Accounts/StoreSettingsPayPal.cs b/Libraries/Hotcakes.Commerce/Accounts/StoreSettingsPayPal.cs index 1a6411fb0..d3e54b9ee 100644 --- a/Libraries/Hotcakes.Commerce/Accounts/StoreSettingsPayPal.cs +++ b/Libraries/Hotcakes.Commerce/Accounts/StoreSettingsPayPal.cs @@ -49,6 +49,18 @@ public string Password set { parent.SetPropEncrypted("PaypalPassword", value); } } + public string ClienId + { + get { return parent.GetPropEncrypted("PaypalClientId"); } + set { parent.SetPropEncrypted("PaypalClientId", value); } + } + + public string Secret + { + get { return parent.GetPropEncrypted("PaypalSecret"); } + set { parent.SetPropEncrypted("PaypalSecret", value); } + } + public string Signature { get { return parent.GetPropEncrypted("PaypalSignature"); } diff --git a/Libraries/Hotcakes.Commerce/BusinessRules/OrderTasks/ReceivePaypalExpressPayments.cs b/Libraries/Hotcakes.Commerce/BusinessRules/OrderTasks/ReceivePaypalExpressPayments.cs index bfbff0b3a..56cecb4be 100644 --- a/Libraries/Hotcakes.Commerce/BusinessRules/OrderTasks/ReceivePaypalExpressPayments.cs +++ b/Libraries/Hotcakes.Commerce/BusinessRules/OrderTasks/ReceivePaypalExpressPayments.cs @@ -48,7 +48,7 @@ public override bool Execute(OrderTaskContext context) { //Use the last transaction entered by customer first - List transactions = context.HccApp.OrderServices.Transactions + List transactions = context.HccApp.OrderServices.Transactions .FindForOrder(context.Order.bvin) .OrderByDescending(x => x.TimeStampUtc) .ToList(); diff --git a/Libraries/Hotcakes.Commerce/BusinessRules/OrderTasks/StartPaypalExpressCheckout.cs b/Libraries/Hotcakes.Commerce/BusinessRules/OrderTasks/StartPaypalExpressCheckout.cs index 0219cbc56..baf71398a 100644 --- a/Libraries/Hotcakes.Commerce/BusinessRules/OrderTasks/StartPaypalExpressCheckout.cs +++ b/Libraries/Hotcakes.Commerce/BusinessRules/OrderTasks/StartPaypalExpressCheckout.cs @@ -32,8 +32,13 @@ using System.Web; using Hotcakes.Web.Logging; using System.Globalization; +using System.Net; +using System.Threading.Tasks; using Hotcakes.Commerce.Globalization; using Hotcakes.Commerce.Payment; +using PayPalCheckoutSdk.Core; +using PayPalCheckoutSdk.Orders; +using PayPalHttp; namespace Hotcakes.Commerce.BusinessRules.OrderTasks { @@ -54,7 +59,7 @@ public override bool ProcessCheckout(OrderTaskContext context) { try { - PayPalAPI ppAPI = Utilities.PaypalExpressUtilities.GetPaypalAPI(context.HccApp.CurrentStore); + RestPayPalApi replacePayPal = Utilities.PaypalExpressUtilities.GetRestPaypalAPI(context.HccApp.CurrentStore); string cartReturnUrl = HccUrlBuilder.RouteHccUrl(HccRoute.ThirdPartyPayment, null, Uri.UriSchemeHttps); string cartCancelUrl = HccUrlBuilder.RouteHccUrl(HccRoute.Checkout, null, Uri.UriSchemeHttps); @@ -62,16 +67,16 @@ public override bool ProcessCheckout(OrderTaskContext context) EventLog.LogEvent("PayPal Express Checkout", "CartCancelUrl=" + cartCancelUrl, EventLogSeverity.Information); EventLog.LogEvent("PayPal Express Checkout", "CartReturnUrl=" + cartReturnUrl, EventLogSeverity.Information); - PaymentActionCodeType mode = PaymentActionCodeType.Authorization; + string mode = "AUTHORIZE"; if (!context.HccApp.CurrentStore.Settings.PayPal.ExpressAuthorizeOnly) { - mode = PaymentActionCodeType.Sale; + mode = "CAPTURE"; } // Accelerated boarding if (string.IsNullOrWhiteSpace(context.HccApp.CurrentStore.Settings.PayPal.UserName)) { - mode = PaymentActionCodeType.Sale; + mode = "CAPTURE"; } var solutionType = context.HccApp.CurrentStore.Settings.PayPal.RequirePayPalAccount ? SolutionTypeType.Mark : SolutionTypeType.Sole; @@ -87,7 +92,7 @@ public override bool ProcessCheckout(OrderTaskContext context) PaymentDetailsItemType[] itemsDetails = GetOrderItemsDetails(context); - SetExpressCheckoutResponseType expressResponse; + PayPalHttp.HttpResponse expressResponse; if (addressSupplied) { Contacts.Address address = context.Order.ShippingAddress; @@ -101,12 +106,14 @@ public override bool ProcessCheckout(OrderTaskContext context) if (address.CountryData != null) { - var itemsTotalWithoutTax = context.Order.TotalOrderAfterDiscounts; + var ISOCode = address.CountryData.IsoCode; + + var itemsTotalWithoutTax = context.Order.TotalOrderAfterDiscounts; if (context.HccApp.CurrentStore.Settings.ApplyVATRules) { itemsTotalWithoutTax -= context.Order.ItemsTax; } - string itemsTotal = itemsTotalWithoutTax.ToString("N", CultureInfo.InvariantCulture); + string itemsTotal = itemsTotalWithoutTax.ToString("N", CultureInfo.InvariantCulture); string taxTotal = context.Order.TotalTax.ToString("N", CultureInfo.InvariantCulture); var shippingTotalWithoutTax = context.Order.TotalShippingAfterDiscounts; if (context.HccApp.CurrentStore.Settings.ApplyVATRules) @@ -116,27 +123,27 @@ public override bool ProcessCheckout(OrderTaskContext context) string shippingTotal = shippingTotalWithoutTax.ToString("N", CultureInfo.InvariantCulture); string orderTotal = context.Order.TotalGrand.ToString("N", CultureInfo.InvariantCulture); - expressResponse = ppAPI.SetExpressCheckout( - itemsDetails, - itemsTotal, - taxTotal, - shippingTotal, - orderTotal, - cartReturnUrl, - cartCancelUrl, - mode, - PayPalAPI.GetCurrencyCodeType(context.HccApp.CurrentStore.Settings.PayPal.Currency), - solutionType, - address.FirstName + " " + address.LastName, - address.CountryData.IsoCode, - address.Line1, - address.Line2, - address.City, - address.RegionBvin, - address.PostalCode, - address.Phone, - context.Order.OrderNumber + Guid.NewGuid().ToString(), - isNonShipping); + expressResponse = System.Threading.Tasks.Task.Run(() => replacePayPal.createOrder( + itemsDetails, + itemsTotal, + taxTotal, + shippingTotal, + orderTotal, + cartReturnUrl, + cartCancelUrl, + mode, + context.HccApp.CurrentStore.Settings.PayPal.Currency, + solutionType, + address.FirstName + " " + address.LastName, + ISOCode, + address.Line1, + address.Line2, + address.City, + address.RegionBvin, + address.PostalCode, + address.Phone, + context.Order.OrderNumber + Guid.NewGuid().ToString(), + isNonShipping)).GetAwaiter().GetResult(); if (expressResponse == null) { EventLog.LogEvent("PayPal Express Checkout", "Express Response Was Null!", EventLogSeverity.Error); @@ -163,26 +170,27 @@ public override bool ProcessCheckout(OrderTaskContext context) } string itemsTotal = itemsTotalWithoutTax.ToString("N", CultureInfo.InvariantCulture); string orderTotal = context.Order.TotalOrderAfterDiscounts.ToString("N", CultureInfo.InvariantCulture); - expressResponse = ppAPI.SetExpressCheckout(itemsDetails, - itemsTotal, - taxTotal, - orderTotal, - cartReturnUrl, - cartCancelUrl, - mode, - PayPalAPI.GetCurrencyCodeType(context.HccApp.CurrentStore.Settings.PayPal.Currency), - solutionType, - context.Order.OrderNumber + Guid.NewGuid().ToString(), - isNonShipping); + expressResponse = System.Threading.Tasks.Task.Run(() => replacePayPal.createOrder(itemsDetails, + itemsTotal, + taxTotal, + orderTotal, + cartReturnUrl, + cartCancelUrl, + mode, + context.HccApp.CurrentStore.Settings.PayPal.Currency, + solutionType, + context.Order.OrderNumber + Guid.NewGuid().ToString(), + isNonShipping)).GetAwaiter().GetResult(); + if (expressResponse == null) { EventLog.LogEvent("PayPal Express Checkout", "Express Response2 Was Null!", EventLogSeverity.Error); } } - if (expressResponse.Ack == AckCodeType.Success || expressResponse.Ack == AckCodeType.SuccessWithWarning) + if (expressResponse.StatusCode == HttpStatusCode.Created /*|| expressResponse.Ack == AckCodeType.SuccessWithWarning*/) { - context.Order.ThirdPartyOrderId = expressResponse.Token; + context.Order.ThirdPartyOrderId = expressResponse.Result().Id; // Recording of this info is handled on the paypal express // checkout page instead of here. @@ -193,38 +201,26 @@ public override bool ProcessCheckout(OrderTaskContext context) Orders.OrderNote note = new Orders.OrderNote(); note.IsPublic = false; - note.Note = "Paypal Order Accepted With Paypal Order Number: " + expressResponse.Token; + note.Note = "Paypal Order Accepted With Paypal Order Number: " + expressResponse.Result().Id; context.Order.Notes.Add(note); if (context.HccApp.OrderServices.Orders.Update(context.Order)) { string urlTemplate; if (string.Compare(context.HccApp.CurrentStore.Settings.PayPal.Mode, "Live", true) == 0) { - urlTemplate = "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token={0}"; + urlTemplate = "https://www.paypal.com/checkoutnow?token={0}"; } else { - urlTemplate = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token={0}"; + urlTemplate = "https://www.sandbox.paypal.com/checkoutnow?token={0}"; } HttpContextBase httpContext = new HccHttpContextWrapper(HttpContext.Current); - httpContext.Response.Redirect(string.Format(urlTemplate, expressResponse.Token), true); + httpContext.Response.Redirect(string.Format(urlTemplate, expressResponse.Result().Id), true); } return true; } else { - foreach (ErrorType ppError in expressResponse.Errors) - { - context.Errors.Add(new WorkflowMessage(ppError.ErrorCode, ppError.ShortMessage, true)); - - //create a note to save the paypal error info onto the order - Orders.OrderNote note = new Orders.OrderNote(); - note.IsPublic = false; - note.Note = "Paypal error number: " + ppError.ErrorCode + " Paypal Error: '" + ppError.ShortMessage + "' Message: '" + ppError.LongMessage; - context.Order.Notes.Add(note); - - EventLog.LogEvent("Paypal error number: " + ppError.ErrorCode, "Paypal Error: '" + ppError.ShortMessage + "' Message: '" + ppError.LongMessage + "' " + " Values passed to SetExpressCheckout: Total=" + string.Format("{0:c}", context.Order.TotalOrderBeforeDiscounts) + " Cart Return Url: " + cartReturnUrl + " Cart Cancel Url: " + cartCancelUrl, EventLogSeverity.Error); - } context.Errors.Add(new WorkflowMessage("Paypal checkout error", GlobalLocalization.GetString("PaypalCheckoutCustomerError"), true)); return false; } diff --git a/Libraries/Hotcakes.Commerce/Hotcakes.Commerce.csproj b/Libraries/Hotcakes.Commerce/Hotcakes.Commerce.csproj index 61f94d348..6fd7af8e8 100644 --- a/Libraries/Hotcakes.Commerce/Hotcakes.Commerce.csproj +++ b/Libraries/Hotcakes.Commerce/Hotcakes.Commerce.csproj @@ -94,6 +94,12 @@ ..\..\References\Dnn\Newtonsoft.Json.dll + + ..\..\packages\PayPalCheckoutSdk.1.0.4\lib\netstandard2.0\PayPalCheckoutSdk.dll + + + ..\..\packages\PayPalHttp.1.0.1\lib\netstandard2.0\PayPalHttp-Dotnet.dll + ..\..\References\Misc\paypal_base.dll True diff --git a/Libraries/Hotcakes.Commerce/Orders/OrderPaymentManager.cs b/Libraries/Hotcakes.Commerce/Orders/OrderPaymentManager.cs index f6224c60e..ccfe2ac86 100644 --- a/Libraries/Hotcakes.Commerce/Orders/OrderPaymentManager.cs +++ b/Libraries/Hotcakes.Commerce/Orders/OrderPaymentManager.cs @@ -1242,6 +1242,7 @@ public bool PayPalExpressCompleteAllPayments() var t = CreateEmptyTransaction(); t.Card = p.CreditCard; t.Amount = p.Amount; + t.PreviousTransactionNumber = p.RefNum1; var processor = new PaypalExpress(); diff --git a/Libraries/Hotcakes.Commerce/Payment/Methods/PaypalExpress.cs b/Libraries/Hotcakes.Commerce/Payment/Methods/PaypalExpress.cs index e396529bd..3314ca14c 100644 --- a/Libraries/Hotcakes.Commerce/Payment/Methods/PaypalExpress.cs +++ b/Libraries/Hotcakes.Commerce/Payment/Methods/PaypalExpress.cs @@ -25,10 +25,14 @@ using System; using System.Globalization; +using System.Net; +using System.Threading.Tasks; using com.paypal.soap.api; using Hotcakes.Commerce.Utilities; using Hotcakes.Payment; using Hotcakes.PaypalWebServices; +using PayPalCheckoutSdk.Orders; +using PayPalHttp; namespace Hotcakes.Commerce.Payment.Methods { @@ -64,7 +68,7 @@ public bool Authorize(Transaction t, HotcakesApplication app) { try { - var ppAPI = PaypalExpressUtilities.GetPaypalAPI(app.CurrentStore); + var ppAPI = PaypalExpressUtilities.GetRestPaypalAPI(app.CurrentStore); if (t.PreviousTransactionNumber != null) { @@ -76,20 +80,15 @@ public bool Authorize(Transaction t, HotcakesApplication app) mode = PaymentActionCodeType.Sale; } - var paymentResponse = ppAPI.DoExpressCheckoutPayment(t.PreviousTransactionNumber, - t.PreviousTransactionAuthCode, - t.Amount.ToString("N", CultureInfo.InvariantCulture), - mode, - PayPalAPI.GetCurrencyCodeType(app.CurrentStore.Settings.PayPal.Currency), - OrderNumber); + var paymentResponse = Task.Run(() => ppAPI.AuthorizeOrder(t.PreviousTransactionNumber)).GetAwaiter().GetResult(); + - if (paymentResponse.Ack == AckCodeType.Success || - paymentResponse.Ack == AckCodeType.SuccessWithWarning) + if (paymentResponse.StatusCode == HttpStatusCode.Created) { - var paymentInfo = paymentResponse.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0]; + var paymentInfo = paymentResponse.Result().PurchaseUnits[0].Payments.Authorizations[0]; t.Result.Succeeded = true; - t.Result.ReferenceNumber = paymentInfo.TransactionID; + t.Result.ReferenceNumber = paymentInfo.Id; t.Result.ResponseCode = "OK"; t.Result.ResponseCodeDescription = "PayPal Express Payment Authorized Successfully."; return true; @@ -97,10 +96,6 @@ public bool Authorize(Transaction t, HotcakesApplication app) t.Result.Succeeded = false; t.Result.Messages.Add(new Message("PayPal Express Payment Authorization Failed.", string.Empty, MessageType.Error)); - foreach (var ppError in paymentResponse.Errors) - { - t.Result.Messages.Add(new Message(ppError.LongMessage, ppError.ErrorCode, MessageType.Error)); - } return false; } } @@ -116,22 +111,20 @@ public bool Capture(Transaction t, HotcakesApplication app) { try { - var ppAPI = PaypalExpressUtilities.GetPaypalAPI(app.CurrentStore); + var ppAPI = PaypalExpressUtilities.GetRestPaypalAPI(app.CurrentStore); var OrderNumber = t.MerchantInvoiceNumber + Guid.NewGuid(); - var captureResponse = ppAPI.DoCapture(t.PreviousTransactionNumber, - "Thank you for your payment.", + var captureResponse = Task.Run(() => ppAPI.CaptureAuthorizedOrder(t.PreviousTransactionNumber, t.Amount.ToString("N", CultureInfo.InvariantCulture), - PayPalAPI.GetCurrencyCodeType(app.CurrentStore.Settings.PayPal.Currency), - OrderNumber); + app.CurrentStore.Settings.PayPal.Currency, + OrderNumber)).GetAwaiter().GetResult(); - if (captureResponse.Ack == AckCodeType.Success || captureResponse.Ack == AckCodeType.SuccessWithWarning) + if (captureResponse.StatusCode == HttpStatusCode.OK || captureResponse.StatusCode == HttpStatusCode.Created) { - t.Result.ReferenceNumber = captureResponse.DoCaptureResponseDetails.PaymentInfo.TransactionID; + t.Result.ReferenceNumber = captureResponse.Result().Id; - if (captureResponse.DoCaptureResponseDetails.PaymentInfo.PaymentStatus == - PaymentStatusCodeType.Pending) + if (captureResponse.Result().Status == "PENDING") { t.Result.Succeeded = true; t.Result.ResponseCode = "PENDING"; @@ -140,37 +133,16 @@ public bool Capture(Transaction t, HotcakesApplication app) MessageType.Information)); return true; } - if (captureResponse.DoCaptureResponseDetails.PaymentInfo.PaymentStatus == - PaymentStatusCodeType.InProgress) - { - t.Result.Succeeded = true; - t.Result.ResponseCode = "PENDING"; - t.Result.ResponseCodeDescription = "PayPal Express Payment IN PROGRESS"; - t.Result.Messages.Add(new Message("Paypal Express Payment PENDING. In Progress.", "OK", - MessageType.Information)); - return true; - } - if (captureResponse.DoCaptureResponseDetails.PaymentInfo.PaymentStatus == PaymentStatusCodeType.None) + if (captureResponse.Result().Status == "DENIED") { t.Result.Succeeded = true; t.Result.ResponseCode = "PENDING"; - t.Result.ResponseCodeDescription = "PayPal Express Payment: No Status Yet"; - t.Result.Messages.Add(new Message("Paypal Express Payment PENDING. No Status Yet.", "OK", + t.Result.ResponseCodeDescription = "PayPal Express Payment DENIED"; + t.Result.Messages.Add(new Message("Paypal Express Payment DENIED.", "OK", MessageType.Information)); return true; } - if (captureResponse.DoCaptureResponseDetails.PaymentInfo.PaymentStatus == - PaymentStatusCodeType.Processed) - { - t.Result.Succeeded = true; - t.Result.ResponseCode = "PENDING"; - t.Result.ResponseCodeDescription = "PayPal Express Payment PENDING"; - t.Result.Messages.Add(new Message("Paypal Express Payment PENDING.", "OK", - MessageType.Information)); - return true; - } - if (captureResponse.DoCaptureResponseDetails.PaymentInfo.PaymentStatus == - PaymentStatusCodeType.Completed) + if (captureResponse.Result().Status == "COMPLETED") { t.Result.Succeeded = true; t.Result.Messages.Add(new Message("PayPal Express Payment Captured Successfully.", "OK", @@ -185,10 +157,6 @@ public bool Capture(Transaction t, HotcakesApplication app) t.Result.Succeeded = false; t.Result.Messages.Add(new Message("Paypal Express Payment Charge Failed.", string.Empty, MessageType.Error)); - foreach (var ppError in captureResponse.Errors) - { - t.Result.Messages.Add(new Message(ppError.LongMessage, ppError.ErrorCode, MessageType.Error)); - } return false; } catch (Exception ex) @@ -202,33 +170,28 @@ public bool Charge(Transaction t, HotcakesApplication app) { try { - var ppAPI = PaypalExpressUtilities.GetPaypalAPI(app.CurrentStore); + var ppAPI = PaypalExpressUtilities.GetRestPaypalAPI(app.CurrentStore); - var OrderNumber = t.MerchantInvoiceNumber + Guid.NewGuid(); - DoExpressCheckoutPaymentResponseType paymentResponse; + HttpResponse paymentResponse; //there was no authorization so we just need to do a direct sale - paymentResponse = ppAPI.DoExpressCheckoutPayment(t.PreviousTransactionNumber, - t.PreviousTransactionAuthCode, - t.Amount.ToString("N", CultureInfo.InvariantCulture), - PaymentActionCodeType.Sale, - PayPalAPI.GetCurrencyCodeType(app.CurrentStore.Settings.PayPal.Currency), - OrderNumber); + + paymentResponse = Task.Run(() => ppAPI.CaptureOrder(t.PreviousTransactionNumber)).GetAwaiter().GetResult(); - if (paymentResponse.Ack == AckCodeType.Success || paymentResponse.Ack == AckCodeType.SuccessWithWarning) + if (paymentResponse.StatusCode == HttpStatusCode.Created || paymentResponse.StatusCode == HttpStatusCode.OK) { - var paymentInfo = paymentResponse.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0]; + var paymentInfo = paymentResponse.Result().PurchaseUnits[0].Payments.Captures[0]; - t.Result.ReferenceNumber = paymentInfo.TransactionID; + t.Result.ReferenceNumber = paymentInfo.Id; - if (paymentInfo.PaymentStatus == PaymentStatusCodeType.Completed) + if (paymentInfo.Status == "COMPLETED") { t.Result.Succeeded = true; t.Result.Messages.Add(new Message("PayPal Express Payment Charged Successfully.", "OK", MessageType.Information)); return true; } - if (paymentInfo.PaymentStatus == PaymentStatusCodeType.Pending) + if (paymentInfo.Status == "PENDING") { t.Result.Succeeded = true; t.Result.ResponseCode = "PENDING"; @@ -245,10 +208,7 @@ public bool Charge(Transaction t, HotcakesApplication app) t.Result.Succeeded = false; t.Result.Messages.Add(new Message("Paypal Express Payment Charge Failed.", string.Empty, MessageType.Error)); - foreach (var ppError in paymentResponse.Errors) - { - t.Result.Messages.Add(new Message(ppError.LongMessage, ppError.ErrorCode, MessageType.Error)); - } + return false; } catch (Exception ex) @@ -262,20 +222,19 @@ public bool Refund(Transaction t, HotcakesApplication app) { try { - var ppAPI = PaypalExpressUtilities.GetPaypalAPI(app.CurrentStore); + var ppAPI = PaypalExpressUtilities.GetRestPaypalAPI(app.CurrentStore); if (t.PreviousTransactionNumber != null) { var refundType = string.Empty; //per paypal's request, the refund type should always be set to partial refundType = "Partial"; - var refundResponse = ppAPI.RefundTransaction( + + var refundResponse = Task.Run(() => ppAPI.CapturesRefund( t.PreviousTransactionNumber, - refundType, t.Amount.ToString("N", CultureInfo.InvariantCulture), - PayPalAPI.GetCurrencyCodeType(app.CurrentStore.Settings.PayPal.Currency)); - if (refundResponse.Ack == AckCodeType.Success || - refundResponse.Ack == AckCodeType.SuccessWithWarning) + app.CurrentStore.Settings.PayPal.Currency)).GetAwaiter().GetResult(); + if (refundResponse.StatusCode == HttpStatusCode.Created) { t.Result.Succeeded = true; t.Result.Messages.Add(new Message("PayPal Express Payment Refunded Successfully.", "OK", @@ -285,10 +244,6 @@ public bool Refund(Transaction t, HotcakesApplication app) t.Result.Succeeded = false; t.Result.Messages.Add(new Message("Paypal Express Payment Refund Failed.", string.Empty, MessageType.Error)); - foreach (var ppError in refundResponse.Errors) - { - t.Result.Messages.Add(new Message(ppError.LongMessage, ppError.ErrorCode, MessageType.Error)); - } return false; } } @@ -303,12 +258,12 @@ public bool Void(Transaction t, HotcakesApplication app) { try { - var ppAPI = PaypalExpressUtilities.GetPaypalAPI(app.CurrentStore); + var ppAPI = PaypalExpressUtilities.GetRestPaypalAPI(app.CurrentStore); if (t.PreviousTransactionNumber != null) { - var voidResponse = ppAPI.DoVoid(t.PreviousTransactionNumber, "Transaction Voided"); - if (voidResponse.Ack == AckCodeType.Success || voidResponse.Ack == AckCodeType.SuccessWithWarning) + var voidResponse = Task.Run(() => ppAPI.VoidAuthorizedPayment(t.PreviousTransactionNumber)).GetAwaiter().GetResult(); + if (voidResponse.StatusCode == HttpStatusCode.NoContent) { t.Result.Succeeded = true; t.Result.Messages.Add(new Message("PayPal Express Payment Voided Successfully.", "OK", @@ -318,10 +273,6 @@ public bool Void(Transaction t, HotcakesApplication app) t.Result.Succeeded = false; t.Result.Messages.Add(new Message("Paypal Express Payment Void Failed.", string.Empty, MessageType.Error)); - foreach (var ppError in voidResponse.Errors) - { - t.Result.Messages.Add(new Message(ppError.LongMessage, ppError.ErrorCode, MessageType.Error)); - } return false; } } diff --git a/Libraries/Hotcakes.Commerce/Utilities/PaypalExpressUtilities.cs b/Libraries/Hotcakes.Commerce/Utilities/PaypalExpressUtilities.cs index 0eabb49cb..948c4fda0 100644 --- a/Libraries/Hotcakes.Commerce/Utilities/PaypalExpressUtilities.cs +++ b/Libraries/Hotcakes.Commerce/Utilities/PaypalExpressUtilities.cs @@ -33,50 +33,12 @@ namespace Hotcakes.Commerce.Utilities { public class PaypalExpressUtilities { - public static PayPalAPI GetPaypalAPI(Store currentStore) + public static RestPayPalApi GetRestPaypalAPI(Store currentStore) { - var APIProfile - = CreateAPIProfile(currentStore.Settings.PayPal.UserName, - currentStore.Settings.PayPal.Password, - currentStore.Settings.PayPal.Signature, - currentStore.Settings.PayPal.FastSignupEmail, - currentStore.Settings.PayPal.Mode); - - return new PayPalAPI(APIProfile); - } - - private static IAPIProfile CreateAPIProfile(string PayPalUserName, string PayPalPassword, string PayPalSignature, - string subject, string mode) - { - try - { - var profile = ProfileFactory.createSignatureAPIProfile(); - - if (profile != null) - { - profile.Environment = mode; - - EventLog.LogEvent("PayPal Express Get Api", "Getting Environment " + mode, - EventLogSeverity.Information); - - profile.APIUsername = PayPalUserName; - profile.APIPassword = PayPalPassword; - profile.APISignature = PayPalSignature; - profile.Subject = subject; - } - else - { - EventLog.LogEvent("Paypal API", - "Paypal com.paypal.sdk.profiles.ProfileFactory.CreateAPIProfile has failed.", - EventLogSeverity.Error); - } - return profile; - } - catch (Exception ex) - { - EventLog.LogEvent("PayPal Utilities", ex.Message + " | " + ex.StackTrace, EventLogSeverity.Warning); - } - return null; + return new RestPayPalApi(currentStore.Settings.PayPal.ClienId, + currentStore.Settings.PayPal.Secret, currentStore.Settings.PayPal.Mode); } } + + } \ No newline at end of file diff --git a/Libraries/Hotcakes.Commerce/app.config b/Libraries/Hotcakes.Commerce/app.config index bf878282b..bf9b35c1f 100644 --- a/Libraries/Hotcakes.Commerce/app.config +++ b/Libraries/Hotcakes.Commerce/app.config @@ -1,28 +1,28 @@ - + -
+
- + - + - + - - + + - + diff --git a/Libraries/Hotcakes.Commerce/packages.config b/Libraries/Hotcakes.Commerce/packages.config index 756907dc3..ff7fbc845 100644 --- a/Libraries/Hotcakes.Commerce/packages.config +++ b/Libraries/Hotcakes.Commerce/packages.config @@ -4,4 +4,6 @@ + + \ No newline at end of file diff --git a/Libraries/Hotcakes.PaypalWebServices/Hotcakes.PaypalWebServices.csproj b/Libraries/Hotcakes.PaypalWebServices/Hotcakes.PaypalWebServices.csproj index 0ba44f583..df4819591 100644 --- a/Libraries/Hotcakes.PaypalWebServices/Hotcakes.PaypalWebServices.csproj +++ b/Libraries/Hotcakes.PaypalWebServices/Hotcakes.PaypalWebServices.csproj @@ -64,21 +64,64 @@ false + + ..\..\packages\PayPalCheckoutSdk.1.0.4\lib\netstandard2.0\PayPalCheckoutSdk.dll + + + ..\..\packages\PayPalHttp.1.0.1\lib\netstandard2.0\PayPalHttp-Dotnet.dll + False ..\..\References\Misc\paypal_base.dll + 3.5 + + ..\..\packages\System.IO.4.3.0\lib\net462\System.IO.dll + True + True + + + ..\..\packages\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll + True + True + + + ..\..\packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll + True + True + + + ..\..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net463\System.Security.Cryptography.Algorithms.dll + True + True + + + ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll + True + True + + + ..\..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll + True + True + + + ..\..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll + True + True + Properties\CommonAssemblyInfo.cs + @@ -104,6 +147,9 @@ true + + +