gpt4 book ai didi

c# - 通过 Authorize.Net C# SDK 使用银行账户

转载 作者:IT王子 更新时间:2023-10-29 04:13:13 25 4
gpt4 key购买 nike

在玩弄了Authorize.Net CIM XML API C# sample code之后, 我开始使用 Authorize.Net C# SDK .我能够使用 CIM XML API 示例代码将信用卡和银行帐户添加到客户资料中。不过,我不知道如何使用 SDK 添加银行账户。

使用 CIM XML API 添加银行账户:

...
customerPaymentProfileType new_payment_profile = new customerPaymentProfileType();
paymentType new_payment = new paymentType();

bankAccountType new_bank = new bankAccountType();
new_bank.nameOnAccount = "xyz";
new_bank.accountNumber = "4111111";
new_bank.routingNumber = "325070760";
new_payment.Item = new_bank;

new_payment_profile.payment = new_payment;

createCustomerPaymentProfileRequest request = new createCustomerPaymentProfileRequest();
XmlAPIUtilities.PopulateMerchantAuthentication((ANetApiRequest)request);

request.customerProfileId = profile_id.ToString();
request.paymentProfile = new_payment_profile;
request.validationMode = validationModeEnum.testMode;
...

使用 SDK 我只看到 .AddCreditCard() 方法,但无法添加银行帐户。当我遍历所有 PaymentProfiles 时,它也会在遇到银行账户时抛出异常:

CustomerGateway cg = new CustomerGateway("xxx", "yyy");

foreach (string cid in cg.GetCustomerIDs())
{
Customer c = cg.GetCustomer(cid);
foreach (PaymentProfile pp in c.PaymentProfiles)
{
Console.WriteLine(pp.ToString());
}
}

异常(exception):

Unable to cast object of type 'AuthorizeNet.APICore.bankAccountMaskedType' to type 'AuthorizeNet.APICore.creditCardMaskedType'.

enter image description here

如何使用 Authorize.Net C# SDK 将银行账户添加到 CIM 配置文件?

更新:

CIM 可以存储银行账户信息的证明:

enter image description here

最佳答案

以下内容经过测试,但仅限于原始问题提出的内容(为我测试更多?),我使用提供的 XML 示例并通过复制 AddCreditCard 代码的代码来编写它。

当你全部完成更新后,下面的代码就可以工作了:

        var cg = new CustomerGateway("login", "transkey", ServiceMode.Test);
var c = cg.CreateCustomer("peter@example.com", "test customer");
//just to show that we didn't break CC
cg.AddCreditCard(c.ProfileID, "cc#", 07, 2011);
cg.AddBankAccount(c.ProfileID, "Peter", "bankaccoung#", "routing#");
//tostring doesn't actually do much... but if you break on it you can see the details for both the CC and the bank info.
foreach (PaymentProfile pp in cg.GetCustomer(c.ProfileID).PaymentProfiles)
{
Console.WriteLine(pp.ToString());
}

首先,从 http://developer.authorize.net/downloads/ 下载 API 的 C# 源代码.

在查看代码时,我可以看到 4 个使用“creditCardType”的文件,它们是 SubscriptionRequest.cs、CustomerGateway.cs、PaymentProfile.cs 和 AnetApiSchema.cs(最后一个我们不必触及)。我们还需要注意在 PaymentProfile.cs、Transaction.cs 和 AnetApiSchema.cs 中使用的“creditCardMaskedType”。这些文件出现的任何地方我们都需要确保我们也支持 bankAccount 等价物。

打开 AuthorizeNET 解决方案。我们将跳过上面列出的文件。

在 CustomerGateway.cs 中添加以下代码块:

    /// <summary>
/// Adds a bank account profile to the user and returns the profile ID
/// </summary>
/// <returns></returns>
public string AddBankAccount(string profileID, string nameOnAccount, string accountNumber, string routingNumber)
{
var req = new createCustomerPaymentProfileRequest();
req.customerProfileId = profileID;
req.paymentProfile = new customerPaymentProfileType();
req.paymentProfile.payment = new paymentType();

bankAccountType new_bank = new bankAccountType();
new_bank.nameOnAccount = nameOnAccount;
new_bank.accountNumber = accountNumber;
new_bank.routingNumber = routingNumber;

req.paymentProfile.payment.Item = new_bank;

var response = (createCustomerPaymentProfileResponse)_gateway.Send(req);

return response.customerPaymentProfileId;
}

在 PaymentProfile.cs 中添加一些公共(public)属性

    public string BankNameOnAccount {get; set; }
public string BankAccountNumber { get; set; }
public string BankRoutingNumber { get; set; }

修改 PaymentProfile(customerPaymentProfileMaskedType apiType) 构造函数的以下 block :

        if (apiType.payment != null) {
if(apiType.payment.Item is bankAccountMaskedType) {
var bankAccount = (bankAccountMaskedType)apiType.payment.Item;
this.BankNameOnAccount = bankAccount.nameOnAccount;
this.BankAccountNumber = bankAccount.accountNumber;
this.BankRoutingNumber = bankAccount.routingNumber;
}
else if (apiType.payment.Item is creditCardMaskedType)
{
var card = (creditCardMaskedType)apiType.payment.Item;
this.CardType = card.cardType;
this.CardNumber = card.cardNumber;
this.CardExpiration = card.expirationDate;
}
}

将此 block 添加到 PaymentProfile.ToAPI() 方法:

        if (!string.IsNullOrEmpty(this.BankAccountNumber))
{
bankAccountType new_bank = new bankAccountType();
new_bank.nameOnAccount = BankNameOnAccount;
new_bank.accountNumber = BankAccountNumber;
new_bank.routingNumber = BankRoutingNumber;

result.payment.Item = new_bank;
}

将以下公共(public)属性添加到 SubscriptionRequest.cs > SubscriptionRequest 类(第 187 行左右)

    public string BankNameOnAccount {get; set; }
public string BankAccountNumber { get; set; }
public string BankRoutingNumber { get; set; }

将以下 else if block TWICE 添加到 SubscriptionRequest。第一次是在 ToAPI 方法中,第二次是在 ToUpdateableAPI 方法中,在这两种情况下,它都在 CC 号码空检查之后。

        else if (!String.IsNullOrEmpty(this.BankAccountNumber))
{
bankAccountType new_bank = new bankAccountType();
new_bank.nameOnAccount = BankNameOnAccount;
new_bank.accountNumber = BankAccountNumber;
new_bank.routingNumber = BankRoutingNumber;

sub.payment = new paymentType();
sub.payment.Item = new_bank;
}

将以下公共(public)属性添加到 Transaction.cs

    public string BankNameOnAccount { get; set; }
public string BankAccountNumber { get; set; }
public string BankRoutingNumber { get; set; }

在静态 NewFromResponse(transactionDetailsType trans) 方法中的 Transaction.cs 中,找到检查 trans.payment != null 的 block 并进行调整,如下所示:

        if (trans.payment != null) {
if (trans.payment.Item.GetType() == typeof(creditCardMaskedType))
{
var cc = (creditCardMaskedType)trans.payment.Item;
result.CardNumber = cc.cardNumber;
result.CardExpiration = cc.expirationDate;
result.CardType = cc.cardType;
}
else if (trans.payment.Item.GetType() == typeof(bankAccountMaskedType))
{
var bankAccount = (bankAccountMaskedType)trans.payment.Item;
result.BankNameOnAccount = bankAccount.nameOnAccount;
result.BankAccountNumber = bankAccount.accountNumber;
result.BankRoutingNumber = bankAccount.routingNumber;
}
}

关于c# - 通过 Authorize.Net C# SDK 使用银行账户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10981540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com