public void Exercise_14_2_Auth_Capture_Order_Payment() { var orderNumber = 11; //Create an Order resource. This resource is used to get, create, update orders var orderResource = new Mozu.Api.Resources.Commerce.OrderResource(_apiContext); var paymentResource = new Mozu.Api.Resources.Commerce.Orders.PaymentResource(_apiContext); var existingOrder = (orderResource.GetOrdersAsync(filter: "OrderNumber eq '" + orderNumber + "'").Result).Items[0]; Mozu.Api.Contracts.CommerceRuntime.Payments.Payment authorizedPayment = null; Mozu.Api.Contracts.CommerceRuntime.Payments.Payment pendingPayment = null; #region Add BillingInfo from Customer Object var customerResource = new Mozu.Api.Resources.Commerce.Customer.CustomerAccountResource(_apiContext); var customerAccount = customerResource.GetAccountAsync(1002).Result; var contactInfo = new Mozu.Api.Contracts.Core.Contact(); foreach (var contact in customerAccount.Contacts) { foreach (var type in contact.Types) { if (type.IsPrimary) { contactInfo.Address = contact.Address; contactInfo.CompanyOrOrganization = contact.CompanyOrOrganization; contactInfo.Email = contact.Email; contactInfo.FirstName = contact.FirstName; contactInfo.LastNameOrSurname = contact.LastNameOrSurname; contactInfo.MiddleNameOrInitial = contact.MiddleNameOrInitial; contactInfo.PhoneNumbers = contact.PhoneNumbers; } } } var billingInfo = new Mozu.Api.Contracts.CommerceRuntime.Payments.BillingInfo() { BillingContact = contactInfo, IsSameBillingShippingAddress = true, PaymentType = "Check", }; #endregion var action = new Mozu.Api.Contracts.CommerceRuntime.Payments.PaymentAction() { Amount = existingOrder.Total, CurrencyCode = "USD", InteractionDate = DateTime.Now, NewBillingInfo = billingInfo, ActionName = "CreatePayment", ReferenceSourcePaymentId = null, CheckNumber = "1234" }; try { authorizedPayment = existingOrder.Payments.FirstOrDefault(d => d.Status == "Authorized"); pendingPayment = existingOrder.Payments.FirstOrDefault(d => d.Status == "Pending"); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } if (authorizedPayment != null) { action.ActionName = "CapturePayment"; var capturedPayment = paymentResource.PerformPaymentActionAsync(action, existingOrder.Id, authorizedPayment.Id).Result; } else if (pendingPayment != null) { action.ActionName = "CapturePayment"; var capturedPayment = paymentResource.PerformPaymentActionAsync(action, existingOrder.Id, pendingPayment.Id).Result; } else { var authPayment = paymentResource.CreatePaymentActionAsync(action, existingOrder.Id).Result; } }
public void Exercise_14_2_Auth_Capture_Order_Payment() { var orderNumber = 12; //Create an Order resource. This resource is used to get, create, update orders var orderResource = new Mozu.Api.Resources.Commerce.OrderResource(_apiContext); var paymentResource = new Mozu.Api.Resources.Commerce.Orders.PaymentResource(_apiContext); var existingOrder = (orderResource.GetOrdersAsync(filter: "OrderNumber eq '" + orderNumber + "'").Result).Items[0]; Mozu.Api.Contracts.CommerceRuntime.Payments.Payment authorizedPayment = null; Mozu.Api.Contracts.CommerceRuntime.Payments.Payment pendingPayment = null; #region Add BillingInfo from Customer Object var customerResource = new Mozu.Api.Resources.Commerce.Customer.CustomerAccountResource(_apiContext); var customerAccount = customerResource.GetAccountAsync(1002).Result; var contactInfo = new Mozu.Api.Contracts.Core.Contact(); foreach (var contact in customerAccount.Contacts) { foreach (var type in contact.Types) { if (type.IsPrimary) { contactInfo.Address = contact.Address; contactInfo.CompanyOrOrganization = contact.CompanyOrOrganization; contactInfo.Email = contact.Email; contactInfo.FirstName = contact.FirstName; contactInfo.LastNameOrSurname = contact.LastNameOrSurname; contactInfo.MiddleNameOrInitial = contact.MiddleNameOrInitial; contactInfo.PhoneNumbers = contact.PhoneNumbers; } } } var billingInfo = new Mozu.Api.Contracts.CommerceRuntime.Payments.BillingInfo() { BillingContact = contactInfo, IsSameBillingShippingAddress = true, PaymentType = "Check", }; #endregion var action = new Mozu.Api.Contracts.CommerceRuntime.Payments.PaymentAction() { Amount = existingOrder.Total, CurrencyCode = "USD", InteractionDate = DateTime.Now, NewBillingInfo = billingInfo, ActionName = "CreatePayment", ReferenceSourcePaymentId = null, CheckNumber = "1234" }; try { authorizedPayment = existingOrder.Payments.FirstOrDefault(d => d.Status == "Authorized"); pendingPayment = existingOrder.Payments.FirstOrDefault(d => d.Status == "Pending"); } catch(Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } if(authorizedPayment != null) { action.ActionName = "CapturePayment"; var capturedPayment = paymentResource.PerformPaymentActionAsync(action, existingOrder.Id, authorizedPayment.Id).Result; } else if(pendingPayment != null) { action.ActionName = "CapturePayment"; var capturedPayment = paymentResource.PerformPaymentActionAsync(action, existingOrder.Id, pendingPayment.Id).Result; } else { var authPayment = paymentResource.CreatePaymentActionAsync(action, existingOrder.Id).Result; } }