Skip to content

Commit

Permalink
Merge pull request #425 from jldelatorre/development
Browse files Browse the repository at this point in the history
Replace PayPal Express
  • Loading branch information
WillStrohl authored Nov 1, 2022
2 parents 7fae564 + 1f582c4 commit 9f3f49b
Show file tree
Hide file tree
Showing 22 changed files with 605 additions and 356 deletions.
12 changes: 12 additions & 0 deletions Libraries/Hotcakes.Commerce/Accounts/StoreSettingsPayPal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override bool Execute(OrderTaskContext context)
{

//Use the last transaction entered by customer first
List<OrderTransaction> transactions = context.HccApp.OrderServices.Transactions
List<OrderTransaction> transactions = context.HccApp.OrderServices.Transactions
.FindForOrder(context.Order.bvin)
.OrderByDescending(x => x.TimeStampUtc)
.ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@
using System.Web;
using Hotcakes.Web.Logging;
using System.Globalization;
using System.Net;
using System.Threading.Tasks;
using Hotcakes.Commerce.Common;
using Hotcakes.Commerce.Globalization;
using Hotcakes.Commerce.Payment;
using PayPalCheckoutSdk.Core;
using PayPalCheckoutSdk.Orders;
using PayPalHttp;

namespace Hotcakes.Commerce.BusinessRules.OrderTasks
{
Expand All @@ -54,24 +60,24 @@ 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);

EventLog.LogEvent("PayPal Express Checkout", "CartCancelUrl=" + cartCancelUrl, EventLogSeverity.Information);
EventLog.LogEvent("PayPal Express Checkout", "CartReturnUrl=" + cartReturnUrl, EventLogSeverity.Information);

PaymentActionCodeType mode = PaymentActionCodeType.Authorization;
string mode = PayPalConstants.PAYMENT_MODE_AUTHORIZE;
if (!context.HccApp.CurrentStore.Settings.PayPal.ExpressAuthorizeOnly)
{
mode = PaymentActionCodeType.Sale;
mode = PayPalConstants.PAYMENT_MODE_CAPTURE;
}

// Accelerated boarding
if (string.IsNullOrWhiteSpace(context.HccApp.CurrentStore.Settings.PayPal.UserName))
{
mode = PaymentActionCodeType.Sale;
mode = PayPalConstants.PAYMENT_MODE_CAPTURE;
}

var solutionType = context.HccApp.CurrentStore.Settings.PayPal.RequirePayPalAccount ? SolutionTypeType.Mark : SolutionTypeType.Sole;
Expand All @@ -87,7 +93,7 @@ public override bool ProcessCheckout(OrderTaskContext context)

PaymentDetailsItemType[] itemsDetails = GetOrderItemsDetails(context);

SetExpressCheckoutResponseType expressResponse;
PayPalHttp.HttpResponse expressResponse;
if (addressSupplied)
{
Contacts.Address address = context.Order.ShippingAddress;
Expand All @@ -101,12 +107,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)
Expand All @@ -116,27 +124,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);
Expand All @@ -163,26 +171,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<Order>().Id;

// Recording of this info is handled on the paypal express
// checkout page instead of here.
Expand All @@ -193,38 +202,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<Order>().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 = PayPalConstants.LIVE_URL;
}
else
{
urlTemplate = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token={0}";
urlTemplate = PayPalConstants.SANDBOX_URL;
}
HttpContextBase httpContext = new HccHttpContextWrapper(HttpContext.Current);
httpContext.Response.Redirect(string.Format(urlTemplate, expressResponse.Token), true);
httpContext.Response.Redirect(string.Format(urlTemplate, expressResponse.Result<Order>().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;
}
Expand Down
1 change: 1 addition & 0 deletions Libraries/Hotcakes.Commerce/Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,6 @@ public class Constants
public const string TAG_IOGPRICEAMOUNT = "<meta property=\"og:price:amount\" content=\"{0}\" />";
public const string TAG_IOGPRICECURRENCY = "<meta property=\"og:price:currency\" content=\"{0}\" />";
#endregion

}
}
42 changes: 42 additions & 0 deletions Libraries/Hotcakes.Commerce/Common/PayPalConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;


namespace Hotcakes.Commerce.Common
{
public class PayPalConstants
{
//Payments Status
public const string PAYMENT_STATUS_COMPLETED = "COMPLETED";
public const string PAYMENT_STATUS_COMPLETED_DESCRIPTION = "PayPal Express Payment Captured Successfully";
public const string PAYMENT_STATUS_Pending = "PENDING";
public const string PAYMENT_STATUS_Pending_DESCRIPTION = "PayPal Express Payment PENDING";
public const string PAYMENT_STATUS_DENIED = "DENIED";
public const string PAYMENT_STATUS_DENIED_DESCRIPTION = "PayPal Express Payment DENIED";
public const string PAYMENT_STATUS_ERROR_DESCRIPTION =
"An error occurred while trying to capture your PayPal payment.";
public const string PAYMENT_CHARGE_ERROR = "An error occurred while trying to charge your PayPal payment.";
public const string PAYMENT_ERROR = "Paypal Express Payment Charge Failed.";

//Payment Authorize
public const string PAYMENT_AUTHORIZE_SUCCESS = "PayPal Express Payment Authorized Successfully.";
public const string PAYMENT_AUTHORIZE_FAILED = "PayPal Express Payment Authorization Failed.";


//Payment Refund
public const string PAYMENT_REFUND_SUCCESS = "PayPal Express Payment Refunded Successfully.";
public const string PAYMENT_REFUND_FAILED = "Paypal Express Payment Refund Failed.";

//Payment Void
public const string PAYMENT_Void_SUCCESS = "PayPal Express Payment Voided Successfully.";
public const string PAYMENT_Void_FAILED = "Paypal Express Payment Void Failed.";


//Payment Mode
public const string PAYMENT_MODE_AUTHORIZE = "AUTHORIZE";
public const string PAYMENT_MODE_CAPTURE = "CAPTURE";

//Template URL
public const string LIVE_URL = "https://www.paypal.com/checkoutnow?token={0}";
public const string SANDBOX_URL = "https://www.sandbox.paypal.com/checkoutnow?token={0}";
}
}
7 changes: 7 additions & 0 deletions Libraries/Hotcakes.Commerce/Hotcakes.Commerce.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\References\Dnn\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="PayPalCheckoutSdk, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\PayPalCheckoutSdk.1.0.4\lib\netstandard2.0\PayPalCheckoutSdk.dll</HintPath>
</Reference>
<Reference Include="PayPalHttp-Dotnet, Version=1.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\PayPalHttp.1.0.1\lib\netstandard2.0\PayPalHttp-Dotnet.dll</HintPath>
</Reference>
<Reference Include="paypal_base">
<HintPath>..\..\References\Misc\paypal_base.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -212,6 +218,7 @@
<Compile Include="Catalog\ResevationItem.cs" />
<Compile Include="Catalog\ValidateSelectionsResult.cs" />
<Compile Include="Common\Constants.cs" />
<Compile Include="Common\PayPalConstants.cs" />
<Compile Include="Configuration\IConfigurationManager.cs" />
<Compile Include="Configuration\SmtpSettings.cs" />
<Compile Include="Contacts\AffiliatePayment.cs" />
Expand Down
1 change: 1 addition & 0 deletions Libraries/Hotcakes.Commerce/Orders/OrderPaymentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Loading

0 comments on commit 9f3f49b

Please sign in to comment.