<commit_before>﻿using AuthorizeNetCore.Models;
using System;
using System.Collections.Generic;
using System.Text;

namespace AuthorizeNetCore
{
    public class CreditCard
    {
        private readonly string _authorizeNetUrl;
        private readonly string _apiLoginId;
        private readonly string _transactionKey;

        public CreditCard(string authorizeNetUrl, string apiLoginId, string transactionKey)
        {
            _authorizeNetUrl = authorizeNetUrl;
            _apiLoginId = apiLoginId;
            _transactionKey = transactionKey;
        }

        public TransactionResponse Charge(ChargeCreditCardRequest chargeCreditCardRequest)
        {
            return new TransactionResponse();
        }

        public TransactionResponse Charge(string nounce, string referenceId, decimal amount, string customerId, string customerIpAddress)
        {
            // Build the request
            var chargeCreditCardRequest = new ChargeCreditCardRequest
            {
                CreateTransactionRequest = new CreateTransactionRequest
                {
                    MerchantAuthentication = new MerchantAuthentication { LoginId = _apiLoginId, TransactionKey = _transactionKey },
                    ReferenceId = referenceId,
                    TransactionRequest = new TransactionRequest
                    {
                        Amount = amount.ToString(),
                        BillTo = new CustomerContact(),
                        Customer = new Customer { Id = customerId },
                        CustomerIP = customerIpAddress,
                        Duty = new Duty(),
                        LineItems = new LineItems { LineItem = new LineItem[0] },
                        Payment = new Payment
                        {
                            OpaqueData = new OpaqueData { NonceValue = nounce }
                        },
                        PoNumber = "",
                        Shipping = new Shipping(),
                        ShipTo = new CustomerContact(),
                        Tax = new Tax(),
                        TransactionSettings = new TransactionSettings { Setting = new Setting[0] },
                        UserFields = new UserFields { UserField = new UserField[0] }
                    }
                }
            };

            return Charge(chargeCreditCardRequest);
        }
    }
}
<commit_msg>Implemented ChargeAsync methods and connecting to Authorize.net<commit_after>﻿using AuthorizeNetCore.Models;
using Newtonsoft.Json;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace AuthorizeNetCore
{
    public class CreditCard
    {
        private readonly string _authorizeNetUrl;
        private readonly string _apiLoginId;
        private readonly string _transactionKey;

        public CreditCard(string authorizeNetUrl, string apiLoginId, string transactionKey)
        {
            _authorizeNetUrl = authorizeNetUrl;
            _apiLoginId = apiLoginId;
            _transactionKey = transactionKey;
        }

        public async Task<ChargeCreditCardResponse> ChargeAsync(ChargeCreditCardRequest chargeCreditCardRequest)
        {
            // Serialize the ChargeCreditCardRequest object
            var stringContent = new StringContent(JsonConvert.SerializeObject(chargeCreditCardRequest), Encoding.UTF8, "application/json");

            // Connect to Authorize.net
            var httpClient = new HttpClient();
            var response = await httpClient.PostAsync(_authorizeNetUrl, stringContent);

            // If response is not successful, return appropriate transaction response
            if (!response.IsSuccessStatusCode)
            {
                var resultMessage = new ResultMessage { Code = response.StatusCode.ToString(), Text = response.ReasonPhrase };
                var resultMessages = new ResultMessage[1];
                resultMessages[0] = resultMessage;
                return new ChargeCreditCardResponse { Results = new Results { ResultCode = "Error", ResultMessages = resultMessages } };
            }

            // Deserialize the response content
            var json = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<ChargeCreditCardResponse>(json);
        }

        public async Task<ChargeCreditCardResponse> ChargeAsync(string nounce, string referenceId, decimal amount, string customerId, string customerIpAddress)
        {
            // Build the request
            var chargeCreditCardRequest = new ChargeCreditCardRequest
            {
                CreateTransactionRequest = new CreateTransactionRequest
                {
                    MerchantAuthentication = new MerchantAuthentication { LoginId = _apiLoginId, TransactionKey = _transactionKey },
                    ReferenceId = referenceId,
                    TransactionRequest = new TransactionRequest
                    {
                        Amount = amount.ToString(),
                        BillTo = new CustomerContact(),
                        Customer = new Customer { Id = customerId },
                        CustomerIP = customerIpAddress,
                        Duty = new Duty(),
                        LineItems = new LineItems { LineItem = new LineItem[0] },
                        Payment = new Payment
                        {
                            OpaqueData = new OpaqueData { NonceValue = nounce }
                        },
                        PoNumber = "",
                        Shipping = new Shipping(),
                        ShipTo = new CustomerContact(),
                        Tax = new Tax(),
                        TransactionSettings = new TransactionSettings { Setting = new Setting[0] },
                        UserFields = new UserFields { UserField = new UserField[0] }
                    }
                }
            };

            return await ChargeAsync(chargeCreditCardRequest);
        }
    }
}
{'fraction_non_alphanumeric': 0.05949197860962567, 'fraction_numerical': 0.0015040106951871657, 'mean_word_length': 1.676654740608229, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6934437', 'n_tokens_mistral': 1306, 'n_tokens_neox': 1212, 'n_words': 339}