Exemplo n.º 1
0
        /// <summary>
        /// Process a payment
        /// </summary>
        /// <param name="processPaymentRequest">Payment info required for an order processing</param>
        /// <returns>Process payment result</returns>
        public ProcessPaymentResult ProcessPayment(ProcessPaymentRequest processPaymentRequest)
        {
            var result = new ProcessPaymentResult();

            var eWaygateway = new GatewayConnector();

            var eWayRequest = new GatewayRequest
            {
                EwayCustomerID  = _eWayPaymentSettings.CustomerId,
                CardNumber      = processPaymentRequest.CreditCardNumber,
                CardExpiryMonth = processPaymentRequest.CreditCardExpireMonth.ToString("D2"),
                CardExpiryYear  = processPaymentRequest.CreditCardExpireYear.ToString(),
                CardHolderName  = processPaymentRequest.CreditCardName,
                InvoiceAmount   = Convert.ToInt32(processPaymentRequest.OrderTotal * 100)
            };

            //Integer

            var customer       = _customerService.GetCustomerById(processPaymentRequest.CustomerId);
            var billingAddress = customer.BillingAddress;

            eWayRequest.PurchaserFirstName    = billingAddress.FirstName;
            eWayRequest.PurchaserLastName     = billingAddress.LastName;
            eWayRequest.PurchaserEmailAddress = billingAddress.Email;
            eWayRequest.PurchaserAddress      = billingAddress.Address1;
            eWayRequest.PurchaserPostalCode   = billingAddress.ZipPostalCode;
            eWayRequest.InvoiceReference      = processPaymentRequest.OrderGuid.ToString();
            eWayRequest.InvoiceDescription    = _storeContext.CurrentStore.Name + ". Order #" + processPaymentRequest.OrderGuid;
            eWayRequest.TransactionNumber     = processPaymentRequest.OrderGuid.ToString();
            eWayRequest.CVN         = processPaymentRequest.CreditCardCvv2;
            eWayRequest.EwayOption1 = string.Empty;
            eWayRequest.EwayOption2 = string.Empty;
            eWayRequest.EwayOption3 = string.Empty;

            // Do the payment, send XML doc containing information gathered
            eWaygateway.Uri = GeteWayUrl();
            var eWayResponse = eWaygateway.ProcessRequest(eWayRequest);

            if (eWayResponse != null)
            {
                // Payment succeeded get values returned
                if (eWayResponse.Status && (eWayResponse.Error.StartsWith(APPROVED_RESPONSE) || eWayResponse.Error.StartsWith(HONOUR_RESPONSE)))
                {
                    result.AuthorizationTransactionCode   = eWayResponse.AuthorisationCode;
                    result.AuthorizationTransactionResult = eWayResponse.InvoiceReference;
                    result.AuthorizationTransactionId     = eWayResponse.TransactionNumber;
                    result.NewPaymentStatus = PaymentStatus.Paid;
                    //processPaymentResult.AuthorizationDate = DateTime.UtcNow;
                }
                else
                {
                    result.AddError("An invalid response was recieved from the payment gateway." + eWayResponse.Error);
                    //full error: eWAYRequest.ToXml().ToString()
                }
            }
            else
            {
                // invalid response recieved from server.
                result.AddError("An invalid response was recieved from the payment gateway.");
                //full error: eWAYRequest.ToXml().ToString()
            }

            return(result);
        }