2012-06-11 28 views
29

Sau khi chơi xung quanh với Authorize.Net CIM XML API C# sample code, tôi bắt đầu sử dụng Authorize.Net C# SDK. Tôi có thể thêm thẻ tín dụng và tài khoản ngân hàng vào hồ sơ khách hàng bằng mã mẫu CIM XML API. Tuy nhiên, tôi không thấy cách thêm tài khoản ngân hàng bằng cách sử dụng SDK.Sử dụng tài khoản ngân hàng với Authorize.Net C# SDK

Thêm tài khoản ngân hàng với 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; 
... 

Sử dụng SDK tôi chỉ nhìn thấy một phương pháp .AddCreditCard(), nhưng không có cách nào để thêm một tài khoản ngân hàng. Khi tôi vòng qua tất cả PaymentProfiles tôi Nó ném một ngoại lệ khi nó đi qua một tài khoản ngân hàng quá:

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

Ngoại lệ:

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

enter image description here

Làm thế nào để thêm một tài khoản ngân hàng để một Hồ sơ CIM sử dụng SDK C# Authorize.Net?

Cập nhật:

Proof rằng CIM có thể lưu trữ thông tin tài khoản ngân hàng:

enter image description here

+0

@Ramhound để bạn nói rằng tôi không thể lưu trữ thông tin tài khoản ngân hàng bằng CIM? – Greg

+0

@Ramhound sử dụng mã mẫu CIM trên trang web của họ, họ cho phép bạn tạo hồ sơ thanh toán cho khách hàng của mình để khi họ đăng nhập lại, họ không phải nhập lại thông tin thanh toán (vì Authorize.Net CIM lưu nó) nên tôi không 't có để lưu nó hoặc thậm chí có quyền truy cập trực tiếp vào nó với ứng dụng của tôi – Greg

+2

@Ramhound Giải thích điều này sau đó https://dl.dropbox.com/u/3115379/ProofThatCIMStoresBankAccountInformation.png – Greg

Trả lời

9

Sau đây là thử nghiệm, nhưng chỉ chừng mực những gì các câu hỏi ban đầu lớn lên (Thử nghiệm nó nhiều hơn cho tôi?), tôi đã viết nó bằng cách sử dụng ví dụ XML được cung cấp và bằng cách sao chép mã cho mã AddCreditCard.

Khi bạn đều được thực hiện cập nhật các mã sau đây sẽ làm việc:

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

Trước tiên, tải mã nguồn C# cho các API từ http://developer.authorize.net/downloads/.

Khi xem lại mã, tôi có thể thấy 4 tệp sử dụng "creditCardType", đây là các tệp SubscriptionRequest.cs, CustomerGateway.cs, PaymentProfile.cs và AnetApiSchema.cs (lần cuối chúng tôi không phải chạm vào). Chúng ta cũng cần xem 'creditCardMaskedType', được sử dụng trong PaymentProfile.cs, Transaction.cs và AnetApiSchema.cs. Bất kỳ nơi nào các tệp này xuất hiện, chúng tôi cũng cần đảm bảo chúng tôi cũng hỗ trợ các cân bằng bankAccount.

Mở giải pháp AuthorizeNET. Chúng tôi sẽ nhảy xung quanh một chút thông qua các tập tin được liệt kê ở trên.

Trong CustomerGateway.cs thêm khối mã sau đây:

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

Trong PaymentProfile.cs thêm một số tài sản công cộng

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

Sửa đổi các khối sau của các nhà xây dựng 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; 
      } 
     } 

Thêm khối này đến PaymentProfile.ToAPI() phương pháp:

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

Thêm các thuộc tính nào sau đây để SubscriptionRequest.cs> Lớp SubscriptionRequest (xung quanh dòng 187)

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

Thêm mục khác nếu chặn TWICE để đăng kýYêu cầu. Lần đầu tiên là trong phương pháp ToAPI, thứ hai là trong phương pháp ToUpdateableAPI, trong cả hai trường hợp nó đi sau khi kiểm tra null số 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; 
     } 

Thêm các thuộc tính nào sau đây để Transaction.cs

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

Trong Transaction.cs trong NewFromResponse tĩnh (transactionDetailsType trans) phương pháp, tìm ra khối để kiểm tra cho trans.payment != null và tinh chỉnh như:

 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

Đẹp! Tôi hoàn toàn bị mất rằng có một nguồn tải về: -/ – Rup

+0

@ Cảm ơn bạn rất nhiều, điều này đang làm việc tuyệt vời – Greg

+0

Cảm ơn bạn rất nhiều cho mã mẫu này. Tôi đã hỗ trợ tại authorize.net nói với tôi rằng C# SDK không thể sử dụng CIM trừ khi nó được thực hiện thông qua SOAP/XML, mặc dù tôi đã chỉ ra cho họ rằng đã có các phương thức rõ ràng trong SDK để triển khai CIM. – Ricky

Các vấn đề liên quan