Skip to content

Commit a3d5797

Browse files
committed
PayPal PLUS: Integration review through PayPal (part 2)
1 parent 6cf721e commit a3d5797

4 files changed

Lines changed: 37 additions & 31 deletions

File tree

src/Plugins/SmartStore.PayPal/Controllers/PayPalPlusController.cs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -288,38 +288,40 @@ public ActionResult PaymentWall()
288288

289289
model.ThirdPartyFees = sb.ToString();
290290

291-
if (session.PaymentId.IsEmpty() || session.ApprovalUrl.IsEmpty())
291+
// we must create a new paypal payment each time the payment wall is rendered because otherwise patch payment can fail
292+
// with "Item amount must add up to specified amount subtotal (or total if amount details not specified)".
293+
session.PaymentId = null;
294+
session.ApprovalUrl = null;
295+
296+
var result = PayPalService.EnsureAccessToken(session, settings);
297+
if (result.Success)
292298
{
293-
var result = PayPalService.EnsureAccessToken(session, settings);
294-
if (result.Success)
295-
{
296-
var protocol = (store.SslEnabled ? "https" : "http");
297-
var returnUrl = Url.Action("CheckoutReturn", "PayPalPlus", new { area = Plugin.SystemName }, protocol);
298-
var cancelUrl = Url.Action("CheckoutCancel", "PayPalPlus", new { area = Plugin.SystemName }, protocol);
299+
var protocol = (store.SslEnabled ? "https" : "http");
300+
var returnUrl = Url.Action("CheckoutReturn", "PayPalPlus", new { area = Plugin.SystemName }, protocol);
301+
var cancelUrl = Url.Action("CheckoutCancel", "PayPalPlus", new { area = Plugin.SystemName }, protocol);
299302

300-
result = PayPalService.CreatePayment(settings, session, cart, PayPalPlusProvider.SystemName, returnUrl, cancelUrl);
301-
if (result.Success && result.Json != null)
303+
result = PayPalService.CreatePayment(settings, session, cart, PayPalPlusProvider.SystemName, returnUrl, cancelUrl);
304+
if (result.Success && result.Json != null)
305+
{
306+
foreach (var link in result.Json.links)
302307
{
303-
foreach (var link in result.Json.links)
308+
if (((string)link.rel).IsCaseInsensitiveEqual("approval_url"))
304309
{
305-
if (((string)link.rel).IsCaseInsensitiveEqual("approval_url"))
306-
{
307-
session.PaymentId = result.Id;
308-
session.ApprovalUrl = link.href;
309-
break;
310-
}
310+
session.PaymentId = result.Id;
311+
session.ApprovalUrl = link.href;
312+
break;
311313
}
312314
}
313-
else
314-
{
315-
model.ErrorMessage = result.ErrorMessage;
316-
}
317315
}
318316
else
319317
{
320318
model.ErrorMessage = result.ErrorMessage;
321319
}
322320
}
321+
else
322+
{
323+
model.ErrorMessage = result.ErrorMessage;
324+
}
323325

324326
model.ApprovalUrl = session.ApprovalUrl;
325327

src/Plugins/SmartStore.PayPal/Description.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Description: Provides the PayPal payment methods PayPal Standard, PayPal Direct, PayPal Express and PayPal PLUS.
33
SystemName: SmartStore.PayPal
44
Group: Payment
5-
Version: 2.6.0.2
5+
Version: 2.6.0.3
66
MinAppVersion: 2.5.0
77
DisplayOrder: 1
88
FileName: SmartStore.PayPal.dll

src/Plugins/SmartStore.PayPal/Services/PayPalService.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class PayPalService : IPayPalService
4545
private readonly IPaymentService _paymentService;
4646
private readonly IPriceCalculationService _priceCalculationService;
4747
private readonly ITaxService _taxService;
48-
private readonly Lazy<ICurrencyService> _currencyService;
48+
private readonly ICurrencyService _currencyService;
4949
private readonly Lazy<IPictureService> _pictureService;
5050
private readonly Lazy<CompanyInformationSettings> _companyInfoSettings;
5151

@@ -59,7 +59,7 @@ public PayPalService(
5959
IPaymentService paymentService,
6060
IPriceCalculationService priceCalculationService,
6161
ITaxService taxService,
62-
Lazy<ICurrencyService> currencyService,
62+
ICurrencyService currencyService,
6363
Lazy<IPictureService> pictureService,
6464
Lazy<CompanyInformationSettings> companyInfoSettings)
6565
{
@@ -126,6 +126,7 @@ private Dictionary<string, object> CreateAmount(
126126
var amount = new Dictionary<string, object>();
127127
var amountDetails = new Dictionary<string, object>();
128128
var language = _services.WorkContext.WorkingLanguage;
129+
var currency = _services.WorkContext.WorkingCurrency;
129130
var currencyCode = store.PrimaryStoreCurrency.CurrencyCode;
130131
var includingTax = (_services.WorkContext.GetTaxDisplayTypeFor(customer, store.Id) == TaxDisplayType.IncludingTax);
131132

@@ -138,7 +139,9 @@ private Dictionary<string, object> CreateAmount(
138139

139140
var shipping = (_orderTotalCalculationService.GetShoppingCartShippingTotal(cart) ?? decimal.Zero);
140141

141-
var paymentFee = _paymentService.GetAdditionalHandlingFee(cart, providerSystemName);
142+
var additionalHandlingFee = _paymentService.GetAdditionalHandlingFee(cart, providerSystemName);
143+
var paymentFeeBase = _taxService.GetPaymentMethodAdditionalFee(additionalHandlingFee, customer);
144+
var paymentFee = _currencyService.ConvertFromPrimaryStoreCurrency(paymentFeeBase, currency);
142145

143146
var total = (_orderTotalCalculationService.GetShoppingCartTotal(cart, out orderDiscountInclTax, out orderAppliedDiscount, out appliedGiftCards,
144147
out redeemedRewardPoints, out redeemedRewardPointsAmount) ?? decimal.Zero);
@@ -150,7 +153,7 @@ private Dictionary<string, object> CreateAmount(
150153
decimal unitPrice = _priceCalculationService.GetUnitPrice(item, true);
151154
decimal productPrice = _taxService.GetProductPrice(item.Item.Product, unitPrice, includingTax, customer, out unitPriceTaxRate);
152155

153-
if (items != null)
156+
if (items != null && productPrice != decimal.Zero)
154157
{
155158
var line = new Dictionary<string, object>();
156159
line.Add("quantity", item.Item.Quantity);
@@ -168,18 +171,19 @@ private Dictionary<string, object> CreateAmount(
168171

169172
if (total != itemsPlusMisc)
170173
{
171-
if (items != null)
174+
var otherAmount = (total - itemsPlusMisc);
175+
totalOrderItems += otherAmount;
176+
177+
if (items != null && otherAmount != decimal.Zero)
172178
{
173179
// e.g. discount applied to cart total
174180
var line = new Dictionary<string, object>();
175181
line.Add("quantity", "1");
176182
line.Add("name", T("Plugins.SmartStore.PayPal.Other").Text.Truncate(127));
177-
line.Add("price", (total - itemsPlusMisc).FormatInvariant());
183+
line.Add("price", otherAmount.FormatInvariant());
178184
line.Add("currency", currencyCode);
179185
items.Add(line);
180186
}
181-
182-
totalOrderItems += (total - itemsPlusMisc);
183187
}
184188

185189
// fill amount object
@@ -417,7 +421,7 @@ public string CreatePaymentInstruction(PayPalPaymentInstruction instruct)
417421

418422
try
419423
{
420-
var currency = _currencyService.Value.GetCurrencyByCode(instruct.AmountCurrencyCode);
424+
var currency = _currencyService.GetCurrencyByCode(instruct.AmountCurrencyCode);
421425
var format = (currency != null && currency.CustomFormatting.HasValue() ? currency.CustomFormatting : "C");
422426

423427
amount = instruct.Amount.ToString(format, culture);

src/Plugins/SmartStore.PayPal/changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Release Notes
22

3-
##Paypal 2.6.0.2
3+
##Paypal 2.6.0.3
44
###Bugfixes
55
* PayPal PLUS: Integration review through PayPal
66

0 commit comments

Comments
 (0)