2012-06-11 21 views
29

在玩了Authorize.Net CIM XML API C# sample code之后,我开始使用Authorize.Net C# SDK。我可以使用CIM XML API示例代码将信用卡和银行帐户添加到客户配置文件中。但我不明白如何使用SDK添加银行账户。通过Authorize.Net使用银行账号C#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'. 

enter image description here

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

更新:

证明,CIM可以存储银行帐户信息:

enter image description here

+0

@Ramhound所以你说我不能使用CIM存储银行账户信息? – Greg

+0

@Ramhound在他们的网站上使用CIM示例代码,他们允许您为客户创建付款配置文件,这样当他们再次登录时,他们不必重新输入付款信息(因为Authorize.Net CIM保存它),所以我不用没必要保存它,甚至没有直接访问它与我的应用程序 – Greg

+2

@Ramhound解释这个然后https://dl.dropbox.com/u/3115379/ProofThatCIMStoresBankAccountInformation.png – Greg

回答

9

下面的测试,但只有这样了,因为它原来的问题提出了(详细测试一下对我而言),我使用提供的XML示例并通过复制AddCreditCard代码的代码来编写它。

当你全部完成更新下面的代码将工作:

 var cg = new CustomerGateway("login", "transkey", ServiceMode.Test); 
     var c = cg.CreateCustomer("[email protected]", "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/下载C#源代码的API。

在审查代码时,我可以看到4个使用“creditCardType”的文件,这些文件是SubscriptionRequest.cs,CustomerGateway.cs,PaymentProfile.cs和AnetApiSchema.cs(这是我们不必触碰的最后一个)。我们还需要注意'PaymentCardMaskedType',它在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; } 

添加以下否则,如果块商讯到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; } 

在Transaction.cs在静态NewFromResponse(transactionDetailsType反式)的方法,发现,检查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; 
      } 
     } 
+0

不错!我完全错过了有一个源代码下载: -/ – Rup

+0

@Peter非常感谢你,这是伟大的工作 – Greg

+0

非常感谢你这个示例代码。我在authorize.net上得到了支持,告诉我C#SDK不能使用CIM,除非它是通过SOAP/XML完成的,尽管我向他们指出SDK中有明确的方法来实现CIM。 – Ricky