public ActionResult PaymentWithCreditCard()
        {
            //create and item for which you are taking payment
            //if you need to add more items in the list
            //Then you will need to create multiple item objects or use some loop to instantiate object
            Item item = new Item();

            item.name     = "Demo Item";
            item.currency = "USD";
            item.price    = "5";
            item.quantity = "1";
            item.sku      = "sku";

            //Now make a List of Item and add the above item to it
            //you can create as many items as you want and add to this list
            List <Item> itms = new List <Item>();

            itms.Add(item);
            ItemList itemList = new ItemList();

            itemList.items = itms;

            //Address for the payment
            Address billingAddress = new Address();

            billingAddress.city         = "NewYork";
            billingAddress.country_code = "US";
            billingAddress.line1        = "23rd street kew gardens";
            billingAddress.postal_code  = "43210";
            billingAddress.state        = "NY";


            //Now Create an object of credit card and add above details to it
            CreditCard crdtCard = new CreditCard();

            crdtCard.billing_address = billingAddress;
            crdtCard.cvv2            = "874";
            crdtCard.expire_month    = 1;
            crdtCard.expire_year     = 2020;
            crdtCard.first_name      = "Aman";
            crdtCard.last_name       = "Thakur";
            crdtCard.number          = "1234567890123456";
            crdtCard.type            = "discover";

            // Specify details of your payment amount.
            Details details = new Details();

            details.shipping = "1";
            details.subtotal = "5";
            details.tax      = "1";

            // Specify your total payment amount and assign the details object
            Amount amnt = new Amount();

            amnt.currency = "USD";
            // Total = shipping tax + subtotal.
            amnt.total   = "7";
            amnt.details = details;

            // Now make a trasaction object and assign the Amount object
            Transaction tran = new Transaction();

            tran.amount         = amnt;
            tran.description    = "Description about the payment amount.";
            tran.item_list      = itemList;
            tran.invoice_number = "your invoice number which you are generating";

            // Now, we have to make a list of trasaction and add the trasactions object
            // to this list. You can create one or more object as per your requirements

            List <Transaction> transactions = new List <Transaction>();

            transactions.Add(tran);

            // Now we need to specify the FundingInstrument of the Payer
            // for credit card payments, set the CreditCard which we made above

            FundingInstrument fundInstrument = new FundingInstrument();

            fundInstrument.credit_card = crdtCard;

            // The Payment creation API requires a list of FundingIntrument

            List <FundingInstrument> fundingInstrumentList = new List <FundingInstrument>();

            fundingInstrumentList.Add(fundInstrument);

            // Now create Payer object and assign the fundinginstrument list to the object
            Payer payr = new Payer();

            payr.funding_instruments = fundingInstrumentList;
            payr.payment_method      = "credit_card";

            // finally create the payment object and assign the payer object & transaction list to it
            Payment pymnt = new Payment();

            pymnt.intent       = "sale";
            pymnt.payer        = payr;
            pymnt.transactions = transactions;

            try
            {
                //getting context from the paypal, basically we are sending the clientID and clientSecret key in this function
                //to the get the context from the paypal API to make the payment for which we have created the object above.

                //Code for the configuration class is provided next

                // Basically, apiContext has a accesstoken which is sent by the paypal to authenticate the payment to facilitator account. An access token could be an alphanumeric string

                APIContext apiContext = Configuration.GetAPIContext();

                // Create is a Payment class function which actually sends the payment details to the paypal API for the payment. The function is passed with the ApiContext which we received above.

                Payment createdPayment = pymnt.Create(apiContext);

                //if the createdPayment.State is "approved" it means the payment was successfull else not

                if (createdPayment.state.ToLower() != "approved")
                {
                    return(View("FailureView"));
                }
            }
            catch (PayPal.PayPalException ex)
            {
                Logger.Log("Error: " + ex.Message);
                return(View("FailureView"));
            }

            return(View("SuccessView"));
        }
        // ##Create
        // Sample showing to create a Payment using
        // CreditCard as a FundingInstrument
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpContext CurrContext = HttpContext.Current;

            // ###Items
            // Items within a transaction.
            Item item = new Item();

            item.name     = "Item Name";
            item.currency = "USD";
            item.price    = "1";
            item.quantity = "5";
            item.sku      = "sku";

            List <Item> itms = new List <Item>();

            itms.Add(item);
            ItemList itemList = new ItemList();

            itemList.items = itms;

            // ###Address
            // Base Address object used as shipping or billing
            // address in a payment.
            Address billingAddress = new Address();

            billingAddress.city         = "Johnstown";
            billingAddress.country_code = "US";
            billingAddress.line1        = "52 N Main ST";
            billingAddress.postal_code  = "43210";
            billingAddress.state        = "OH";

            // ###CreditCard
            // A resource representing a credit card that can be
            // used to fund a payment.
            CreditCard crdtCard = new CreditCard();

            crdtCard.billing_address = billingAddress;
            crdtCard.cvv2            = 874;
            crdtCard.expire_month    = 11;
            crdtCard.expire_year     = 2018;
            crdtCard.first_name      = "Joe";
            crdtCard.last_name       = "Shopper";
            crdtCard.number          = "4417119669820331";
            crdtCard.type            = "visa";

            // ###Details
            // Let's you specify details of a payment amount.
            Details details = new Details();

            details.shipping = "1";
            details.subtotal = "5";
            details.tax      = "1";

            // ###Amount
            // Let's you specify a payment amount.
            Amount amnt = new Amount();

            amnt.currency = "USD";
            // Total must be equal to sum of shipping, tax and subtotal.
            amnt.total   = "7";
            amnt.details = details;

            // ###Transaction
            // A transaction defines the contract of a
            // payment - what is the payment for and who
            // is fulfilling it.
            Transaction tran = new Transaction();

            tran.amount      = amnt;
            tran.description = "This is the payment transaction description.";
            tran.item_list   = itemList;

            // The Payment creation API requires a list of
            // Transaction; add the created `Transaction`
            // to a List
            List <Transaction> transactions = new List <Transaction>();

            transactions.Add(tran);

            // ###FundingInstrument
            // A resource representing a Payer's funding instrument.
            // For direct credit card payments, set the CreditCard
            // field on this object.
            FundingInstrument fundInstrument = new FundingInstrument();

            fundInstrument.credit_card = crdtCard;

            // The Payment creation API requires a list of
            // FundingInstrument; add the created `FundingInstrument`
            // to a List
            List <FundingInstrument> fundingInstrumentList = new List <FundingInstrument>();

            fundingInstrumentList.Add(fundInstrument);

            // ###Payer
            // A resource representing a Payer that funds a payment
            // Use the List of `FundingInstrument` and the Payment Method
            // as `credit_card`
            Payer payr = new Payer();

            payr.funding_instruments = fundingInstrumentList;
            payr.payment_method      = "credit_card";

            // ###Payment
            // A Payment Resource; create one using
            // the above types and intent as `sale` or `authorize`
            Payment pymnt = new Payment();

            pymnt.intent       = "sale";
            pymnt.payer        = payr;
            pymnt.transactions = transactions;

            try
            {
                // ### Api Context
                // Pass in a `APIContext` object to authenticate
                // the call and to send a unique request id
                // (that ensures idempotency). The SDK generates
                // a request id if you do not pass one explicitly.
                // See [Configuration.cs](/Source/Configuration.html) to know more about APIContext..
                APIContext apiContext = Configuration.GetAPIContext();

                // Create a payment using a valid APIContext
                Payment createdPayment = pymnt.Create(apiContext);
                CurrContext.Items.Add("ResponseJson", JObject.Parse(createdPayment.ConvertToJson()).ToString(Formatting.Indented));
            }
            catch (PayPal.Exception.PayPalException ex)
            {
                CurrContext.Items.Add("Error", ex.Message);
            }

            CurrContext.Items.Add("RequestJson", JObject.Parse(pymnt.ConvertToJson()).ToString(Formatting.Indented));

            Server.Transfer("~/Response.aspx");
        }
        public void FundingInstrumentConstructorTest()
        {
            FundingInstrument target = new FundingInstrument();

            Assert.IsNotNull(target);
        }
예제 #4
0
        public ActionResult PaymentWithCreditCard()
        {
            Item item = new Item();

            item.name     = "Demo Item";
            item.currency = "USD";
            item.price    = "5";
            item.quantity = "1";
            item.sku      = "sku";


            List <Item> itms = new List <Item>();

            itms.Add(item);
            ItemList itemList = new ItemList();

            itemList.items = itms;


            Address billingAddress = new Address();

            billingAddress.city         = "NewYork";
            billingAddress.country_code = "US";
            billingAddress.line1        = "23rd street kew gardens";
            billingAddress.postal_code  = "43210";
            billingAddress.state        = "NY";



            CreditCard crdtCard = new CreditCard();

            crdtCard.billing_address = billingAddress;
            crdtCard.cvv2            = ""; // CVV here
            crdtCard.expire_month    = 1;
            crdtCard.expire_year     = 2020;
            crdtCard.first_name      = "Dileepa";
            crdtCard.last_name       = "Rajapaksa";
            crdtCard.number          = ""; //Card Number Here
            crdtCard.type            = "visa";


            Details details = new Details();

            details.shipping = "1";
            details.subtotal = "5";
            details.tax      = "1";

            Amount amnt = new Amount();

            amnt.currency = "USD";

            amnt.total   = "7";
            amnt.details = details;


            Transaction tran = new Transaction();

            tran.amount         = amnt;
            tran.description    = "Description about the payment amount.";
            tran.item_list      = itemList;
            tran.invoice_number = "your invoice number which you are generating";



            List <Transaction> transactions = new List <Transaction>();

            transactions.Add(tran);



            FundingInstrument fundInstrument = new FundingInstrument();

            fundInstrument.credit_card = crdtCard;



            List <FundingInstrument> fundingInstrumentList = new List <FundingInstrument>();

            fundingInstrumentList.Add(fundInstrument);


            Payer payr = new Payer();

            payr.funding_instruments = fundingInstrumentList;
            payr.payment_method      = "credit_card";


            Payment pymnt = new Payment();

            pymnt.intent       = "sale";
            pymnt.payer        = payr;
            pymnt.transactions = transactions;

            try
            {
                APIContext apiContext = Configuration.GetAPIContext();


                Payment createdPayment = pymnt.Create(apiContext);


                if (createdPayment.state.ToLower() != "approved")
                {
                    return(View("Failure"));
                }
            }
            catch (PayPal.PayPalException ex)
            {
                Logger.Log("Error: " + ex.Message);
                return(View("Failure"));
            }

            return(View("Success"));
        }
예제 #5
0
        public ActionResult PaymentWithCreditCard([Bind(Include = "first_name,last_name,billing_address,number,expire_month,expire_year,cvv2")] PayPal.Api.CreditCard creditCard, FormCollection fc)
        {
            creditCard.type = fc["type"];

            //Now make a List of Item and add the above item to it
            //you can create as many items as you want and add to this list
            List <Item> itms = new List <Item>();

            //Get cart items and each to itms list below
            var cart = db.Carts.Where(m => m.MemberID == MyHelpers.LoggedInMember.MemberID && m.Status.Name.ToLower() == "active");

            if (cart.Count() > 0)
            {
                foreach (Cart_Items ci in cart.First().Cart_Items)
                {
                    ////create an item for which you are taking payment
                    Item item = new Item();
                    item.name     = ci.Catalog_Items.Name;
                    item.currency = "USD";
                    item.price    = String.Format("{0:0.00}", (decimal)ci.Catalog_Items.Price);
                    item.quantity = "1";
                    item.sku      = "sku";
                    item.tax      = "0";

                    itms.Add(item);
                }
                ItemList itemList = new ItemList();
                itemList.items = itms;

                // Specify details of your payment amount.
                string subtotal = cart.First().Cart_Items.Sum(m => m.Catalog_Items.Price).ToString();

                Details details = new Details();
                details.shipping = "0";
                details.subtotal = String.Format("{0:0.00}", Convert.ToDecimal(subtotal));
                details.tax      = "0";

                // Specify your total payment amount and assign the details object
                Amount amnt = new Amount();
                amnt.currency = "USD";
                // Total = shipping tax + subtotal.
                amnt.total   = String.Format("{0:0.00}", Convert.ToDecimal(subtotal));
                amnt.details = details;

                // Now make a transaction object and assign the Amount object
                Transaction tran = new Transaction();
                tran.amount      = amnt;
                tran.description = "Description about the payment amount.";
                tran.item_list   = itemList;

                //generate Invoice Number
                DateTime _now   = DateTime.Now;
                string   invNbr = MyHelpers.LoggedInMember.MemberID.ToString() + _now.Year.ToString() + _now.Month.ToString() + _now.Day.ToString() + _now.Minute.ToString() + _now.Second.ToString();

                tran.invoice_number = invNbr;

                // Now, we have to make a list of transaction and add the transactions object
                // to this list. You can create one or more object as per your requirements

                List <Transaction> transactions = new List <Transaction>();
                transactions.Add(tran);

                // Now we need to specify the FundingInstrument of the Payer
                // for credit card payments, set the CreditCard which we made above

                FundingInstrument fundInstrument = new FundingInstrument();
                fundInstrument.credit_card = creditCard;

                // The Payment creation API requires a list of FundingIntrument

                List <FundingInstrument> fundingInstrumentList = new List <FundingInstrument>();
                fundingInstrumentList.Add(fundInstrument);

                // Now create Payer object and assign the fundinginstrument list to the object
                Payer payr = new Payer();
                payr.funding_instruments = fundingInstrumentList;
                payr.payment_method      = "credit_card";

                // finally create the payment object and assign the payer object & transaction list to it
                Payment pymnt = new Payment();
                pymnt.intent       = "sale";
                pymnt.payer        = payr;
                pymnt.transactions = transactions;

                try
                {
                    //getting context from the paypal
                    //basically we are sending the clientID and clientSecret key in this function
                    //to the get the context from the paypal API to make the payment
                    //for which we have created the object above.

                    //Basically, apiContext object has a accesstoken which is sent by the paypal
                    //to authenticate the payment to facilitator account.
                    //An access token could be an alphanumeric string

                    APIContext apiContext = Configuration.GetAPIContext();

                    //Create is a Payment class function which actually sends the payment details
                    //to the paypal API for the payment. The function is passed with the ApiContext
                    //which we received above.

                    Payment createdPayment = pymnt.Create(apiContext);

                    //if the createdPayment.state is "approved" it means the payment was successful else not

                    if (createdPayment.state.ToLower() != "approved")
                    {
                        ViewBag.message = "Card was not approved: " + createdPayment.state.ToString();
                        return(View("Index", creditCard));
                    }
                    else
                    {
                        //SUCCESSFUL
                        //create purchase record for member for each cart item. Then clear cart or mark inactive
                        try
                        {
                            foreach (Cart_Items ci in cart.First().Cart_Items)
                            {
                                Purchase purch = new Purchase();
                                purch.MemberID       = MyHelpers.LoggedInMember.MemberID;
                                purch.Price          = ci.Catalog_Items.Price;
                                purch.Catalog_ItemID = ci.Catalog_ItemID;
                                purch.CreateDate     = DateTime.Now;

                                db.Purchases.Add(purch);
                                db.SaveChanges();
                            }
                            //clear cart
                            db.Carts.First().StatusID = db.Status.Where(m => m.Name.ToLower() == "fulfilled").First().StatusID;
                            db.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                            //could not complete purchase/clear cart operation
                            ViewBag.message = "Could not complete purchase/clear cart operation. " + ex.Message;
                            return(View("Index", creditCard));
                        }

                        ViewBag.message = createdPayment.state.ToString().ToUpper();
                        return(View("SuccessView"));
                    }
                }
                catch (PayPal.PayPalException ex)
                {
                    Logger.Log("Error: " + ex.Message);
                    ViewBag.error = JsonConvert.DeserializeObject <PayPal.Api.Error>(((PayPal.ConnectionException)ex).Response);

                    ViewBag.cnnerror = ((PayPal.ConnectionException)ex).ToString();

                    return(View("Index", creditCard));
                }
            }
            else
            {
                //cart is empty
                ViewBag.message = "Cart is empty";
                return(View("Index", creditCard));
            }
        }
예제 #6
0
파일: Common.cs 프로젝트: iljafiers/FotoLab
        // Create an authorized payment
        public static Authorization CreateAuthorization(APIContext apiContext)
        {
            // ###Address
            // Base Address object used as shipping or billing
            // address in a payment.
            Address billingAddress = new Address();

            billingAddress.city         = "Johnstown";
            billingAddress.country_code = "US";
            billingAddress.line1        = "52 N Main ST";
            billingAddress.postal_code  = "43210";
            billingAddress.state        = "OH";

            // ###CreditCard
            // A resource representing a credit card that can be
            // used to fund a payment.
            CreditCard crdtCard = new CreditCard();

            crdtCard.billing_address = billingAddress;
            crdtCard.cvv2            = "874";
            crdtCard.expire_month    = 11;
            crdtCard.expire_year     = 2018;
            crdtCard.first_name      = "Joe";
            crdtCard.last_name       = "Shopper";
            crdtCard.number          = "4417119669820331";
            crdtCard.type            = "visa";

            // ###Details
            // Let's you specify details of a payment amount.
            Details details = new Details();

            details.shipping = "0.03";
            details.subtotal = "107.41";
            details.tax      = "0.03";

            // ###Amount
            // Let's you specify a payment amount.
            Amount amnt = new Amount();

            amnt.currency = "USD";
            // Total must be equal to sum of shipping, tax and subtotal.
            amnt.total   = "107.47";
            amnt.details = details;

            // ###Transaction
            // A transaction defines the contract of a
            // payment - what is the payment for and who
            // is fulfilling it. Transaction is created with
            // a `Payee` and `Amount` types
            Transaction tran = new Transaction();

            tran.amount      = amnt;
            tran.description = "This is the payment transaction description.";

            // The Payment creation API requires a list of
            // Transaction; add the created `Transaction`
            // to a List
            List <Transaction> transactions = new List <Transaction>();

            transactions.Add(tran);

            // ###FundingInstrument
            // A resource representing a Payeer's funding instrument.
            // Use a Payer ID (A unique identifier of the payer generated
            // and provided by the facilitator. This is required when
            // creating or using a tokenized funding instrument)
            // and the `CreditCardDetails`
            FundingInstrument fundInstrument = new FundingInstrument();

            fundInstrument.credit_card = crdtCard;

            // The Payment creation API requires a list of
            // FundingInstrument; add the created `FundingInstrument`
            // to a List
            List <FundingInstrument> fundingInstrumentList = new List <FundingInstrument>();

            fundingInstrumentList.Add(fundInstrument);

            // ###Payer
            // A resource representing a Payer that funds a payment
            // Use the List of `FundingInstrument` and the Payment Method
            // as 'credit_card'
            Payer payr = new Payer();

            payr.funding_instruments = fundingInstrumentList;
            payr.payment_method      = "credit_card";

            // ###Payment
            // A Payment Resource; create one using
            // the above types and intent as `sale`
            Payment pymnt = new Payment();

            pymnt.intent       = "authorize";
            pymnt.payer        = payr;
            pymnt.transactions = transactions;

            // Create a payment by posting to the APIService
            // using a valid APIContext
            Payment createdPayment = pymnt.Create(apiContext);

            return(createdPayment.transactions[0].related_resources[0].authorization);
        }
예제 #7
0
        public ActionResult PaymentWithCreditCard()
        {
            var item = new Item
            {
                name     = "Demo Item",
                currency = "USD",
                price    = "5",
                quantity = "1",
                sku      = "sku"
            };

            var itms = new List <Item> {
                item
            };
            var itemList = new ItemList {
                items = itms
            };

            var billingAddress = new Address
            {
                city         = "NewYork",
                country_code = "US",
                line1        = "23rd street kew gardens",
                postal_code  = "43210",
                state        = "NY"
            };

            var crdtCard = new CreditCard
            {
                billing_address = billingAddress,
                cvv2            = "874",
                expire_month    = 1,
                expire_year     = 2020,
                first_name      = "Aman",
                last_name       = "Thakur",
                number          = "1234567890123456",
                type            = "visa"
            };

            var details = new Details
            {
                shipping = "1",
                subtotal = "5",
                tax      = "1"
            };

            var amnt = new Amount
            {
                currency = "USD",
                total    = "7",
                details  = details
            };

            var tran = new Transaction
            {
                amount         = amnt,
                description    = "Description about the payment amount.",
                item_list      = itemList,
                invoice_number = "1"
            };

            var transactions = new List <Transaction> {
                tran
            };

            var fundInstrument = new FundingInstrument {
                credit_card = crdtCard
            };

            var fundingInstrumentList = new List <FundingInstrument> {
                fundInstrument
            };

            var payr = new Payer
            {
                funding_instruments = fundingInstrumentList,
                payment_method      = "credit_card"
            };

            var pymnt = new Payment
            {
                intent       = "sale",
                payer        = payr,
                transactions = transactions
            };

            try
            {
                var apiContext     = Configuration.GetApiContext();
                var createdPayment = pymnt.Create(apiContext);
                if (createdPayment.state.ToLower() != "approved")
                {
                    return(View());
                }
            }
            catch (PayPal.PayPalException ex)
            {
                throw ex;
            }

            return(View());
        }
예제 #8
0
        public static Payment CreditCardPayment()
        {
            CreditCard cc = new CreditCard();

            cc.number          = "4012888888881881";
            cc.type            = "visa";
            cc.expire_month    = 11;
            cc.expire_year     = 2018;
            cc.cvv2            = "874";
            cc.first_name      = "Besty";
            cc.last_name       = "Buyer";
            cc.billing_address = new Address()
            {
                line1        = "111 first street",
                city         = "Saratoga",
                state        = "CA",
                postal_code  = "95070",
                country_code = "USA"
            };

            FundingInstrument funding = new FundingInstrument()
            {
                credit_card = cc
            };

            List <FundingInstrument> funds = new List <FundingInstrument>();

            funds.Add(funding);

            Payer payer = new Payer();

            payer.payment_method      = "credit_card";
            payer.funding_instruments = funds;

            List <Transaction> transactions = new List <Transaction>();
            Transaction        tran         = new Transaction()
            {
                amount = new Amount()
                {
                    currency = "USD",
                    total    = "7",
                    details  = new Details()
                    {
                        subtotal = "5",
                        tax      = "1",
                        shipping = "1"
                    }
                },
                description = "My First Paypal Transaction",
                payee       = new Payee()
                {
                    email = "*****@*****.**"
                }
            };

            transactions.Add(tran);

            Payment payment = new Payment();

            payment.intent       = "sale";
            payment.payer        = payer;
            payment.transactions = transactions;

            try
            {
                var     apiContext     = PaypalConfiguration.GetAPIContext();
                Payment createdPayment = payment.Create(apiContext);
                return(createdPayment);
            }
            catch (PayPal.PayPalException ex)
            {
                throw ex;
            }
        }
예제 #9
0
        public static bool PayWithCreditCard(TaxInformationModel taxInformationModel)
        {
            ItemList itemList = new ItemList
            {
                items = new List <Item>
                {
                    new Item
                    {
                        name     = "Tax",
                        price    = "1.99",
                        currency = "USD",
                        quantity = "1"
                    }
                }
            };

            //Address for the payment
            Address billingAddress = new Address();

            billingAddress.city         = "NewYork";
            billingAddress.country_code = "US";
            billingAddress.line1        = "23rd street kew gardens";
            billingAddress.postal_code  = "43210";
            billingAddress.state        = "NY";



            //Now Create an object of credit card and add above details to it
            CreditCard creditCard = new CreditCard();

            creditCard.billing_address = billingAddress;
            //creditCard.cvv2 = "874";
            creditCard.expire_month = taxInformationModel.Expiry.Month;
            creditCard.expire_year  = taxInformationModel.Expiry.Year;
            //creditCard.first_name = "Aman";
            //creditCard.last_name = "Thakur";
            //creditCard.number = "6011000990139424";
            creditCard.number = taxInformationModel.CardNumber;
            creditCard.type   = "discover";

            // Specify details of your payment amount.
            Details details = new Details();

            //details.shipping = "1";
            details.subtotal = "1.99";
            //details.tax = "1";

            // Specify your total payment amount and assign the details object
            Amount amount = new Amount();

            amount.currency = "USD";
            // Total = shipping tax + subtotal.
            amount.total   = "1.99";
            amount.details = details;

            // Now make a trasaction object and assign the Amount object
            Transaction transaction = new Transaction();

            transaction.amount         = amount;
            transaction.description    = "Description about the payment amount.";
            transaction.item_list      = itemList;
            transaction.invoice_number = "your invoice number which you are generating";

            // Now, we have to make a list of trasaction and add the trasactions object
            // to this list. You can create one or more object as per your requirements

            List <Transaction> transactions = new List <Transaction>();

            transactions.Add(transaction);

            // Now we need to specify the FundingInstrument of the Payer
            // for credit card payments, set the CreditCard which we made above

            FundingInstrument fundInstrument = new FundingInstrument();

            fundInstrument.credit_card = creditCard;

            // The Payment creation API requires a list of FundingIntrument

            List <FundingInstrument> fundingInstrumentList = new List <FundingInstrument>();

            fundingInstrumentList.Add(fundInstrument);

            // Now create Payer object and assign the fundinginstrument list to the object
            Payer payer = new Payer();

            payer.funding_instruments = fundingInstrumentList;
            payer.payment_method      = "credit_card";

            // finally create the payment object and assign the payer object & transaction list to it
            Payment payment = new Payment();

            payment.intent       = "sale";
            payment.payer        = payer;
            payment.transactions = transactions;


            //getting context from the paypal
            //basically we are sending the clientID and clientSecret key in this function
            //to the get the context from the paypal API to make the payment
            //for which we have created the object above.

            //Basically, apiContext object has a accesstoken which is sent by the paypal
            //to authenticate the payment to facilitator account.
            //An access token could be an alphanumeric string

            APIContext apiContext = PaypalConfiguration.GetAPIContext();

            //Create is a Payment class function which actually sends the payment details
            //to the paypal API for the payment. The function is passed with the ApiContext
            //which we received above.

            //Payment createdPayment = null;
            //try
            //{
            //    createdPayment = payment.Create(apiContext);
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.Message);
            //}
            Payment createdPayment = payment.Create(apiContext);

            //if the createdPayment.state is "approved" it means the payment was successful else not

            if (createdPayment != null && createdPayment.state.ToLower() != "approved")
            {
                return(true);
            }

            return(false);
        }
예제 #10
0
        public ActionResult PaymentWithCreditCard(PaymentViewModel paymentViewModel)
        {
            ViewBag.SubjectId = new SelectList(db.Disciplines, "Id", "Subject", paymentViewModel.subjectId);

            var currUser = (UserModel)System.Web.HttpContext.Current.Session["user"];

            paymentViewModel.studentPin = currUser.Login;

            //create and item for which you are taking payment
            //if you need to add more items in the list
            //Then you will need to create multiple item objects or use some loop to instantiate object
            Item item = new Item();

            item.name     = "Subject Fees";
            item.currency = "USD";
            item.price    = paymentViewModel.price;
            item.quantity = "1";
            item.sku      = "sku";

            //Now make a List of Item and add the above item to it
            //you can create as many items as you want and add to this list
            List <Item> itms = new List <Item>();

            itms.Add(item);
            ItemList itemList = new ItemList();

            itemList.items = itms;

            //Address for the payment
            Address billingAddress = new Address();

            billingAddress.city         = paymentViewModel.city;
            billingAddress.country_code = paymentViewModel.countryCode.ToUpper();
            billingAddress.line1        = paymentViewModel.line1;
            billingAddress.postal_code  = paymentViewModel.postalCode;
            billingAddress.state        = paymentViewModel.state;

            //Now Create an object of credit card and add above details to it
            CreditCard crdtCard = new CreditCard();

            crdtCard.billing_address = billingAddress;
            crdtCard.cvv2            = paymentViewModel.cvv;
            crdtCard.expire_month    = paymentViewModel.expireMonth;
            crdtCard.expire_year     = paymentViewModel.expireYear;
            crdtCard.first_name      = paymentViewModel.firstName;
            crdtCard.last_name       = paymentViewModel.lastName;
            crdtCard.number          = paymentViewModel.creditCardNo;
            crdtCard.type            = paymentViewModel.creditCardType.ToLower();

            // Specify details of your payment amount.
            Details details = new Details();

            details.shipping = "0";
            details.subtotal = paymentViewModel.price;
            details.tax      = Convert.ToString(Convert.ToDecimal(paymentViewModel.price) * Convert.ToDecimal(0.14));

            // Specify your total payment amount and assign the details object
            Amount amnt = new Amount();

            amnt.currency = "USD";
            // Total = shipping tax + subtotal.
            amnt.total   = Convert.ToString(Convert.ToDecimal(details.subtotal) + Convert.ToDecimal(details.tax));
            amnt.details = details;

            paymentViewModel.total = amnt.total;

            int count = repository.Payments.Where(p => p.Student_PIN == currUser.Login).Count();

            // Now make a trasaction object and assign the Amount object
            Transaction tran = new Transaction();

            tran.amount         = amnt;
            tran.description    = "Transaction made directly with credit card.";
            tran.item_list      = itemList;
            tran.invoice_number = paymentViewModel.studentPin + "APPA0" + count + 1;

            // Now, we have to make a list of trasaction and add the trasactions object
            // to this list. You can create one or more object as per your requirements

            List <Transaction> transactions = new List <Transaction>();

            transactions.Add(tran);

            // Now we need to specify the FundingInstrument of the Payer
            // for credit card payments, set the CreditCard which we made above

            FundingInstrument fundInstrument = new FundingInstrument();

            fundInstrument.credit_card = crdtCard;

            // The Payment creation API requires a list of FundingIntrument

            List <FundingInstrument> fundingInstrumentList = new List <FundingInstrument>();

            fundingInstrumentList.Add(fundInstrument);

            // Now create Payer object and assign the fundinginstrument list to the object
            Payer payr = new Payer();

            payr.funding_instruments = fundingInstrumentList;
            payr.payment_method      = "credit_card";

            paymentViewModel.paymentType = payr.payment_method;

            // finally create the payment object and assign the payer object & transaction list to it
            Payment pymnt = new Payment();

            pymnt.intent       = "sale";
            pymnt.payer        = payr;
            pymnt.transactions = transactions;

            try
            {
                //getting context from the paypal, basically we are sending the clientID and clientSecret key in this function
                //to the get the context from the paypal API to make the payment for which we have created the object above.

                //Code for the configuration class is provided next

                // Basically, apiContext has a accesstoken which is sent by the paypal to authenticate the payment to facilitator account. An access token could be an alphanumeric string

                APIContext apiContext = Models.PaypalSetUp.Configuration.GetAPIContext();

                // Create is a Payment class function which actually sends the payment details to the paypal API for the payment. The function is passed with the ApiContext which we received above.

                Payment createdPayment = pymnt.Create(apiContext);

                //if the createdPayment.State is "approved" it means the payment was successfull else not

                if (createdPayment.state.ToLower() != "approved")
                {
                    return(View("FailureView"));
                }
                SavePayment(paymentViewModel);
            }
            catch (PayPal.PayPalException ex)
            {
                Logger.Log("Error: " + ex.Message);
                return(View("FailureView"));
            }

            return(RedirectToAction("Index", "Payment"));
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpContext CurrContext = HttpContext.Current;

            // ###Items
            // Items within a transaction.
            Item item = new Item();

            item.name     = "Item Name";
            item.currency = "USD";
            item.price    = "1";
            item.quantity = "5";
            item.sku      = "sku";

            List <Item> itms = new List <Item>();

            itms.Add(item);
            ItemList itemList = new ItemList();

            itemList.items = itms;

            // ###CreditCard
            // A resource representing a credit card that can be
            // used to fund a payment.
            CreditCardToken credCardToken = new CreditCardToken();

            credCardToken.credit_card_id = "CARD-5MY32504F4899612AKIHAQHY";

            // ###Details
            // Let's you specify details of a payment amount.
            Details details = new Details();

            details.shipping = "1";
            details.subtotal = "5";
            details.tax      = "1";

            // ###Amount
            // Let's you specify a payment amount.
            Amount amnt = new Amount();

            amnt.currency = "USD";
            // Total must be equal to the sum of shipping, tax and subtotal.
            amnt.total   = "7";
            amnt.details = details;

            // ###Transaction
            // A transaction defines the contract of a
            // payment - what is the payment for and who
            // is fulfilling it.
            Transaction tran = new Transaction();

            tran.amount      = amnt;
            tran.description = "This is the payment transaction description.";
            tran.item_list   = itemList;

            // The Payment creation API requires a list of
            // Transaction; add the created `Transaction`
            // to a List
            List <Transaction> transactions = new List <Transaction>();

            transactions.Add(tran);

            // ###FundingInstrument
            // A resource representing a Payer's funding instrument.
            // For stored credit card payments, set the CreditCardToken
            // field on this object.
            FundingInstrument fundInstrument = new FundingInstrument();

            fundInstrument.credit_card_token = credCardToken;

            // The Payment creation API requires a list of
            // FundingInstrument; add the created `FundingInstrument`
            // to a List
            List <FundingInstrument> fundingInstrumentList = new List <FundingInstrument>();

            fundingInstrumentList.Add(fundInstrument);

            // ###Payer
            // A resource representing a Payer that funds a payment
            // Use the List of `FundingInstrument` and the Payment Method
            // as 'credit_card'
            Payer payr = new Payer();

            payr.funding_instruments = fundingInstrumentList;
            payr.payment_method      = "credit_card";

            // ###Payment
            // A Payment Resource; create one using
            // the above types and intent as 'sale'
            Payment pymnt = new Payment();

            pymnt.intent       = "sale";
            pymnt.payer        = payr;
            pymnt.transactions = transactions;

            try
            {
                // ### Api Context
                // Pass in a `APIContext` object to authenticate
                // the call and to send a unique request id
                // (that ensures idempotency). The SDK generates
                // a request id if you do not pass one explicitly.
                // See [Configuration.cs](/Source/Configuration.html) to know more about APIContext..
                APIContext apiContext = Configuration.GetAPIContext();

                // Create a payment using a valid APIContext
                Payment createdPayment = pymnt.Create(apiContext);
                CurrContext.Items.Add("ResponseJson", Common.FormatJsonString(createdPayment.ConvertToJson()));
            }
            catch (PayPal.Exception.PayPalException ex)
            {
                CurrContext.Items.Add("Error", ex.Message);
            }
            CurrContext.Items.Add("RequestJson", Common.FormatJsonString(pymnt.ConvertToJson()));
            Server.Transfer("~/Response.aspx");
        }
예제 #12
0
        // POST: api/Payment
        public PaymentResponse Post(PaymentDetails paymentDetails)
        {
            var creditCard = new CreditCard
            {
                cvv2         = paymentDetails.Cvv,
                expire_month = int.Parse(paymentDetails.ExpirationMonth),
                expire_year  = int.Parse(paymentDetails.ExpirationYear),
                number       = paymentDetails.CardNumber,
                type         = "visa"
            };

            var details = new Details
            {
                shipping = "0",
                subtotal = paymentDetails.Amount,
                tax      = "0",
            };

            var amount = new Amount
            {
                currency = "USD",
                total    = paymentDetails.Amount,
                details  = details,
            };

            var transaction = new Transaction
            {
                amount         = amount,
                invoice_number = Common.GetRandomInvoiceNumber()
            };

            var transactions = new List <Transaction> {
                transaction
            };

            var fundingInstrument = new FundingInstrument {
                credit_card = creditCard
            };

            var fundingInstruments = new List <FundingInstrument> {
                fundingInstrument
            };

            var payer = new Payer
            {
                funding_instruments = fundingInstruments,
                payment_method      = "credit_card"
            };

            var paymet = new Payment
            {
                intent       = "sale",
                payer        = payer,
                transactions = transactions
            };

            try
            {
                var apiContext    = Models.Configuration.GetApiContext();
                var createPayment = paymet.Create(apiContext);

                if (createPayment.state.ToLower() != "approved")
                {
                    return(new PaymentResponse
                    {
                        TransactionSuccessful = false,
                        Message = null
                    });
                }
            }
            catch (PayPal.PayPalException ex)
            {
                return(new PaymentResponse
                {
                    TransactionSuccessful = false,
                    Message = ex.InnerException?.Message
                });
            }
            return(new PaymentResponse
            {
                TransactionSuccessful = true,
                Message = null
            });
        }
예제 #13
0
        public ActionResult PaymentWithCreditCard()
        {
            //create and item for which you are taking payment
            //if you need to add more items in the list
            //Then you will need to create multiple item objects or use some loop to instantiate object

            Item item = new Item
            {
                name     = "Demo Item1",
                currency = "USD",
                price    = "6",
                quantity = "2",
                sku      = "dhghftghjn"
            };

            //Now make a List of Item and add the above item to it
            //you can create as many items as you want and add to this list
            List <Item> itms = new List <Item> {
                item
            };
            ItemList itemList = new ItemList {
                items = itms
            };

            //Address for the payment
            Address billingAddress = new Address
            {
                city         = "NewYork",
                country_code = "US",
                line1        = "23rd street kew gardens",
                postal_code  = "43210",
                state        = "NY"
            };


            //Now Create an object of credit card and add above details to it
            //Please replace your credit card details over here which you got from paypal
            CreditCard crdtCard = new CreditCard
            {
                billing_address = billingAddress,
                cvv2            = "874",
                expire_month    = 1,
                expire_year     = 2020,
                first_name      = "Aman",
                last_name       = "Thakur",
                number          = "1234567890123456",
                type            = "visa"
            };
            //card cvv2 number
            //card expire date
            //card expire year
            //enter your credit card number here
            //credit card type here paypal allows 4 types

            // Specify details of your payment amount.
            Details details = new Details
            {
                shipping = "1",
                subtotal = "5",
                tax      = "1"
            };

            // Specify your total payment amount and assign the details object
            Amount amnt = new Amount
            {
                currency = "USD",
                total    = "7",
                details  = details
            };
            // Total = shipping tax + subtotal.

            // Now make a transaction object and assign the Amount object

            var guid = Guid.NewGuid().ToString();

            Transaction tran = new Transaction
            {
                amount         = amnt,
                description    = "Description about the payment amount.",
                item_list      = itemList,
                invoice_number = guid
            };

            // Now, we have to make a list of transaction and add the transactions object
            // to this list. You can create one or more object as per your requirements

            List <Transaction> transactions = new List <Transaction>
            {
                tran
            };

            // Now we need to specify the FundingInstrument of the Payer
            // for credit card payments, set the CreditCard which we made above

            FundingInstrument fundInstrument = new FundingInstrument
            {
                credit_card = crdtCard
            };

            // The Payment creation API requires a list of FundingIntrument

            List <FundingInstrument> fundingInstrumentList = new List <FundingInstrument>
            {
                fundInstrument
            };

            // Now create Payer object and assign the fundinginstrument list to the object
            Payer payr = new Payer
            {
                funding_instruments = fundingInstrumentList,
                payment_method      = "credit_card"
            };

            // finally create the payment object and assign the payer object & transaction list to it
            Payment pymnt = new Payment
            {
                intent       = "sale",
                payer        = payr,
                transactions = transactions
            };

            try
            {
                //getting context from the paypal
                //basically we are sending the clientID and clientSecret key in this function
                //to the get the context from the paypal API to make the payment
                //for which we have created the object above.

                //Basically, apiContext object has a accesstoken which is sent by the paypal
                //to authenticate the payment to facilitator account.
                //An access token could be an alphanumeric string

                APIContext apiContext = Configuration.GetAPIContext();

                //Create is a Payment class function which actually sends the payment details
                //to the paypal API for the payment. The function is passed with the ApiContext
                //which we received above.

                Payment createdPayment = pymnt.Create(apiContext);

                //if the createdPayment.state is "approved" it means the payment was successful else not

                if (createdPayment.state.ToLower() != "approved")
                {
                    return(View("FailureView"));
                }
            }
            catch (PayPal.PayPalException ex)
            {
                // Logger.Log("Error: " + ex.Message);
                return(View("FailureView"));
            }

            return(View("SuccessView"));
        }
예제 #14
0
        public static string ProcessSale(string firstName, string lastName, string billAddress1, string billAddress2, string billCity, string billState, string billCountry, string billZip,
                                         List <SaleProduct> CartItems, string CardNo, string cvv2, int expYear, int expMonth, decimal shippingCharge)
        {
            string  PaymentID  = "";
            decimal totalPrice = 0M;
            Dictionary <string, string> payPalConfig = new Dictionary <string, string>();

            payPalConfig.Add("mode", mode);

            try
            {
                string     AccessToken = GetPayPalAccessToken();
                APIContext AC          = new APIContext(AccessToken);
                AC.Config = payPalConfig;

                Payee pe = new Payee();
                pe.merchant_id = "Q4A2XY37JY7VW";

                PayPal.Api.Address billingAddress = new PayPal.Api.Address();
                billingAddress.city  = billCity;
                billingAddress.line1 = billAddress1;
                if (!string.IsNullOrWhiteSpace(billAddress2))
                {
                    billingAddress.line2 = billAddress2;
                }
                billingAddress.state        = billState;
                billingAddress.country_code = billCountry;
                billingAddress.postal_code  = billZip;

                CreditCard cc = new CreditCard();
                cc.billing_address = billingAddress;
                cc.number          = CardNo;
                cc.cvv2            = cvv2;
                cc.type            = CCUtils.CreditCardType(CardNo).ToLower();
                cc.first_name      = firstName;
                cc.last_name       = lastName;
                cc.expire_month    = expMonth;
                cc.expire_year     = expYear;

                FundingInstrument fi = new FundingInstrument();
                fi.credit_card = cc;

                Payer py = new Payer();
                py.payment_method      = "credit_card";
                py.funding_instruments = new List <FundingInstrument>()
                {
                    fi
                };

                Transaction t = new Transaction();
                t.amount = new Amount();

                foreach (SaleProduct item in CartItems)
                {
                    totalPrice = Math.Round(totalPrice + (item.UnitPrice * item.Quantity), 2, MidpointRounding.AwayFromZero);
                }

                t.amount.currency         = "USD";
                t.amount.details          = new Details();
                t.amount.details.subtotal = totalPrice.ToString("0.00");
                t.amount.details.tax      = "0.00";
                t.amount.details.shipping = shippingCharge.ToString("0.00");
                t.amount.total            = Math.Round(totalPrice + shippingCharge, 2, MidpointRounding.AwayFromZero).ToString("0.00");
                t.description             = "Chimaera Conspiracy Store Purchase";

                Payment p = new Payment();
                p.intent       = "sale";
                p.transactions = new List <Transaction>()
                {
                    t
                };
                p.payer = py;

                Payment pResp = p.Create(AC);

                if (pResp.state.Equals("approved", StringComparison.OrdinalIgnoreCase))
                {
                    PaymentID = pResp.id;
                }
            }
            catch (PayPal.PayPalException ppex)
            {
                LoggingUtil.InsertError(ppex);
            }
            catch (WebException ex)
            {
                LoggingUtil.InsertError(ex);
                WebResponse wr = ex.Response;
                if (wr != null)
                {
                    StreamReader sr = new StreamReader(wr.GetResponseStream());
                    string       ss = sr.ReadToEnd();
                    throw new Exception(ss);
                }
            }
            catch (Exception ex)
            {
                LoggingUtil.InsertError(ex);
            }

            return(PaymentID);
        }