如何使用C#为Worldpay API生成令牌

时间:2019-04-25 16:59:30

标签: api worldpay

我想获得在Wordpay设备中使用的令牌以集成到C#WPF应用程序中

我已经下载了这个https://github.com/worldpay/worldpay-lib-dotnet库,并将其包含在我的项目中

curl request to generate token is 
url https://api.worldpay.com/v1/tokens 
-H "Content-type: application/json" 
-X POST 
-d '{
    "reusable": true/false,
    "paymentMethod": {
        "name": "name",
        "expiryMonth": 2,
        "expiryYear": 2015,
        "issueNumber": 1,
        "startMonth": 2,
        "startYear": 2013,
        "cardNumber": "4444 3333 2222 1111",
        "type": "Card",
        "cvc": "123"
    },
    "clientKey": "T_C_client_key"
}

我想将上述curl请求转换为c#以生成令牌。

       System.Net.ServicePointManager.SecurityProtocol = 
        SecurityProtocolType.Tls12;
        Worldpay.Sdk.WorldpayRestClient restClient = new Worldpay.Sdk.WorldpayRestClient("https://api.worldpay.com/v1", "T_S_11cca65b-c15a-467c-8561-35ecfa07725b");


        var orderRequest = new OrderRequest()
        {
            amount = 1999,
            currencyCode = CurrencyCode.GBP.ToString(),
            name = "Joe Bloggs",
            orderDescription = "Order description",
            token= "where to get this "
        };

        var address = new Address()
        {
            address1 = "line 1",
            address2 = "line 2",
            city = "city",
            countryCode = CountryCode.GB.ToString(),
            postalCode = "AB1 2CD"
        };

        orderRequest.billingAddress = address;

        try
        {
            OrderResponse orderResponse = restClient.GetOrderService().Create(orderRequest);
           MessageBox.Show("Order code: " + orderResponse.orderCode);
        }
        catch (WorldpayException er)
        {
            MessageBox.Show("Error code:" + er.apiError.customCode);
            MessageBox.Show("Error description: " + er.apiError.description);
            MessageBox.Show("Error message: " + er.apiError.message);
        }

1 个答案:

答案 0 :(得分:0)

这是我使用API​​服务器端创建令牌的方式

internal static string CreateToken(AuthService authService, string card, string cvv,string name, int month,int year)
    {
        var tokenRequest = new TokenRequest();
        tokenRequest.clientKey = Configuration.ClientKey;

        var cardRequest = new CardRequest();
        cardRequest.cardNumber = card;
        cardRequest.cvc = cvv;
        cardRequest.name = name;
        cardRequest.expiryMonth = 2;
        cardRequest.expiryYear = 2021;
        cardRequest.type = "Card";

        tokenRequest.paymentMethod = cardRequest;

        TokenResponse response
            = authService.GetToken(tokenRequest);
        return response.token;
    }