1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- using System;
- using RestSharp;
- using Newtonsoft.Json;
- using MTWorkHR.Core.Global;
- using MTWorkHR.Application.Services.Interfaces;
- namespace MTWorkHR.Application.Services.Payment
- {
- public class MatchMoveService: IMatchMoveService
- {
- private readonly string _baseUrl;
- private readonly string _clientId;
- private readonly string _clientSecret;
- private readonly AppSettingsConfiguration _configuration;
- public MatchMoveService(AppSettingsConfiguration configuration)
- {
- _configuration = configuration;
- _baseUrl = configuration.MatchMoveSettings.BaseUrl;
- _clientId = configuration.MatchMoveSettings.ClientId;
- _clientSecret = configuration.MatchMoveSettings.ClientSecret;
- }
- /// <summary>
- /// Retrieves an OAuth token from MatchMove.
- /// </summary>
- /// <returns>Access token string.</returns>
- public async Task<string> GetAccessTokenAsync()
- {
- var client = new RestClient(_baseUrl);
- var request = new RestRequest("/oauth/token")
- {
- Method = Method.Post
- };
- // Add required headers and body parameters
- request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
- request.AddParameter("grant_type", "client_credentials");
- request.AddParameter("client_id", _clientId);
- request.AddParameter("client_secret", _clientSecret);
- var response = await client.ExecuteAsync(request);
- if (!response.IsSuccessful)
- {
- throw new Exception($"Error retrieving access token: {response.StatusCode} - {response.ErrorMessage}");
- }
- var token = response.Content; // Parse JSON response to extract the token if needed
- return token;
- }
- /// <summary>
- /// Processes a payment using MatchMove's API.
- /// </summary>
- /// <param name="token">OAuth token.</param>
- /// <param name="paymentRequest">Payment details as an object.</param>
- /// <returns>Response from MatchMove API.</returns>
- public async Task<string> ProcessPaymentAsync(string token, object paymentRequest)
- {
- var client = new RestClient(_baseUrl);
- var request = new RestRequest("/payments")
- {
- Method = Method.Post
- };
- // Add required headers
- request.AddHeader("Authorization", $"Bearer {token}");
- request.AddHeader("Content-Type", "application/json");
- // Add JSON body
- request.AddJsonBody(paymentRequest);
- var response = await client.ExecuteAsync(request);
- if (!response.IsSuccessful)
- {
- throw new Exception($"Error processing payment: {response.StatusCode} - {response.ErrorMessage}");
- }
- return response.Content;
- }
- }
- }
|