如何在C#中为Adyen加密卡数据?

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

How to encrypt card data for adyen in C#?

问题

有许多Python/JavaScript中的Adyen加密库,但我找不到一个适用于C#的。
我尝试使用SHA256+BASE64加密卡片详细信息并添加前缀+版本,但没有成功。这似乎对于加密字符串来说太短了。

你知道是否有适用于C#的Adyen加密库吗?
如果没有,我该如何编写一个?

英文:

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?

答案1

得分: 1

假设对于您的用例,您是完全 符合PCI规范,您将需要自己构建用户界面。您可以在 GitHub 上的 adyen-dotnet-library 中找到一些辅助函数。

或者,集成的最简单方法是使用 Adyen.Web 包 中的 drop-incomponents(用于客户端)。然后,可以安全地将加密的 state.data 传递给后续的请求。示例在这里

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

async function initCheckout() {
    try {
        const paymentMethodsResponse = await sendPostRequest("/api/getPaymentMethods");
        const configuration = {
            // 将 paymentMethodsResponse 传递给配置
            paymentMethodsResponse: paymentMethodsResponse,
            clientKey,
            locale: "en_US",
            environment: "test",
            showPayButton: true,
            paymentMethodsConfiguration: {
                ideal: {
                    showImage: true,
                },
                card: {
                    hasHolderName: true,
                    holderNameRequired: true,
                    name: "信用卡或借记卡",
                    amount: {
                        value: 10000,
                        currency: "EUR",
                    },
                },
                paypal: {
                    amount: {
                        value: 10000,
                        currency: "USD",
                    },
                    environment: "test", // 当您准备接受真实的 PayPal 付款时,请将其更改为 "live"
                    countryCode: "US", // 仅在测试时需要。在生产环境中,这将自动检索
                    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("发生错误。请查看控制台以获取详细信息。");
    }
}

// 当购物者选择支付按钮或需要额外信息以完成付款时调用的事件处理程序
async function handleSubmission(state, component, url) {
    try {
        const res = await sendPostRequest(url, state.data);
        handleServerResponse(res, component);
    } catch (error) {
        console.error(error);
        alert("发生错误。请查看控制台以获取详细信息。");
    }
}

// 发送 POST 请求到 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();
}

// 处理从服务器发送到客户端的响应
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();

希望这对您有所帮助!

英文:

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!

huangapple
  • 本文由 发表于 2023年7月13日 21:16:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679796.html
匿名

发表评论

匿名网友

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

确定