读取 Adyen CreateCheckoutSessionResponse 对象作为 JSON 字符串。

huangapple go评论58阅读模式
英文:

Read Adyen CreateCheckoutSessionResponse object as json string

问题

以下是您要翻译的内容:

"My app is a Blazor Web Assembly client .net server...
I am calling my server (succesfully) to create a 'CreateCheckoutSessionResponse' object and return it to the client.
Looking at the browser, the response is a json coded string. This is from the debug preview tools in the chrome browser, looking at the response.. which seems fine:
读取 Adyen CreateCheckoutSessionResponse 对象作为 JSON 字符串。

Initially, I tried to just bring this into a CreateCheckoutSessionResponse object in the client service that calls the server:

public async Task GetAdyenPaymentSessionAsync(string userId, decimal amount)
{
AdyenSessionRequestDTO request = new AdyenSessionRequestDTO(userId: userId, currencyCode: "GBP", amount);
var response = await _http.PostAsJsonAsync(Routes.GetAdyenSessionRoute, request);
CreateCheckoutSessionResponse? res = await response.Content.ReadFromJsonAsync();
return res;
}

But, to instatiate the drop-in, I need to pass this session response as Json. The CreateCheckoutSessionResponse oject has a '.ToJson()'

So the complete call looks like this:

AdyenSessionResponse = await _transactionService.GetAdyenPaymentSessionAsync(_userService.CurrentUser.Id, CreditWanted);
AdyenSession = AdyenSessionResponse.ToJson();
await _JS.InvokeVoidAsync("setAdyenCheckoutConfiguration", AdyenSession, _transactionService.AdyenPublicClientKey);

I haven't really been able to get as far as debugging the javascript to see if the dropin gets instatiated, because the CreateCheckoutSessionResponse seems to be corrupted when operated on '.ToJson'.

读取 Adyen CreateCheckoutSessionResponse 对象作为 JSON 字符串。

This is the object after '.ToJson' loaded into a VSCode json view page:


'
{
"channel": "Web",
"amount": {
"currency": "GBP",
"value": 0
},
"countryCode": "NL",
"expiresAt": "2023-03-31T12:06:39+01:00",
"lineItems": [
{
"amountExcludingTax": 0,
"amountIncludingTax": 0,
"description": "Site Credit",
"quantity": 1,
"taxAmount": 0,
"taxPercentage": 0
}
],
"merchantAccount": "RedRocksTechnologyLtdECOM",
"reference": "000000113",
"returnUrl": "/redirect?orderRef=000000113",
"sessionData": "Ab02b4c0!B…c41T7F8gRuFdA/gf8NaFUV5OrVDhhMpKKaQp5ntwqhCmir9tvpqT+/z8/BsrrxmaseRwD06DHDbgtlqM40QV8IXV7kw7uIsoZVX8A9Ce+mAROdLvmQhrYZRX1psj0bZTWN/2fkiqTQhNHt6fYvBfZu0sTmhDiSt4f+E9TRjjChCFVeIU4iL7SLbh+3vVoIKWLxon7+DfImyPxrSNKrPDH18s+O4Ak1d5MyJYjrRXPSlrUxboI5Yfh6wLYDUrgvOpLR+sOBoL5bSqsOMX3FFKk8d6vbaRRaN2olMVqbHO7KYxJ/sKbKDoyDfTpeR/CojQSSabA4a45Ot7vMtPVhRy4eKfk4zWijeJSYC7ku0jK6ke9tJpIbUNaEYb3bthhgsc1ryYf8Ufz",
"shopperLocale": "en-US",
"splitCardFundingSources": false,
"threeDSAuthenticationOnly": false
}'

Which to be honest, doesnt look very much like good json...

So, I thought maybe a different approach...
Rather than use the 'CreateCheckoutSessionResponse' object, I'll try to read the json, straight out of the http response.. Chenged the the functions to this:

public async Task GetAdyenPaymentSessionAsync(string userId, decimal amount)
{
AdyenSessionRequestDTO request = new AdyenSessionRequestDTO(userId: userId, currencyCode: "GBP", amount);
var response = await _http.PostAsJsonAsync(Routes.GetAdyenSessionRoute, request);
string? jsonresponse = await response.Content.ReadAsStringAsync();
int length = jsonresponse.Length; // For Debugging

return jsonresponse;

}

Which I think should just return the raw string.. right? But when I do that, I am losing a lot of the information...


'
{
"channel": 3,
"recurringProcessingModel": null,
"shopperInteraction": null,
"accountInfo": null,
"additionalAmount": null,
"additionalData": null,
"allowedPaymentMethods": null,
"amount": {
"currency": "GBP",
"value": 0
},
"applicationInfo": null,
"authenticationData": null,
"billingAddress": null,
"blockedPaymentMethods": null,
"captureDelayHours": null,
"company": null,
"countryCode": "NL",
"dateOfBirth": null,
"deliverAt": null,
"deliveryAddress": null,
"enableOneClick": null,
"enablePayOut": null,
"enableRecurring": null,
"expiresAt": "…WMF4620wYAkhfLNzoDj2DP2NBpYUjm8gP0DY6MOTlcb0omHNkf7gQADv6eZMCq6tw+KQKtN8+XtAwCWQYdgrE7G+dj1KW0vlKPKJzv7dOloB8m/vXpkhZFBlfej0uTHJ0ir9BOoxKYjRv6mh3+e4VZeVjjsCfIrW8kfgYbnZ/ERb0Q==",
"shopperEmail": null,
"shopperIP": null,
"shopperLocale": "en-US",
"shopperName": null,
"shopperReference": null,
"shopperStatement": null,
"socialSecurityNumber": null,
"splitCardFundingSources": false,
"splits": null,
"store": null,
"storePaymentMethod": null,
"telephoneNumber": null,
"threeDSAuthenticationOnly": false,
"trustedShopper": null
}'

So I am

英文:

My app is a Blazor Web Assembly client .net server...
I am calling my server (succesfully) to create a 'CreateCheckoutSessionResponse' object and return it to the client.
Looking at the browser, the response is a json coded string. This is from the debug preview tools in the chrome browser, looking at the response.. which seems fine:
读取 Adyen CreateCheckoutSessionResponse 对象作为 JSON 字符串。

Initially, I tried to just bring this into a CreateCheckoutSessionResponse object in the client service that calls the server:

            public async Task<CreateCheckoutSessionResponse> GetAdyenPaymentSessionAsync(string userId, decimal amount)
            {
                AdyenSessionRequestDTO request = new AdyenSessionRequestDTO(userId: userId, currencyCode: "GBP", amount);
                var response = await _http.PostAsJsonAsync(Routes.GetAdyenSessionRoute, request);
                CreateCheckoutSessionResponse? res = await response.Content.ReadFromJsonAsync<CreateCheckoutSessionResponse>();
                return res;
            }

But, to instatiate the drop-in, I need to pass this session response as Json. The CreateCheckoutSessionResponse oject has a '.ToJson()'

So the complete call looks like this:

        AdyenSessionResponse = await _transactionService.GetAdyenPaymentSessionAsync(_userService.CurrentUser.Id, CreditWanted);
        AdyenSession = AdyenSessionResponse.ToJson();
        await _JS.InvokeVoidAsync("setAdyenCheckoutConfiguration", AdyenSession, _transactionService.AdyenPublicClientKey);

I haven't really been able to get as far as debugging the javascript to see if the dropin gets instatiated, because the CreateCheckoutSessionResponse seems to be corrupted when operated on '.ToJson'.

读取 Adyen CreateCheckoutSessionResponse 对象作为 JSON 字符串。

This is the object after '.ToJson' loaded into a VSCode json view page:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&#39;{\n  &quot;channel&quot;: &quot;Web&quot;,\n  &quot;amount&quot;: {\n    &quot;currency&quot;: &quot;GBP&quot;,\n    &quot;value&quot;: 0\n
    },\n  &quot;countryCode&quot;: &quot;NL&quot;,\n  &quot;expiresAt&quot;: &quot;2023-03-31T12:06:39+01:00&quot;,\n  &quot;lineItems&quot;: [\n    {\n      &quot;amountExcludingTax&quot;: 0,\n      &quot;amountIncludingTax&quot;: 0,\n      &quot;description&quot;: &quot;Site Credit&quot;,\n      &quot;quantity&quot;: 1,\n      &quot;taxAmount&quot;: 0,\n      &quot;taxPercentage&quot;: 0\n
        }\n
    ],\n  &quot;merchantAccount&quot;: &quot;RedRocksTechnologyLtdECOM&quot;,\n  &quot;reference&quot;: &quot;000000113&quot;,\n  &quot;returnUrl&quot;: &quot;/redirect?orderRef=000000113&quot;,\n  &quot;sessionData&quot;: &quot;Ab02b4c0!B…c41T7F8gRuFdA/gf8NaFUV5OrVDhhMpKKaQp5ntwqhCmir9tvpqT+/z8/BsrrxmaseRwD06DHDbgtlqM40QV8IXV7kw7uIsoZVX8A9Ce+mAROdLvmQhrYZRX1psj0bZTWN/2fkiqTQhNHt6fYvBfZu0sTmhDiSt4f+E9TRjjChCFVeIU4iL7SLbh+3vVoIKWLxon7+DfImyPxrSNKrPDH18s+O4Ak1d5MyJYjrRXPSlrUxboI5Yfh6wLYDUrgvOpLR+sOBoL5bSqsOMX3FFKk8d6vbaRRaN2olMVqbHO7KYxJ/sKbKDoyDfTpeR/CojQSSabA4a45Ot7vMtPVhRy4eKfk4zWijeJSYC7ku0jK6ke9tJpIbUNaEYb3bthhgsc1ryYf8Ufz&quot;,\n  &quot;shopperLocale&quot;: &quot;en-US&quot;,\n  &quot;splitCardFundingSources&quot;: false,\n  &quot;threeDSAuthenticationOnly&quot;: false\n
}&#39;

<!-- end snippet -->

Which to be honest, doesnt look very much like good json...

So, I thought maybe a different approach...
Rather than use the 'CreateCheckoutSessionResponse' object, I'll try to read the json, straight out of the http response.. Chenged the the functions to this:

             public async Task&lt;string&gt; GetAdyenPaymentSessionAsync(string userId, decimal amount)
            {
                AdyenSessionRequestDTO request = new AdyenSessionRequestDTO(userId: userId, currencyCode: &quot;GBP&quot;, amount);
                var response = await _http.PostAsJsonAsync(Routes.GetAdyenSessionRoute, request);
                string? jsonresponse = await response.Content.ReadAsStringAsync();
                int length = jsonresponse.Length; // For Debugging

                return jsonresponse;
            }

Which I think should just return the raw string.. right? But when I do that, I am losing a lot of the information...

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&#39;{
    &quot;channel&quot;: 3,
    &quot;recurringProcessingModel&quot;: null,
    &quot;shopperInteraction&quot;: null,
    &quot;accountInfo&quot;: null,
    &quot;additionalAmount&quot;: null,
    &quot;additionalData&quot;: null,
    &quot;allowedPaymentMethods&quot;: null,
    &quot;amount&quot;: {
        &quot;currency&quot;: &quot;GBP&quot;,
        &quot;value&quot;: 0
    },
    &quot;applicationInfo&quot;: null,
    &quot;authenticationData&quot;: null,
    &quot;billingAddress&quot;: null,
    &quot;blockedPaymentMethods&quot;: null,
    &quot;captureDelayHours&quot;: null,
    &quot;company&quot;: null,
    &quot;countryCode&quot;: &quot;NL&quot;,
    &quot;dateOfBirth&quot;: null,
    &quot;deliverAt&quot;: null,
    &quot;deliveryAddress&quot;: null,
    &quot;enableOneClick&quot;: null,
    &quot;enablePayOut&quot;: null,
    &quot;enableRecurring&quot;: null,
    &quot;expiresAt&quot;: &quot;…WMF4620wYAkhfLNzoDj2DP2NBpYUjm8gP0DY6MOTlcb0omHNkf7gQADv6eZMCq6tw+KQKtN8+XtAwCWQYdgrE7G+dj1KW0vlKPKJzv7dOloB8m/vXpkhZFBlfej0uTHJ0ir9BOoxKYjRv6mh3+e4VZeVjjsCfIrW8kfgYbnZ/ERb0Q==&quot;,
    &quot;shopperEmail&quot;: null,
    &quot;shopperIP&quot;: null,
    &quot;shopperLocale&quot;: &quot;en-US&quot;,
    &quot;shopperName&quot;: null,
    &quot;shopperReference&quot;: null,
    &quot;shopperStatement&quot;: null,
    &quot;socialSecurityNumber&quot;: null,
    &quot;splitCardFundingSources&quot;: false,
    &quot;splits&quot;: null,
    &quot;store&quot;: null,
    &quot;storePaymentMethod&quot;: null,
    &quot;telephoneNumber&quot;: null,
    &quot;threeDSAuthenticationOnly&quot;: false,
    &quot;trustedShopper&quot;: null
}&#39;

<!-- end snippet -->

So I am losing something in the 'ReadAsString' functionality.

Any pointers where this could be going wrong?
Thanks.

答案1

得分: 3

Ok, I think I solved it. The text is being decoded with UTF8 as a default.

Stream? jsonresponse = await response.Content.ReadAsStreamAsync();
string res;
using (var reader = new StreamReader(jsonresponse, Encoding.ASCII))
{
res = reader.ReadToEnd();
}
return res;

This returns the correct information.

英文:

Ok, I think I solved it. The text is being decoded with UTF8 as a default.

        Stream? jsonresponse = await response.Content.ReadAsStreamAsync();
        string res;
        using (var reader = new StreamReader(jsonresponse, Encoding.ASCII))
        {
            res = reader.ReadToEnd();
        }
        return res;

This returns the correct information.

huangapple
  • 本文由 发表于 2023年3月31日 18:38:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定