MVC中的Worldpay集成问题

时间:2016-08-01 10:21:08

标签: c# asp.net-mvc worldpay

我正在尝试在我的MVC项目中实施worldpay付款方式并尝试进行测试付款,但它向我显示此错误

  • 您已完成或取消了付款。
  • 您已禁用Cookie。要完成付款,请启用 通过更改浏览器中的隐私设置来使用Cookie。然后 返回商家网站并重新提交付款。
  • 您在WorldPay的会话已超时。请回到 商家网站并重新提交付款。

我正在尝试的代码

<form action="https://secure-test.worldpay.com/wcc/purchase" method="post">
    <input name="address1" type="hidden" value="10 Downing Street" />
    <input name="amount" type="hidden" value="100.00" />
    <input name="cartId" type="hidden" value="DAW" />
    <input name="op-DPChoose-VISA^SSL" type="hidden" value="DAW" />
    <input name="cardNoInput" type="hidden" value="4444333322221111" />
    <input name="country" type="hidden" value="GB" />
    <input name="currency" type="hidden" value="GBP" />
    <input name="email" type="hidden" value="dave@gov.uk" />
    <input name="instId" type="hidden" value="eca6aba9-16b2-4ad0-8019-1212bbb2f152" />
    <input name="name" type="hidden" value="Prime Minister" />
    <input name="postcode" type="hidden" value="SW1A 2AA" />
    <input name="tel" type="hidden" value="020 7925 0918" />
    <input name="testMode" type="hidden" value="100" />
    <input name="town" type="hidden" value="London" />
    <input name="cardCVV" type="hidden" value="1234" />
    <input name="cardExp.month" type="hidden" value="1" />
    <input name="cardExp.year" type="hidden" value="2017" />

    <div class="form-group">
        <button class="btn btn-success btn-lg" type="submit">WorldPay Test Checkout</button>
    </div>
</form>

1 个答案:

答案 0 :(得分:1)

尝试通过WorldPay进行测试订单放置

<form action="/complete" id="paymentForm" method="post">

    <span id="paymentErrors"></span>

    <div class="form-row">
        <label>Name on Card</label>
        <input data-worldpay="name" name="name" type="text" />
    </div>
    <div class="form-row">
        <label>Card Number</label>
        <input data-worldpay="number" size="20" type="text" />
    </div>
    <div class="form-row">
        <label>CVC</label>
        <input data-worldpay="cvc" size="4" type="text" />
    </div>
    <div class="form-row">
        <label>Expiration (MM/YYYY)</label>
        <input data-worldpay="exp-month" size="2" type="text" />
        <label> / </label>
        <input data-worldpay="exp-year" size="4" type="text" />
    </div>

    <input type="submit" value="Place Order" />

</form>

然后在视图文件中添加此脚本

<script src="https://cdn.worldpay.com/v1/worldpay.js"></script>
    <script type="text/javascript">
        var form = document.getElementById('paymentForm');

        Worldpay.useOwnForm({
            'clientKey': 'Your_Client_Key',
            'form': form,
            'reusable': false,
            'callback': function (status, response) {
                document.getElementById('paymentErrors').innerHTML = '';
                if (response.error) {
                    Worldpay.handleError(form, document.getElementById('paymentErrors'), response.error);
                } else {
                    var token = response.token;
                    Worldpay.formBuilder(form, 'input', 'hidden', 'token', token);
                    console.log(token);

                    $.ajax({
                        url: "/Home/payment/",
                        data: { token: token },
                        success: function (data) {

                        },
                        dataType: "html",
                        type: "POST",
                        cache: false,
                        error: function () {
                            //Error Message
                        }
                    });

                    form.submit();
                }
            }
        });
</script>

现在在您的控制器中添加此订单放置代码

public ActionResult payment(string token)
{
    var restClient = new WorldpayRestClient("https://api.worldpay.com/v1", "Your_Service_Key");

    var orderRequest = new OrderRequest()
    {
        token = token,
        amount = 500,
        currencyCode = CurrencyCode.GBP.ToString(),
        name = "test name",
        orderDescription = "Order description",
        customerOrderCode = "Order code"

    };

    var address = new Address()
    {
        address1 = "123 House Road",
        address2 = "A village",
        city = "London",
        countryCode = CountryCode.GB,
        postalCode = "EC1 1AA"
    };

    orderRequest.billingAddress = address;

    try
    {
        OrderResponse orderResponse = restClient.GetOrderService().Create(orderRequest);
        Console.WriteLine("Order code: " + orderResponse.orderCode);
    }
    catch (WorldpayException e)
    {
        Console.WriteLine("Error code:" + e.apiError.customCode);
        Console.WriteLine("Error description: " + e.apiError.description);
        Console.WriteLine("Error message: " + e.apiError.message);
    }
    return Json(null, JsonRequestBehavior.AllowGet);
}