簡體   English   中英

使用Authorize.Net C#SDK使用銀行帳戶

[英]Using Bank Accounts With Authorize.Net C# SDK

在使用Authorize.Net CIM XML API C#示例代碼之后 ,我開始使用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());
    }
}

例外:

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

在此輸入圖像描述

如何使用Authorize.Net C#SDK將銀行帳戶添加到CIM配置文件?

更新:

證明CIM可以存儲銀行賬戶信息:

在此輸入圖像描述

以下是經過測試的,但只是原始問題的出現(對我來說測試更多?),我使用提供的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(最后一個我們不必觸摸)。 我們還需要注意'creditCardMaskedType',它在PaymentProfile.cs,Transaction.cs和AnetApiSchema.cs中使用。 這些文件顯示的任何地方我們都需要確保我們也支持bankAccount equivelants。

打開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 string BankNameOnAccount {get; set; }
    public string BankAccountNumber { get; set; }
    public string BankRoutingNumber { get; set; }

修改PaymentProfile(customerPaymentProfileMaskedType apiType)構造函數的以下塊:

        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;
            }
        }

將此塊添加到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;
        }

將以下公共屬性添加到SubscriptionRequest.cs> SubscriptionRequest類(第187行)

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

如果阻止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;
        }

將以下公共屬性添加到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的塊並調整如下所示:

        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;
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM