如何在MVC C#中使用Paypal Checkout托管页面进行信用卡付款

时间:2016-03-19 05:53:29

标签: c# asp.net-mvc paypal

我在使用Paypal Checkout Hosted页面进行信用卡交易时,实施信用卡付款存在问题。

这是我的Paypal付款代码:

public ActionResult CreatePayment(string packageName)
    {
        #region check client balance
        long clientid = Convert.ToInt64(Session["iClientId"]);
        string newPaymentMethod = "PayPal";
        ClientPackageInfo obj = new ClientPackageInfo();
        obj = objPaymentHelper.CalculateNewPackage(packageName, clientid, newPaymentMethod);

        #region current package descriptor for paypal display
        var newPkg = db.Package.Where(cs => cs.PackageId == obj.newPackageId).SingleOrDefault();
        string paypalDisplayDecription = "Package : "+newPkg.Name+", Price : "+newPkg.Price+", Payable : "+obj.paymentAmount+", Description : "+newPkg.Description;
        #endregion

        if (obj.IsPaymentNeeded == true)
        {

            #region paypal viewdata
            var viewData = new PayPalViewData();
            var guid = Guid.NewGuid().ToString();

            var paymentInit = new PayPal.Api.Payments.Payment
            {
                intent = "authorize",
                payer = new PayPal.Api.Payments.Payer
                {
                    payment_method = "paypal"
                },
                transactions = new List<PayPal.Api.Payments.Transaction>
            {
                new PayPal.Api.Payments.Transaction
                {
                    amount = new PayPal.Api.Payments.Amount
                    {
                        currency = "USD",
                        total = (obj.paymentAmount + 0.0 + 0.0).ToString(),
                        details = new PayPal.Api.Payments.Details
                        {
                            subtotal = obj.paymentAmount.ToString(),
                            tax = 0.0.ToString(),
                            shipping = 0.0.ToString()
                        }
                    },
                    description = paypalDisplayDecription,
                },
            },
                redirect_urls = new PayPal.Api.Payments.RedirectUrls
                {
                    return_url = Utilities.ToAbsoluteUrl(HttpContext, String.Format("~/payment/confirmed?id={0}", guid)),
                    cancel_url = Utilities.ToAbsoluteUrl(HttpContext, String.Format("~/payment/index?id={0}", guid)),
                },
            };

            viewData.JsonRequest = JObject.Parse(paymentInit.ConvertToJson()).ToString(Newtonsoft.Json.Formatting.Indented);
            #endregion

            #region create payment
            try
            {
                var abc = ConfigManager.Instance.GetProperties()["ClientID"];
                var abcc = ConfigManager.Instance.GetProperties()["ClientSecret"];
                var accessToken = new PayPal.OAuthTokenCredential(ConfigManager.Instance.GetProperties()["ClientID"], ConfigManager.Instance.GetProperties()["ClientSecret"]).GetAccessToken();
                var apiContext = new PayPal.APIContext(accessToken);
                var createdPayment = paymentInit.Create(apiContext);

                var approvalUrl = createdPayment.links.ToArray().FirstOrDefault(f => f.rel.Contains("approval_url"));

                if (approvalUrl != null)
                {
                    Session.Add(guid, createdPayment.id);

                    return Redirect(approvalUrl.href);
                }

                viewData.JsonResponse = JObject.Parse(createdPayment.ConvertToJson()).ToString(Newtonsoft.Json.Formatting.Indented);

                return View("Error", viewData);
            }
            catch (PayPalException ex)
            {
                viewData.ErrorMessage = ex.Message;
                return View("Error", viewData);
            }
            #endregion
        }
        else
        {
            #region save client information
            SaveClientInfo saveinfo = new SaveClientInfo();
            saveinfo = objPaymentHelper.SaveInfo(obj);
            if(saveinfo.isSuccessfull == true)
            {
                Session["message"] = "show";
                return RedirectToAction("Index", "iClientPackageHistory");
            }
            else
            {
                return View("Error");
            }
            #endregion
        }

        #endregion
    }

我无法找到使用Paypal的Checkout托管页面的方法。我已经生成了Paypal的代码片段,可以在我的页面上使用。请帮助我如何更改此代码,以便能够使用C#MVC使用Paypal Checkout托管页面。

0 个答案:

没有答案