示例#1
0
        public TokenResponse CreateSession(ProcessPaymentRequest processPaymentRequest)
        {
            //Create a new credit session
            string sessionUri = _baseUrl + "/payments/v1/sessions";

            TokenRequest tokenRequest = new TokenRequest
            {
                Purchase_country  = processPaymentRequest.Order.BillingAddress.CountryCode,
                Purchase_currency = processPaymentRequest.CurrencyCode,
                Locale            = processPaymentRequest.LanuguageCode,
                Order_amount      = Convert.ToInt32(processPaymentRequest.OrderTotal * 100),
                Order_tax_amount  = 0,
                Order_lines       = processPaymentRequest.Order.Items.Select(p => new OrderLines
                {
                    Name                  = p.Name,
                    Reference             = p.StockCode,
                    Quantity              = p.Qty,
                    Unit_price            = Convert.ToInt32(p.Price.Raw.WithTax * 100),
                    Total_amount          = Convert.ToInt32(p.TotalPrice.Raw.WithTax * 100),
                    Type                  = LineType.physical.ToString(),
                    Quantity_unit         = "pcs",
                    Tax_rate              = (int)p.Price.Raw.Tax,
                    Total_discount_amount = 0,
                    Total_tax_amount      = (Convert.ToInt32(p.TotalPrice.Raw.WithTax * 100) / (Convert.ToInt32(p.TotalPrice.Raw.WithTax * 100) + (int)p.Price.Raw.Tax))
                }).ToList()
            };

            var shippingLine = new OrderLines
            {
                Name             = "Shipping",
                Reference        = "Shipping",
                Quantity         = 1,
                Unit_price       = Convert.ToInt32(processPaymentRequest.Order.ShippingCharge.Raw.WithTax * 100),
                Total_amount     = Convert.ToInt32(processPaymentRequest.Order.ShippingCharge.Raw.WithTax * 100),
                Type             = LineType.shipping_fee.ToString(),
                Tax_rate         = 0,
                Total_tax_amount = 0
            };

            tokenRequest.Order_lines.Add(shippingLine);

            var discountLine = new OrderLines
            {
                Name             = "Discount",
                Reference        = "Discount",
                Quantity         = 1,
                Unit_price       = -Convert.ToInt32(processPaymentRequest.Order.Discount.Raw.WithTax * 100),
                Total_amount     = -Convert.ToInt32(processPaymentRequest.Order.Discount.Raw.WithTax * 100),
                Type             = LineType.discount.ToString(),
                Tax_rate         = 0,
                Total_tax_amount = 0
            };

            tokenRequest.Order_lines.Add(discountLine);

            var settings = new JsonSerializerSettings();

            settings.ContractResolver  = new LowercaseContractResolver();
            settings.NullValueHandling = NullValueHandling.Ignore;

            // serialise json with lowercase property names and indented
            string tokenJson = JsonConvert.SerializeObject(tokenRequest, Formatting.Indented, settings);

            string response  = null;
            var    tokenResp = new TokenResponse();

            try
            {
                response = CallWS(sessionUri, tokenJson, "POST");
                // Parse JSON into dynamic object, convenient!
                JObject results = JObject.Parse(response);

                tokenResp.SessionId   = Convert.ToString(results[ResponseVariable.SessionId]);
                tokenResp.ClientToken = Convert.ToString(results[ResponseVariable.ClientToken]);
            }
            catch (Exception e)
            {
                throw e;
            }
            return(tokenResp);
        }
示例#2
0
        public OrderResponse CreateOrder(CheckoutModel processPaymentRequest, string authorizationToken, string orderId, string paymentId)
        {
            //Create a new credit session
            string createOrderUri = _baseUrl + "/payments/v1/authorizations/" + authorizationToken + "/order";

            TokenRequest tokenRequest = new TokenRequest
            {
                Purchase_country  = processPaymentRequest.BillingAddress.CountryCode,
                Purchase_currency = processPaymentRequest.CurrencyCode,
                Locale            = processPaymentRequest.LanuguageCode,
                Billing_address   = new Address
                {
                    Given_name      = processPaymentRequest.BillingAddress.FirstName,
                    Family_name     = processPaymentRequest.BillingAddress.LastName,
                    Email           = processPaymentRequest.Email,
                    Title           = processPaymentRequest.BillingAddress.Title,
                    Street_address  = processPaymentRequest.BillingAddress.Address1,
                    Street_address2 = processPaymentRequest.BillingAddress.Address2,
                    Postal_code     = processPaymentRequest.BillingAddress.PostCode,
                    City            = processPaymentRequest.BillingAddress.City,
                    Region          = processPaymentRequest.BillingAddress.State,
                    Phone           = processPaymentRequest.BillingAddress.PhoneNo,
                    Country         = processPaymentRequest.BillingAddress.CountryCode
                },
                Shipping_address = new Address
                {
                    Given_name      = processPaymentRequest.ShippingAddress.FirstName,
                    Family_name     = processPaymentRequest.ShippingAddress.LastName,
                    Email           = processPaymentRequest.Email,
                    Title           = processPaymentRequest.ShippingAddress.Title,
                    Street_address  = processPaymentRequest.ShippingAddress.Address1,
                    Street_address2 = processPaymentRequest.ShippingAddress.Address2,
                    Postal_code     = processPaymentRequest.ShippingAddress.PostCode,
                    City            = processPaymentRequest.ShippingAddress.City,
                    Region          = processPaymentRequest.ShippingAddress.State,
                    Phone           = processPaymentRequest.ShippingAddress.PhoneNo,
                    Country         = processPaymentRequest.ShippingAddress.CountryCode
                },
                Order_amount     = Convert.ToInt32(processPaymentRequest.BalanceAmount.Raw.WithTax * 100),
                Order_tax_amount = 0,
                Order_lines      = processPaymentRequest.Basket.LineItems.Select(p => new OrderLines
                {
                    Name                  = p.Name,
                    Reference             = p.StockCode,
                    Quantity              = p.Qty,
                    Unit_price            = Convert.ToInt32(p.Price.Raw.WithTax * 100),
                    Total_amount          = Convert.ToInt32(p.Price.Raw.WithTax * 100),
                    Type                  = LineType.physical.ToString(),
                    Quantity_unit         = "pcs",
                    Tax_rate              = (int)p.Price.Raw.Tax,
                    Total_discount_amount = 0,
                    Total_tax_amount      = (Convert.ToInt32(p.Price.Raw.WithTax * 100) / (Convert.ToInt32(p.Price.Raw.WithTax * 100) + (int)p.Price.Raw.Tax))
                }).ToList(),
                Merchant_reference1 = orderId
            };
            var shippingLine = new OrderLines
            {
                Name             = "Shipping",
                Reference        = "Shipping",
                Quantity         = 1,
                Unit_price       = Convert.ToInt32(processPaymentRequest.Basket.ShippingCharge.Raw.WithTax * 100),
                Total_amount     = Convert.ToInt32(processPaymentRequest.Basket.ShippingCharge.Raw.WithTax * 100),
                Type             = LineType.shipping_fee.ToString(),
                Tax_rate         = 0,
                Total_tax_amount = 0
            };

            tokenRequest.Order_lines.Add(shippingLine);
            var discountLine = new OrderLines
            {
                Name             = "Discount",
                Reference        = "Discount",
                Quantity         = 1,
                Unit_price       = -Convert.ToInt32(processPaymentRequest.Basket.Discount.Raw.WithTax * 100),
                Total_amount     = -Convert.ToInt32(processPaymentRequest.Basket.Discount.Raw.WithTax * 100),
                Type             = LineType.discount.ToString(),
                Tax_rate         = 0,
                Total_tax_amount = 0
            };

            tokenRequest.Order_lines.Add(discountLine);
            var settings = new JsonSerializerSettings();

            settings.ContractResolver  = new LowercaseContractResolver();
            settings.NullValueHandling = NullValueHandling.Ignore;

            // serialise json with lowercase property names and indented
            string tokenJson = JsonConvert.SerializeObject(tokenRequest, Formatting.Indented, settings);

            string response  = null;
            var    orderResp = new OrderResponse();

            try
            {
                response = CallWS(createOrderUri, tokenJson, "POST");
                // Parse JSON into dynamic object, convenient!
                JObject results = JObject.Parse(response);

                orderResp.OrderId     = Convert.ToString(results[ResponseVariable.OrderId]);
                orderResp.RedirectUrl = Convert.ToString(results[ResponseVariable.RedirectUrl]);
                orderResp.FraudStatus = Convert.ToString(results[ResponseVariable.FraudStatus]);
                orderResp.PaymentId   = paymentId;
                orderResp.OrderAmount = Convert.ToInt32(processPaymentRequest.BalanceAmount.Raw.WithTax * 100);
                orderResp.RefOrderId  = orderId;
            }
            catch (Exception e)
            {
                throw e;
            }
            return(orderResp);
        }