コード例 #1
0
ファイル: PaymentService.cs プロジェクト: wijayakoon/Unixmo
        public PaymentResponse ProcessPaymentForCart(Cart cart, string cancelUrl, string returnUrl)
        {
            var apiContext = ApiContextFactory.Create();

            var paypalItems = new ItemList
            {
                items = cart.GetItems().OfType<ProductCartItem>().Select(li => new Item
                {
                    name = li.Title,
                    price = li.PriceWithoutTax.ToString("N"),
                    currency = _config.Currency,
                    quantity = li.Quantity.ToString(),
                    sku = li.Id.ToString()
                }).ToList()
            };

            // ###Payer
            // A resource representing a Payer that funds a payment
            // Payment Method
            // as `paypal`
            var payer = new Payer { payment_method = "paypal" };

            //// # Redirect URLS
            var redirUrls = new RedirectUrls
            {
                cancel_url = cancelUrl,
                return_url = returnUrl
            };

            // ###Details
            // Let's you specify details of a payment amount.
            var details = new Details
            {
                tax = cart.CalculateTotalTax().TaxAmount.ToString("N"),
                shipping = cart.ShippingCost.Cost.ToString("N"),
                subtotal = cart.CalculateSubTotalWithoutTax().ToString("N"),
            };

            // ###Amount
            // Let's you specify a payment amount.
            var amount = new Amount
            {
                currency = _config.Currency,
                total = cart.CalculateGrandTotal().ToString("N"), // Total must be equal to sum of shipping, tax and subtotal.
                details = details
            };

            // ###Transaction
            // A transaction defines the contract of a
            // payment - what is the payment for and who
            // is fulfilling it.
            var transactionList = new List<Transaction>
            {
                new Transaction
                {
                    description = "UNIXMO Online Store",
                    amount = amount,
                    item_list = paypalItems
                }
            };

            // The Payment creation API requires a list of
            // Transaction; add the created `Transaction`
            // to a List

            // ###Payment
            // A Payment Resource; create one using
            // the above types and intent as `sale` or `authorize`
            var payment = new Payment
            {
                intent = "sale",
                payer = payer,
                transactions = transactionList,
                redirect_urls = redirUrls
            };

            var myResponse = payment.Create(apiContext);

            if (myResponse == null || myResponse.links == null)
            {
                return PaymentResponse.Failed();
            }

            var approvalUrl = myResponse.links.FirstOrDefault(lnk => lnk.rel.Equals("approval_url", StringComparison.OrdinalIgnoreCase));
            if (approvalUrl == null)
                return PaymentResponse.Failed();

            cart.SetPaymentTransaction(myResponse.id);

            return PaymentResponse.Completed(approvalUrl.href, myResponse.id);
        }
コード例 #2
0
ファイル: Order.cs プロジェクト: wijayakoon/Unixmo
        public static Order FromShoppingCart(Cart shoppingCart)
        {
            var orderGuid = Guid.NewGuid();

            // Create new one from shopping cart, ready for Db
            var order = new Order
            {
                GUID = orderGuid,
                ShippingDetails = shoppingCart.ShippingDetails,
                BillingDetails = shoppingCart.BillingDetails,
                HandlingTotal = 0,
                ShippingTotal = shoppingCart.ShippingCost,
                TaxTotal = shoppingCart.CalculateTotalTax(),
                TotalAmount = shoppingCart.CalculateSubTotalWithoutTax(),
                SourceIpAddress = shoppingCart.SourceIpAddress,
                OrderLineItems = shoppingCart.GetItems().Select(OrderLineItem.FromShoppingCartItem).ToArray(),
                UserName = shoppingCart.UserName,
                CreatedDateTime = DateTime.Now,
                PaymentTransactionId = shoppingCart.PaymentTransactionId,
                InvoiceNumber = shoppingCart.InvoiceNumber
            };

            return order;
        }