itsReinsy
itsReinsy

Reputation: 45

How to encrypt card data for adyen in C#?

There are lot of adyen encryption libraries in python/js, but i couldn't find one for C#. I have tried encrypting card details with SHA256+BASE64 and adding the prefix+version, but not worked. It seems so short for a encryption string.

Do you know any adyen encryption library for C# If not, how can i write one?

Upvotes: 1

Views: 470

Answers (1)

Kwok He
Kwok He

Reputation: 251

Assuming that, for your use-case, you are fully PCI-compliant, you'll have to build the UI yourself. There are helper functions that can find in the adyen-dotnet-library on GitHub.

Alternatively, the easiest way to integrate is using either the drop-in or components from the Adyen.Web package (used on the client-side). The encrypted state.data can then be safely passed on subsequent requests to your backend. See example here.

const clientKey = document.getElementById("clientKey").innerHTML;
const type = document.getElementById("type").innerHTML;

async function initCheckout() {
    try {
        const paymentMethodsResponse = await sendPostRequest("/api/getPaymentMethods");
        const configuration = {
            // Pass paymentMethodsResponse to configuration
            paymentMethodsResponse: paymentMethodsResponse,
            clientKey,
            locale: "en_US",
            environment: "test",
            showPayButton: true,
            paymentMethodsConfiguration: {
                ideal: {
                    showImage: true,
                },
                card: {
                    hasHolderName: true,
                    holderNameRequired: true,
                    name: "Credit or debit card",
                    amount: {
                        value: 10000,
                        currency: "EUR",
                    },
                },
                paypal: {
                    amount: {
                        value: 10000,
                        currency: "USD",
                    },
                    environment: "test", // Change this to "live" when you're ready to accept live PayPal payments
                    countryCode: "US", // Only needed for test. This will be automatically retrieved when you are in production
                    onCancel: (data, component) => {
                        component.setStatus('ready');
                    },
                }
            },
            onSubmit: (state, component) => {
            },
            onAdditionalDetails: (state, component) => {
                handleSubmission(state, component, "/api/submitAdditionalDetails");
            },
        };
        const checkout = await new AdyenCheckout(configuration);
        checkout.create(type).mount(document.getElementById("payment"));
    } catch (error) {
        console.error(error);
        alert("Error occurred. Look at console for details");
    }
}

// Event handlers called when the shopper selects the pay button,
// or when additional information is required to complete the payment
async function handleSubmission(state, component, url) {
    try {
        const res = await sendPostRequest(url, state.data);
        handleServerResponse(res, component);
    } catch (error) {
        console.error(error);
        alert("Error occurred. Look at console for details");
    }
}

// Sends POST request to url
async function sendPostRequest(url, data) {
    const res = await fetch(url, {
        method: "POST",
        body: data ? JSON.stringify(data) : "",
        headers: {
            "Content-Type": "application/json",
        },
    });

    return await res.json();
}

// Handles responses sent from your server to the client
function handleServerResponse(res, component) {
    if (res.action) {
        component.handleAction(res.action);
    } else {
        switch (res.resultCode) {
            case "Authorised":
                window.location.href = "/result/success";
                break;
            case "Pending":
            case "Received":
                window.location.href = "/result/pending";
                break;
            case "Refused":
                window.location.href = "/result/failed";
                break;
            default:
                window.location.href = "/result/error";
                break;
        }
    }
}

initCheckout();

Hope this helped!

Upvotes: 1

Related Questions