Пример #1
0
        public ActionResult PaymentItemReceive(string token, string payerID)
        {
            try {
                string query;

                // First thing's first. We'll need to get the token and payer ID returned from the previous call:
                if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(payerID))
                {
                    ViewBag.ErrorMessage = "Something went wrong, payment didn't go through the gateway.";
                    return(View());
                }


                //BufferRegister model = new BufferRegister();

                BufferRegister dbModel; //= DBHelper.DBMembership.GetModelByTransactionToken(token);
                Helpers.DB     xObj = new Helpers.DB();
                dbModel = xObj.GetBufferRegisterByPayToken(token, new AppDbContext());

                // using (AppDbContext context = new AppDbContext())
                // {
                //     query = "SELECT BufferRegister.*, Membership.Title, Membership.[Description], Membership.Price from BufferRegister " +
                //             " LEFT JOIN Membership on Membership.MembershipID = BufferRegister.MembershipID " +
                //             " WHERE BufferRegister.TransactionToken = @TransactionToken ";
                //     dbModel = context.Database.SqlQuery<BufferRegister>(query,
                //         new SqlParameter("@TransactionToken", token)
                //         ).First();
                //}

                string  nameProduct = HttpUtility.UrlEncode(dbModel.BufferMembership.Title);
                string  descProduct = HttpUtility.UrlEncode(dbModel.BufferMembership.Description);
                decimal tax         = 13 / 100;
                string  sTax        = Helpers.Tools.PriceFormat(tax);
                string  Amount      = Helpers.Tools.PriceFormat(dbModel.BufferMembership.Price);
                string  totalAmount = Helpers.Tools.PriceFormat(dbModel.BufferMembership.Price * (1 + tax));


                // Than we add the tokens to string type variables as we'll need to rebuild the NVP string

                // Rebuilding the NVP string for the request; I've hardcoded the payment values again as this sample app does not have a database behind it.
                string NVP = string.Empty;

                NVP += "METHOD=DoExpressCheckoutPayment";
                NVP += "&VERSION=123";

                NVP += "&USER="******"&PWD=" + Your_Test_Account_Password;
                NVP += "&SIGNATURE=" + Your_Test_Account_Signature;

                NVP += "&TOKEN=" + token;
                NVP += "&PAYERID=" + payerID;

                NVP += "&PAYMENTREQUEST_0_PAYMENTACTION=Sale";
                NVP += "&PAYMENTREQUEST_0_AMT=" + totalAmount;
                NVP += "&PAYMENTREQUEST_0_ITEMAMT=" + Amount;
                NVP += "&PAYMENTREQUEST_0_SHIPPINGAMT=0";
                NVP += "&PAYMENTREQUEST_0_HANDLINGAMT=0";
                NVP += "&PAYMENTREQUEST_0_TAXAMT=" + sTax;

                // Making the API call
                string response = APICall(NVP);

                // Interpreting the response from PayPal; As a simple UI for checking the transaction, I'm displaying the transaction ID in the page on success so to make things easier when I'm checking the transaction log in PayPal's web UI.
                if (response.Contains("Success"))
                {
                    string transactionId  = response.Substring(response.IndexOf("PAYMENTINFO_0_TRANSACTIONID"), response.IndexOf("&", response.IndexOf("PAYMENTINFO_0_TRANSACTIONID")) - response.IndexOf("PAYMENTINFO_0_TRANSACTIONID"));
                    string TransactNumber = transactionId.Split('=')[1];
                    ViewBag.PaymentStatus += transactionId;

                    //model = DBHelper.DBMembership.CheckTransactionTokenReturn(token);
                    if (dbModel.isBufferValid())
                    {
                        Models.PayTransaction modelTrans = new Models.PayTransaction();
                        modelTrans.PayerID           = payerID;
                        modelTrans.TransactionNumber = TransactNumber;
                        dbModel.BufferTransaction    = modelTrans;

                        using (AppDbContext context = new AppDbContext())
                        {
                            //validate transaction, update database
                            query = "update BufferRegister set PayerID=@PayerID, TransactionNumber=@TransactionNumber where Email=@Email";
                            context.Database.ExecuteSqlCommand(query,
                                                               new SqlParameter("@Email", dbModel.BufferContact.Email),
                                                               new SqlParameter("@TransactionNumber", dbModel.BufferTransaction.TransactionNumber),
                                                               new SqlParameter("@PayerID", dbModel.BufferTransaction.PayerID));

                            //buffer transfer, update database
                            query = "BufferTransfer @Email";
                            context.Database.ExecuteSqlCommand(query,
                                                               new SqlParameter("@Email", dbModel.BufferContact.Email));
                        }
                        //create application username
                        Helpers.MembUsers.CreateMember(dbModel, new AuthDbContext());
                    }
                }
                else
                {
                    //model = (MembershipContactInfoModel)Session["Payer"];
                    throw new Exception("Something went wrong, payment didn't go through the gateway.");
                }

                return(View(dbModel));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("CustomError", ex.Message);
                return(View("Error"));
            }
        }
Пример #2
0
        public ActionResult PaymentReceive(string token, string payerID)
        {
            try
            {
                string currency = "CAD";

                string query;
                //string payerID = payerID;
                // First thing's first. We'll need to get the token and payer ID returned from the previous call:
                //|| string.IsNullOrEmpty(payerID)
                if (string.IsNullOrEmpty(token))
                {
                    ViewBag.ErrorMessage = "Something went wrong, payment didn't go through the gateway.";
                    return(View());
                }


                //BufferRegister model = new BufferRegister();

                BufferRegister dbModel; //= DBHelper.DBMembership.GetModelByTransactionToken(token);
                Helpers.DB     xObj = new Helpers.DB();
                dbModel = xObj.GetBufferRegisterByPayToken(token, new AppDbContext());

                string  nameProduct = HttpUtility.UrlEncode(dbModel.BufferMembership.Title);
                string  descProduct = HttpUtility.UrlEncode(dbModel.BufferMembership.Description);
                decimal tax         = 13 / 100;
                string  sTax        = Helpers.Tools.PriceFormat(tax);
                string  Amount      = Helpers.Tools.PriceFormat(dbModel.BufferMembership.Price);
                string  totalAmount = Helpers.Tools.PriceFormat(dbModel.BufferMembership.Price * (1 + tax));
                string  sTaxAmt     = Helpers.Tools.PriceFormat(tax * dbModel.BufferMembership.Price);

                // Than we add the tokens to string type variables as we'll need to rebuild the NVP string

                // Rebuilding the NVP string for the request;
                string NVP = string.Empty;
                NVP += "USER="******"&PWD=" + Your_Test_Account_Password;
                NVP += "&SIGNATURE=" + Your_Test_Account_Signature;

                NVP += "&METHOD=CreateRecurringPaymentsProfile";
                NVP += "&VERSION=86";

                //product
                NVP += "&DESC=" + nameProduct;  //#Profile description - same value as a billing agreement description
                //NVP += "&NAME=" + nameProduct;  //#Profile description - same value as a billing agreement description

                //payment
                NVP += "&TOKEN=" + token;
                NVP += "&TAXAMT=" + sTaxAmt;
                //NVP += "&INITAMT=" + Amount;
                NVP += "&BILLINGPERIOD=Year";  //    #Period of time between billings
                NVP += "&BILLINGFREQUENCY=1";  //    #Frequency of charges
                NVP += "&AMT=" + Amount;       //    #The amount the buyer will pay in a payment period
                NVP += "&MAXFAILEDPAYMENTS=3"; //    #Maximum failed payments before suspension of the profile
                NVP += "&NOSHIPPING=2";        //    #Maximum failed payments before suspension of the profile
                NVP += "&CURRENCYCODE=" + currency;

                // Products involved in the transaction
                NVP += "&PAYMENTREQUEST_0_AMT=" + totalAmount;      // Total payment for the transaction
                NVP += "&PAYMENTREQUEST_0_ITEMAMT=" + Amount;       // Purchased product price
                NVP += "&PAYMENTREQUEST_0_TAXAMT=" + sTaxAmt;       // Tax amount
                NVP += "&PAYMENTREQUEST_0_QTY=1";
                //NVP += "&L_PAYMENTREQUEST_0_NAME0=" + nameProduct;                                                  // Product name
                //NVP += "&L_PAYMENTREQUEST_0_DESC0=" + descProduct;                                                  // Product description
                //NVP += "&L_PAYMENTREQUEST_0_AMT0=" + totalAmount;       // Product price
                //NVP += "&L_PAYMENTREQUEST_0_QTY0=1";                            // Product quantity

                //Payer Information Fields
                NVP += "&EMAIL=" + dbModel.BufferContact.Email;

                NVP += "&SHIPTONAME=" + dbModel.BufferContact.FirstName + " " + dbModel.BufferContact.LastName;

                NVP += "&SUBSCRIBERNAME=" + dbModel.BufferContact.FirstName + " " + dbModel.BufferContact.LastName;
                NVP += "&PROFILEREFERENCE=INVOICENUMBERORSTUFF";
                NVP += "&PROFILESTARTDATE=" + DateTime.UtcNow.ToString(); //  #Billing date start, in UTC/GMT format
                NVP += "&FIRSTNAME=" + dbModel.BufferContact.FirstName;
                NVP += "&LASTNAME=" + dbModel.BufferContact.LastName;
                NVP += "&STREET=1 Anything Street";
                NVP += "&ADDRESS1=1 Anything Street";
                NVP += "&CITY=" + dbModel.BufferContact.City;
                //NVP += "&STATE=ON";
                //NVP += "&ZIP=L4Z3U2";
                NVP += "&COUNTRYCODE=CA";
                NVP += "&BUSINESS=City of Hamilton";
                NVP += "&AUTOBILLAMT=AddToNextBilling";


                // Making the API call
                string response = APICall(NVP);

                // Interpreting the response from PayPal; As a simple UI for checking the transaction, I'm displaying the transaction ID in the page on success so to make things easier when I'm checking the transaction log in PayPal's web UI.
                if (response.Contains("Success"))
                {
                    string Profile        = response.Substring(response.IndexOf("PROFILEID"), response.IndexOf("&", response.IndexOf("PROFILEID")) - response.IndexOf("PROFILEID"));
                    string TransactNumber = Profile.Split('=')[1];
                    ViewBag.PaymentStatus += TransactNumber;

                    //model = DBHelper.DBMembership.CheckTransactionTokenReturn(token);
                    if (dbModel.isBufferValid())
                    {
                        Models.PayTransaction modelTrans = new Models.PayTransaction();
                        modelTrans.PayerID           = payerID;
                        modelTrans.TransactionNumber = TransactNumber;
                        dbModel.BufferTransaction    = modelTrans;

                        using (AppDbContext context = new AppDbContext())
                        {
                            //validate transaction, update database
                            query = "update BufferRegister set PayerID=@PayerID, TransactionNumber=@TransactionNumber where Email=@Email";
                            context.Database.ExecuteSqlCommand(query,
                                                               new SqlParameter("@Email", dbModel.BufferContact.Email),
                                                               new SqlParameter("@TransactionNumber", dbModel.BufferTransaction.TransactionNumber),
                                                               new SqlParameter("@PayerID", dbModel.BufferTransaction.PayerID));

                            //buffer transfer, update database
                            query = "BufferTransfer @Email";
                            context.Database.ExecuteSqlCommand(query,
                                                               new SqlParameter("@Email", dbModel.BufferContact.Email));
                        }
                        //create application username
                        Helpers.MembUsers.CreateMember(dbModel, new AuthDbContext());
                    }
                }
                else
                {
                    //model = (MembershipContactInfoModel)Session["Payer"];
                    throw new Exception("Something went wrong, payment didn't go through the gateway.");
                }

                return(View(dbModel));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("CustomError", ex.Message);
                return(View("Error"));
            }
        }