Worldpay integration issue in MVC

痞子三分冷 提交于 2019-12-04 18:20:21

Try this for test Order placement through 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>

Then add this script in your view file

<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>

Now add this Order Placement code in your controller

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!