2012-06-11 18 views
29

Dopo aver giocato con lo Authorize.Net CIM XML API C# sample code, ho iniziato a utilizzare Authorize.Net C# SDK. Sono in grado di aggiungere carte di credito e conti bancari ai profili dei clienti utilizzando il codice di esempio dell'API XML CIM. Però non vedo come aggiungere conti bancari usando l'SDK.Utilizzo di conti bancari con Authorize.Net C# SDK

Aggiunta di conto bancario con CIM API XML:

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

Utilizzando l'SDK vedo solo un metodo .AddCreditCard(), ma c'è modo di aggiungere un conto bancario. Quando ho ciclo attraverso tutto il mio PaymentProfiles viene generata un'eccezione quando si incontra un conto bancario troppo:

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

Eccezione:

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

enter image description here

Come faccio ad aggiungere un conto bancario per un Profilo CIM utilizzando Authorize.Net C# SDK?

Aggiornamento:

La prova che CIM può memorizzare le informazioni di conto bancario:

enter image description here

+0

@Ramhound quindi stai dicendo che non posso memorizzare le informazioni del conto bancario utilizzando CIM? – Greg

+0

@Ramhound che utilizza il codice di esempio CIM sul proprio sito consente di creare profili di pagamento per i clienti in modo che quando effettuano di nuovo l'accesso non debbano immettere nuovamente le informazioni di pagamento (poiché CIM di Authorize.Net lo salva) quindi non Deve salvarlo o anche avere accesso diretto ad esso con la mia applicazione – Greg

+2

@Ramhound Spiegare questo poi https://dl.dropbox.com/u/3115379/ProofThatCIMStoresBankAccountInformation.png – Greg

risposta

9

effettuano i seguenti controlli, ma solo in quanto ciò che la domanda iniziale cresciuto (test più per me?), l'ho scritto utilizzando l'esempio XML fornito e copiando il codice per il codice AddCreditCard.

Quando si è tutto fatto aggiornando il seguente codice funzionerà:

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

In primo luogo, scaricare il codice sorgente C# per l'API da http://developer.authorize.net/downloads/.

Nella revisione del codice sono in grado di vedere 4 file che utilizzano "creditCardType", questi sono SubscriptionRequest.cs, CustomerGateway.cs, PaymentProfile.cs e AnetApiSchema.cs (quest'ultimo non dobbiamo toccare). Dobbiamo anche fare attenzione a "creditCardMaskedType", che viene utilizzato in PaymentProfile.cs, Transaction.cs e AnetApiSchema.cs. In ogni posto in cui si trovano questi file, dobbiamo assicurarci di supportare anche gli equivelants bankAccount.

Aprire la soluzione AuthorizeNET. Salteremo un po 'tra i file sopra elencati.

Nel CustomerGateway.cs aggiungere la seguente blocco di codice:

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

In PaymentProfile.cs aggiungere alcune proprietà pubbliche

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

Modificare il blocco successivo del costruttore 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; 
      } 
     } 

Aggiungi questo blocco al metodo 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; 
     } 

Aggiungere le seguenti proprietà pubbliche a SubscriptionRequest.cs> SubscriptionRequest class (sulla linea 187)

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

Aggiungere il seguente codice se il blocco TWICE in SubscriptionRequest. La prima volta è nel metodo ToAPI, il secondo è nel metodo ToUpdateableAPI, in entrambi i casi viene eseguito dopo il controllo Null del numero 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; 
     } 

Aggiungere le seguenti proprietà pubbliche per Transaction.cs

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

In Transaction.cs nel NewFromResponse statico (transactionDetailsType trans) metodo, trovare il blocco che controlla trans.payment != null e modificare, come indicato:

 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

Bello! Mi ero completamente perso dal download di una fonte: -/ – Rup

+0

@Peter grazie mille, funziona perfettamente – Greg

+0

Grazie mille per questo codice di esempio. Avevo il supporto di authorize.net che mi diceva che C# SDK non poteva usare CIM a meno che non fosse stato fatto tramite SOAP/XML, anche se stavo sottolineando che c'erano chiaramente metodi all'interno dell'SDK per implementare CIM. – Ricky