private SetExpressCheckoutRequestDetailsType SetupExpressCheckoutReqDetailsType(ICart cart, IPayment payment, string orderNumber) { var setExpressChkOutReqDetails = _payPalApiHelper.CreateExpressCheckoutReqDetailsType(payment, _paymentMethodConfiguration); // This key is sent to PayPal using https so it is not likely it will come from other because // only PayPal knows this key to send back to us var acceptSecurityKey = Utilities.GetAcceptUrlHashValue(orderNumber); var cancelSecurityKey = Utilities.GetCancelUrlHashValue(orderNumber); _acceptUrl = UriSupport.AbsoluteUrlBySettings(_paymentMethodConfiguration.SuccessUrl); _cancelUrl = UriSupport.AbsoluteUrlBySettings(_paymentMethodConfiguration.CancelUrl); var acceptUrl = UriSupport.AddQueryString(_acceptUrl, "accept", "true"); acceptUrl = UriSupport.AddQueryString(acceptUrl, "hash", acceptSecurityKey); var cancelUrl = UriSupport.AddQueryString(_cancelUrl, "accept", "false"); cancelUrl = UriSupport.AddQueryString(cancelUrl, "hash", cancelSecurityKey); setExpressChkOutReqDetails.CancelURL = cancelUrl; setExpressChkOutReqDetails.ReturnURL = acceptUrl; setExpressChkOutReqDetails.PaymentDetails = new List <PaymentDetailsType> { _payPalApiHelper.GetPaymentDetailsType(payment, cart, orderNumber, _acceptUrl) }; setExpressChkOutReqDetails.BillingAddress = AddressHandling.ToAddressType(payment.BillingAddress); return(setExpressChkOutReqDetails); }
public void UpdateOrderAddress_ShouldUpdateCorrectly() { var orderAddress = CreateOrderAddress(); var paypalAddress = AddressHandling.ToAddressType(orderAddress); paypalAddress.Street1 = "Street1"; AddressHandling.UpdateOrderAddress(orderAddress, CustomerAddressTypeEnum.Billing, paypalAddress, "*****@*****.**"); Assert.Equal("Street1", orderAddress.Line1); Assert.Equal("*****@*****.**", orderAddress.Email); }
public void ToAddressType_ShouldConvertCorrectly() { var orderAddress = CreateOrderAddress(); var result = AddressHandling.ToAddressType(orderAddress); Assert.Equal("Los Angeles", result.CityName); Assert.Equal(CountryCodeType.US, result.Country); Assert.Equal("FakeAddress", result.Street1); Assert.Equal("", result.Street2); Assert.Equal("90001", result.PostalCode); Assert.Equal("99999999", result.Phone); Assert.Equal("John Doe", result.Name); Assert.Equal("CA", result.StateOrProvince); }
/// <summary> /// Processes the successful transaction, was called when PayPal.com redirect back. /// </summary> /// <param name="orderGroup">The order group that was processed.</param> /// <param name="payment">The order payment.</param> /// <param name="acceptUrl">The redirect url when finished.</param> /// <param name="cancelUrl">The redirect url when error happens.</param> /// <returns>The url redirection after process.</returns> public string ProcessSuccessfulTransaction(IOrderGroup orderGroup, IPayment payment, string acceptUrl, string cancelUrl) { if (HttpContext.Current == null) { return(cancelUrl); } if (HttpContext.Current.Session != null) { HttpContext.Current.Session.Remove("LastCouponCode"); } if (!(orderGroup is ICart cart)) { // return to the shopping cart page immediately and show error messages return(ProcessUnsuccessfulTransaction(cancelUrl, Utilities.Translate("CommitTranErrorCartNull"))); } string redirectionUrl; using (var scope = new TransactionScope()) { SetSecurityProtocolToTls12(); var getDetailRequest = new GetExpressCheckoutDetailsRequestType { Token = payment.Properties[PayPalExpTokenPropertyName] as string // Add request-specific fields to the request. }; // Execute the API operation and obtain the response. var caller = PayPalApiHelper.GetPayPalApiCallerServices(_paymentMethodConfiguration); var getDetailsResponse = caller.GetExpressCheckoutDetails(new GetExpressCheckoutDetailsReq { GetExpressCheckoutDetailsRequest = getDetailRequest }); var errorCheck = _payPalApiHelper.CheckErrors(getDetailsResponse); if (!string.IsNullOrEmpty(errorCheck)) { RestoreSecurityProtocol(); // unsuccessful get detail call return(ProcessUnsuccessfulTransaction(cancelUrl, errorCheck)); } var expressCheckoutDetailsResponse = getDetailsResponse.GetExpressCheckoutDetailsResponseDetails; // get commerceOrderId from what we put to PayPal instead of getting from cookie payment.Properties[PayPalOrderNumberPropertyName] = expressCheckoutDetailsResponse.InvoiceID; //process details sent from paypal, changing addresses if required string emptyAddressMsg; //process billing address var payPalBillingAddress = expressCheckoutDetailsResponse.BillingAddress; if (payPalBillingAddress != null && AddressHandling.IsAddressChanged(payment.BillingAddress, payPalBillingAddress)) { emptyAddressMsg = _payPalApiHelper.ProcessOrderAddress(expressCheckoutDetailsResponse.PayerInfo, payPalBillingAddress, payment.BillingAddress, CustomerAddressTypeEnum.Billing, "CommitTranErrorPayPalBillingAddressEmpty"); if (!string.IsNullOrEmpty(emptyAddressMsg)) { RestoreSecurityProtocol(); return(ProcessUnsuccessfulTransaction(cancelUrl, emptyAddressMsg)); } } //process shipping address var payPalShippingAddress = expressCheckoutDetailsResponse.PaymentDetails[0].ShipToAddress; if (payPalShippingAddress != null && AddressHandling.IsAddressChanged(cart.GetFirstShipment().ShippingAddress, payPalShippingAddress)) { //when address was changed on PayPal site, it might cause changing tax value changed and changing order value also. var taxValueBefore = _taxCalculator.GetTaxTotal(cart, cart.Market, cart.Currency); var shippingAddress = orderGroup.CreateOrderAddress("address"); emptyAddressMsg = _payPalApiHelper.ProcessOrderAddress(expressCheckoutDetailsResponse.PayerInfo, payPalShippingAddress, shippingAddress, CustomerAddressTypeEnum.Shipping, "CommitTranErrorPayPalShippingAddressEmpty"); if (!string.IsNullOrEmpty(emptyAddressMsg)) { RestoreSecurityProtocol(); return(ProcessUnsuccessfulTransaction(cancelUrl, emptyAddressMsg)); } cart.GetFirstShipment().ShippingAddress = shippingAddress; var taxValueAfter = _taxCalculator.GetTaxTotal(cart, cart.Market, cart.Currency); if (taxValueBefore != taxValueAfter) { RestoreSecurityProtocol(); _orderRepository.Save(cart); // Saving cart to submit order address changed. scope.Complete(); return(ProcessUnsuccessfulTransaction(cancelUrl, Utilities.Translate("ProcessPaymentTaxValueChangedWarning"))); } } // Add request-specific fields to the request. // Create the request details object. var doExpressChkOutPaymentReqDetails = CreateExpressCheckoutPaymentRequest(getDetailsResponse, orderGroup, payment); // Execute the API operation and obtain the response. var doCheckOutResponse = caller.DoExpressCheckoutPayment(new DoExpressCheckoutPaymentReq { DoExpressCheckoutPaymentRequest = new DoExpressCheckoutPaymentRequestType(doExpressChkOutPaymentReqDetails) }); errorCheck = _payPalApiHelper.CheckErrors(doCheckOutResponse); if (!string.IsNullOrEmpty(errorCheck)) { RestoreSecurityProtocol(); // unsuccessful doCheckout response return(ProcessUnsuccessfulTransaction(cancelUrl, errorCheck)); } // everything is fine, this is a flag to tell ProcessPayment know about this case: redirect back from PayPal with accepted payment var errorMessages = new List <string>(); var cartCompleted = DoCompletingCart(cart, errorMessages); if (!cartCompleted) { RestoreSecurityProtocol(); return(UriSupport.AddQueryString(cancelUrl, "message", string.Join(";", errorMessages.Distinct().ToArray()))); } // Place order var purchaseOrder = MakePurchaseOrder(doCheckOutResponse, cart, payment); // Commit changes scope.Complete(); redirectionUrl = CreateRedirectionUrl(purchaseOrder, acceptUrl, payment.BillingAddress.Email); RestoreSecurityProtocol(); } _logger.Information($"PayPal transaction succeeds, redirect end user to {redirectionUrl}"); return(redirectionUrl); }