29

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 が銀行口座情報を保存できることの証明:

ここに画像の説明を入力

4

1 に答える 1

10

以下はテストされていますが、元の質問がもたらしたものについてのみ (もっとテストしてください?)、提供された 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());
        }

まず、API の C# ソース コードをhttp://developer.authorize.net/downloads/からダウンロードします。

コードを確認すると、「creditCardType」を使用する 4 つのファイルが見つかります。これらは、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 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; }

次の else if block TWICEを SubscriptionRequest に追加します。1 回目は ToAPI メソッドで、2 回目は ToUpdateableAPI メソッドで、どちらの場合も CC 番号の null チェックの後に行われます。

        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;
            }
        }
于 2012-07-13T17:58:31.540 に答える