Я использую RestSharp для отправки запросов и получения ответов от API платежного шлюза. Я не уверен, как хранить ответы API в моей таблице базы данных при получении. Вот что я написал до сих пор:
public static void Main(string[] args)
{
var merchantSignupResponse = MerchantSignUpForProPay();
}
private static Task<ProPayResponse> MerchantSignUpForProPay()
{
var baseUrl = "https://xmltest.propay.com/api/propayapi/PropayMerchantService.svc/";
var response = new RestResponse();
var request = BuildMerchantTestData();
var restRequest = CreateRestRequest("/Signup", Method.PUT);
try
{
restRequest.AddJsonBody(request);
//return Execute<SignupResponse>(restRequest, baseUrl);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return Execute(response, baseUrl);
}
private static SignUpRequest BuildMerchantTestData()
{
using(var context = new DNNContext()) {
var Onboarding = new Onboarding();
var userid = Onboarding.UserName;
var email = Onboarding.Email;
var signupRequest = new SignUpRequest
{
SignupAccountData = new SignupAccountData
{
ExternalId = "12345",
Tier = "Premium",
CurrencyCode = "USD",
PhonePIN = Onboarding.PhonePin,
UserId = Onboarding.UserName,
},
Address =
new Address
{
Address1 = Onboarding.Address1Line1,
Address2 = Onboarding.Address1Line2,
ApartmentNumber = "",
City = Onboarding.Address1City,
State = Onboarding.Address1State,
Country = Onboarding.Address1Country,
Zip = Onboarding.Address1ZipCode
},
BusinessAddress =
new Address
{
Address1 = Onboarding.BusinessAddressLine1,
Address2 = Onboarding.BusinessAddressLine2,
ApartmentNumber = "",
City = Onboarding.BusinessCity,
State = Onboarding.BusinessState,
Country = Onboarding.BusinessCountry,
Zip = Onboarding.BusinessZipCode
},
MailAddress = new Address { Address1 = Onboarding.OwnerAddress, City = Onboarding.OwnerCity, State = Onboarding.OwnerRegion, Country = Onboarding.OwnerCountry, Zip =Onboarding.OwnerZipCode },
BankAccount =
new BankAccount
{
AccountCountryCode = Onboarding.BankAccount1CountryCode,
AccountType = Onboarding.BankAccount1Type,
AccountOwnershipType = Onboarding.BankAccount1OwnershipType,
BankAccountNumber = Onboarding.BankAccount1Number,
BankName = Onboarding.BankAccount1BankName,
RoutingNumber = Onboarding.BankAccount1RoutingNumber
},
SecondaryBankAccount =
new BankAccount
{
AccountCountryCode = Onboarding.BankAccount2CountryCode,
AccountType = Onboarding.BankAccount2Type,
AccountOwnershipType = Onboarding.BankAccount2OwnershipType,
BankAccountNumber = Onboarding.BankAccount2Number,
BankName = Onboarding.BankAccount2BankName,
RoutingNumber = Onboarding.BankAccount2RoutingNumber
},
BusinessData =
new BusinessData
{
BusinessLegalName = Onboarding.BusinessLegalName,
DoingBusinessAs = Onboarding.DoingBusinessAs,
EIN = Onboarding.Ein,
},
CreditCardData = new CreditCardData
{
CreditCardNumber = Onboarding.CreditCardNumber, // test card number
ExpirationDate = Onboarding.ExpirationDate
},
PersonalData =
new PersonalData
{
DateOfBirth = Onboarding.DateOfBirth.ToString(),
SourceEmail = Onboarding.Email,
SocialSecurityNumber = Onboarding.Ssn,
FirstName = Onboarding.FirstName,
LastName = Onboarding.Lastname,
MiddleInitial = Onboarding.MiddleInitial,
PhoneInformation =
new PhoneInformation { DayPhone = Onboarding.DayPhone, EveningPhone = Onboarding.EveningPhone }
}
};
return signupRequest;
}
}
private static RestRequest CreateRestRequest(string resource, Method method)
{
var credentials = GetCredentials();
var restRequest = new RestRequest { Resource = resource, Method = method, RequestFormat = DataFormat.Json, };
restRequest.AddHeader("accept", "application/json");
restRequest.AddHeader("Authorization", credentials);
return restRequest;
}
private static async Task<ProPayResponse> Execute(RestResponse response, string baseUrl)
{
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
PortalModuleBase pmb = new PortalModuleBase();
XItem xi = new XItem();
JsonSerializer jsonSerializer = new JsonSerializer();
RestClient client = new RestClient(baseUrl);
IRestRequest request = new RestRequest("/signup", Method.POST);
request.AddHeader("Accept", "application/json");
string jsonObject = JsonConvert.SerializeObject(response, (Newtonsoft.Json.Formatting) Formatting.Indented, jsonSerializerSettings);
request.AddParameter("application/json", jsonObject, ParameterType.RequestBody);
TaskCompletionSource<IRestResponse> taskCompletion = new TaskCompletionSource<IRestResponse>();
RestRequestAsyncHandle handle = client.ExecuteAsync(
request, r => taskCompletion.SetResult(r));
response = (RestResponse)(await taskCompletion.Task);
if (response.ErrorException != null)
{
Console.WriteLine(
"error: Exception: {0}, Headers: {1}, Content: {2}, Status Code: {3}",
response.ErrorException,
response.Headers,
response.Content,
response.StatusCode);
}
return JsonConvert.DeserializeObject<ProPayResponse>(response.Content);
}
Действую ли я в правильном направлении в отношении вышеуказанного кода? Если нет, какие изменения необходимы для обеспечения сохранения ответов API в базе данных?