2011-12-09 56 views
0

您好我正在嘗試在我的asp.net網站上集成worldpay。 我已經使用此代碼來實現集成。worldpay集成asp.net

//test environment url 
string url = "https://secure-test.worldpay.com/wcc/purchase"; 

//get all form elements 
NameValueCollection formData = new NameValueCollection(); 
formData["testMode"] = "100"; 

//all the form fields here 

//make the call to submit form data 
WebClient webClient = new WebClient(); 
byte[] responseBytes = webClient.UploadValues(url, "POST", formData); 
string response = Encoding.UTF8.GetString(responseBytes);    
inputdiv.Visible = false; 
outputdiv.Visible = true; 
outputdiv.InnerHtml = response;  

基本上我得到響應,並在div顯示它。一切正常,但鏈接有相對路徑,不應該如此。除了圖像網址之外,所有其他網址都應該指向worldpay。如何實現這一目標? 任何建議將不勝感激。

+0

我試過的方法與string.replace,但沒有運氣 – sampath

+0

我需要世界薪酬與整合我ASP.NET網站使用c#作爲後端語言。請幫助我如何使用這個,因爲我沒有使用任何支付網關之前,這就是爲什麼我需要你的幫助 –

+0

@AqEeLBaLoCh我已添加示例工作代碼希望這會幫助你很多。 – Slan

回答

0

請試試這個,希望這會有很大的幫助。

的Html

<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

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

即成端代碼C#

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); 
}