简体   繁体   English

PayPal快速结帐:点击按钮即可检查付款

[英]PayPal express checkout: checking payment can be performed on button click

I'm using PayPal express checkout (checkout.js V4.0.0) with asp.net mvc to allow a user to pay for data transactions. 我正在使用带asp.net mvc的 PayPal Express Checkout (checkout.js V4.0.0),以允许用户为数据交易付费。 What I need to do when the express checkout button is clicked is perform some checks on the database and confirm that PayPal can proceed (this is also time related, as the database could be in a locked processing state). 单击快速结帐按钮时,我需要做的是对数据库进行一些检查,并确认PayPal可以继续进行(这也与时间有关,因为数据库可能处于锁定的处理状态)。

I've setup the Advanced Server Integration and I then call the create-payment controller from the payment section in paypal.Button.render , but this expects a json object with a PaymentID element to be returned. 我已经设置了Advanced Server Integration ,然后从paypal.Button.render的付款部分调用create-payment控制器,但这期望json带有PaymentID元素的json对象。 At what point am I able to perform these checks on server side and abort from the paypal process if PayPal can't continue? 在什么时候我可以在服务器端执行这些检查并在PayPal无法继续的情况下退出Paypal流程? If a check fails, the server side also needs to return an appropriate error page or message to be displayed. 如果检查失败,则服务器端还需要返回适当的错误页面或要显示的消息。

This is the paypal button code: 这是贝宝按钮代码:

<script src="https://www.paypalobjects.com/api/checkout.js"></script>
<script>
    paypal.Button.render({
        env: 'sandbox',
        payment: function (resolve, reject) {
            var CREATE_PAYMENT_URL = '@Url.Action("PayTransactions","Pending")';

        paypal.request.post(CREATE_PAYMENT_URL)
            .then(function (data) { resolve(data.paymentID); })
            .catch(function (err) { reject(err); });
        },
        onAuthorize: function(data) {
            var EXECUTE_PAYMENT_URL = 'https://my-store.com/paypal/execute-payment';

            paypal.request.post(EXECUTE_PAYMENT_URL,
            {
                paymentID: data.paymentID,
                payerID: data.payerID
            })
                .then(function(data) { /* Go to a success page */ })
                .catch(function (err) { /* Go to an error page  */ });
        },
        onCancel: function (data, actions) {
            return actions.redirect();
        },
        onError: function (err) {
            // Show an error page here, when an error occurs
        }
    }, '#paypal-button');
</script>

which at the payment section calls this: 在付款部分将其称为:

public async Task<string> PayTransactions()
{
    // check if payment is still necessary or end of month is running
    var condition = await CheckDatabaseIsUsable();

    switch (condition)
    {
        case 1:
            ModelState.AddModelError("error", "some error message");
            return RedirectToAction("Index", "Pending");

        case 2:
            ModelState.AddModelError("error", "some other error");
            return RedirectToAction("Index", "Pending");
    }

    var paypalPayment = FormPayPalPaymentObject();

    return JsonConvert.SerializeObject(new { paymentID = paypalPayment.PaymentId });
}

The problem is that I am now mixing the ActionResult and json string return types. 问题是我现在混合了ActionResult和json string返回类型。

您可以致电reject(error)取消付款。

You can return json also for the redirection responses and control with javascript when it is a redirection or and ok response. 您也可以为重定向响应返回json,并在重定向或OK响应时使用javascript进行控制。

Server side: 服务器端:

  return JsonConvert.SerializeObject(new { redirect= Url.Action("Index", "Pending") });

Javascript: 使用Javascript:

     paypal.request.post(CREATE_PAYMENT_URL)
            .then(function (data) {
                if(data.redirect)
                {
                    //cancel the flow and redirect if needed
                    window.location.href = data.redirect;
                }else{
                    resolve(data.paymentID); 
                }
            })
            .catch(function (err) { reject(err); });
        },

Using an IActionResult object as the return value for the PayTransactions is preferable 最好使用IActionResult对象作为IActionResult的返回值

public async Task<IActionResult> PayTransactions()
{

    ...

    return Json(new { paymentID = paypalPayment.PaymentId });
}

Also consider that the modelstate errors you are adding are useless because of the redirection. 还要考虑由于重定向,您添加的modelstate错误是没有用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM