2

我有以下示例可以调用 PayPal 的 REST API 进行付款。这很好用,但我想将它用于我的新 MVC 应用程序。我想它需要适应使用 MVC 辅助方法,例如 RedirectToAction 而不是 Server.Transfer 等。

是否有人已经将此示例转换为在 MVC4 控制器操作中运行?

// ###Payer
            // A resource representing a Payer that funds a payment
            // Payment Method
            // as `paypal`
            Payer payr = new Payer();
            payr.payment_method = "paypal";
            Random rndm = new Random();
            var guid = Convert.ToString(rndm.Next(100000));

            string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/PaymentWithPayPal.aspx?";

            // # Redirect URLS
            RedirectUrls redirUrls = new RedirectUrls();
            redirUrls.cancel_url = baseURI + "guid=" + guid;
            redirUrls.return_url = baseURI + "guid=" + guid;

            // ###Details
            // Let's you specify details of a payment amount.
            Details details = new Details();
            details.tax = "15";
            details.shipping = "10";
            details.subtotal = "75";

            // ###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 = "100";
            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
            List<Transaction> transactionList = new List<Transaction>();
            Transaction tran = new Transaction();
            tran.description = "Transaction description.";
            tran.amount = amnt;
            // The Payment creation API requires a list of
            // Transaction; add the created `Transaction`
            // to a List
            transactionList.Add(tran);

            // ###Payment
            // A Payment Resource; create one using
            // the above types and intent as 'sale'
            pymnt = new Payment();
            pymnt.intent = "sale";
            pymnt.payer = payr;
            pymnt.transactions = transactionList;
            pymnt.redirect_urls = redirUrls;

            try
            {
                // ###AccessToken
                // Retrieve the access token from
                // OAuthTokenCredential by passing in
                // ClientID and ClientSecret
                // It is not mandatory to generate Access Token on a per call basis.
                // Typically the access token can be generated once and
                // reused within the expiry window
                string accessToken = new OAuthTokenCredential(ConfigManager.Instance.GetProperties()["ClientID"], ConfigManager.Instance.GetProperties()["ClientSecret"]).GetAccessToken();

                // ### 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. 
                APIContext apiContext = new APIContext(accessToken);
                // Use this variant if you want to pass in a request id  
                // that is meaningful in your application, ideally 
                // a order id.
                // String requestId = Long.toString(System.nanoTime();
                // APIContext apiContext = new APIContext(accessToken, requestId ));

                // Create a payment by posting to the APIService
                // using a valid AccessToken
                // The return object contains the status;
                Payment createdPayment = pymnt.Create(apiContext);

                CurrContext.Items.Add("ResponseJson", JObject.Parse(createdPayment.ConvertToJson()).ToString(Formatting.Indented));

                var links = createdPayment.links.GetEnumerator();

                while (links.MoveNext())
                {
                    Links lnk = links.Current;
                    if (lnk.rel.ToLower().Trim().Equals("approval_url"))
                    {
                        CurrContext.Items.Add("RedirectURL", lnk.href);
                    }
                }
                Session.Add(guid, createdPayment.id);
            }
            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");
4

1 回答 1

4

根据您的需要,我实际上刚刚完成了一个基本的 MVC 实现,该实现支持通过 REST API 进行的 PayPal 支付(非信用卡)的授权、捕获和无效。

https://github.com/lakario/PayPalRESTSample

于 2013-08-01T01:21:41.893 回答