private ProcessOrderResponse ProcessPayment(IVippsUserDetails vippsUserDetails, IVippsPaymentDetails paymentDetails, string orderId, ICart cart) { ProcessOrderResponse response; var payment = cart.GetFirstPayment(x => x.IsVippsPayment() && x.TransactionID == orderId && x.TransactionType.Equals(nameof(TransactionType.Authorization))); if (TransactionSuccess(paymentDetails)) { response = HandleSuccess(cart, payment, paymentDetails, vippsUserDetails, orderId); } else if (TransactionCancelled(paymentDetails)) { response = HandleCancelled(cart, payment, paymentDetails, orderId); } else if (TransactionFailed(paymentDetails)) { response = HandleFailed(cart, payment, paymentDetails, orderId); } else { response = new ProcessOrderResponse { ProcessResponseErrorType = ProcessResponseErrorType.OTHER, ErrorMessage = $"No action taken on order id: {orderId}." }; } return(EnsurePaymentType(response, cart)); }
private static void EnsureBillingAddress(IPayment payment, ICart cart, IVippsUserDetails details) { if (string.IsNullOrEmpty(payment.BillingAddress?.Id)) { payment.BillingAddress = AddressHelper.UserDetailsAndShippingDetailsToOrderAddress(details.UserDetails, details.ShippingDetails, cart); } }
private ProcessOrderResponse CheckDependenciesThenProcessPayment(IVippsUserDetails vippsUserDetails, IVippsPaymentDetails paymentDetails, string orderId, Guid contactId, string marketId, string cartName) { var purchaseOrderResponse = EnsureNoPurchaseOrder(orderId); if (purchaseOrderResponse != null) { return(purchaseOrderResponse); } return(GetCartThenProcessPayment(vippsUserDetails, paymentDetails, orderId, contactId, marketId, cartName)); }
public void EnsureExpressPaymentAndShipping(ICart cart, IPayment payment, IVippsUserDetails userDetails) { if (cart.GetFirstShipment().ShippingMethodId == default(Guid) || cart.GetFirstShipment().ShippingAddress == null || payment?.BillingAddress == null) { EnsureShipping(cart, userDetails); EnsureBillingAddress(payment, cart, userDetails); _orderRepository.Save(cart); } }
private ProcessOrderResponse GetCartThenProcessPayment(IVippsUserDetails vippsUserDetails, IVippsPaymentDetails paymentDetails, string orderId, Guid contactId, string marketId, string cartName) { var cart = _vippsService.GetCartByContactId(contactId, marketId, cartName); var errorResponse = ValidateCartAndSetProcessing(orderId, cart); if (errorResponse != null) { return(errorResponse); } return(ProcessPayment(vippsUserDetails, paymentDetails, orderId, cart)); }
private static void EnsureShipping(ICart cart, IVippsUserDetails details) { var shipment = cart.GetFirstShipment(); if (shipment.ShippingMethodId == default(Guid)) { if (details?.ShippingDetails?.ShippingMethodId != null) { shipment.ShippingMethodId = new Guid(details.ShippingDetails.ShippingMethodId); } } if (string.IsNullOrEmpty(shipment.ShippingAddress?.Id)) { shipment.ShippingAddress = AddressHelper.UserDetailsAndShippingDetailsToOrderAddress(details?.UserDetails, details?.ShippingDetails, cart); } }
private static void EnsurePayment(IPayment payment, ICart cart, IVippsPaymentDetails paymentDetails, IVippsUserDetails details) { if (string.IsNullOrEmpty(payment.BillingAddress?.Id)) { payment.BillingAddress = AddressHelper.UserDetailsAndShippingDetailsToOrderAddress(details.UserDetails, details.ShippingDetails, cart); payment.Amount = paymentDetails.Amount.FormatAmountFromVipps(); } }
private void EnsureExpressPaymentAndShipping(ICart cart, IPayment payment, IVippsPaymentDetails paymentDetails, IVippsUserDetails userDetails) { EnsureShipping(cart, userDetails); EnsurePayment(payment, cart, paymentDetails, userDetails); }
private ProcessOrderResponse HandleSuccess(ICart cart, IPayment payment, IVippsPaymentDetails paymentDetails, IVippsUserDetails userDetails, string orderId) { if (payment == null) { OrderNoteHelper.AddNoteAndSaveChanges(cart, "Cancel", $"No vipps payment found for vipps order id {orderId}. Canceling payment"); PaymentHelper.CancelPayment(cart, paymentDetails.Amount, orderId); return(new ProcessOrderResponse { ErrorMessage = $"No vipps payment found for vipps order id {orderId}.", ProcessResponseErrorType = ProcessResponseErrorType.NOVIPPSPAYMENTINCART }); } EnsureExpressPaymentAndShipping(cart, payment, paymentDetails, userDetails); payment.Status = PaymentStatus.Processed.ToString(); AddNote(cart, payment, orderId, paymentDetails); var loadOrCreatePurchaseOrderResponse = CreatePurchaseOrder(cart); if (loadOrCreatePurchaseOrderResponse.PurchaseOrder != null) { return(loadOrCreatePurchaseOrderResponse); } PaymentHelper.CancelPayment(cart, payment); return(loadOrCreatePurchaseOrderResponse); }
private ProcessOrderResponse ProcessOrder(IVippsUserDetails vippsUserDetails, IVippsPaymentDetails paymentDetails, string orderId, ICart cart) { var lockInfo = GetLock(orderId, cart); return(ProcessLocked(lockInfo, () => ProcessPayment(vippsUserDetails, paymentDetails, orderId, cart))); }
private ProcessOrderResponse ProcessOrder(IVippsUserDetails vippsUserDetails, IVippsPaymentDetails paymentDetails, string orderId, Guid contactId, string marketId, string cartName) { var lockInfo = GetLock(orderId, contactId, marketId, cartName); return(ProcessLocked(lockInfo, () => CheckDependenciesThenProcessPayment(vippsUserDetails, paymentDetails, orderId, contactId, marketId, cartName))); }
private async Task <ProcessOrderResponse> ProcessOrder(IVippsUserDetails vippsUserDetails, IVippsPaymentDetails paymentDetails, string orderId, Guid contactId, string marketId, string cartName) { var readLock = _synchronizer.Get(orderId); try { await readLock.WaitAsync(); var purchaseOrder = _vippsService.GetPurchaseOrderByOrderId(orderId); if (purchaseOrder != null) { return(new ProcessOrderResponse { PurchaseOrder = purchaseOrder }); } var cart = _vippsService.GetCartByContactId(contactId, marketId, cartName); if (cart == null) { _logger.Warning($"No cart found for vipps order id {orderId}"); return(new ProcessOrderResponse { ProcessResponseErrorType = ProcessResponseErrorType.NOCARTFOUND, ErrorMessage = $"No cart found for vipps order id {orderId}" }); } var payment = cart.GetFirstForm().Payments.FirstOrDefault(x => x.IsVippsPayment() && x.TransactionID == orderId && x.TransactionType.Equals(TransactionType.Authorization.ToString())); if (TransactionSuccess(paymentDetails)) { return(await HandleSuccess(cart, payment, paymentDetails, vippsUserDetails, orderId)); } if (TransactionCancelled(paymentDetails)) { return(HandleCancelled(cart, payment, paymentDetails, orderId)); } if (TransactionFailed(paymentDetails)) { return(HandleFailed(cart, payment, paymentDetails, orderId)); } return(new ProcessOrderResponse { ProcessResponseErrorType = ProcessResponseErrorType.OTHER, ErrorMessage = $"No action taken on order id: {orderId}." }); } catch (Exception ex) { _logger.Error(ex.Message, ex); return(new ProcessOrderResponse { ErrorMessage = ex.Message, ProcessResponseErrorType = ProcessResponseErrorType.EXCEPTION }); } finally { readLock.Release(); if (readLock.CurrentCount > 0) { _synchronizer.Remove(orderId); } } }