MatchMoveService.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using RestSharp;
  3. using Newtonsoft.Json;
  4. using MTWorkHR.Core.Global;
  5. using MTWorkHR.Application.Services.Interfaces;
  6. namespace MTWorkHR.Application.Services.Payment
  7. {
  8. public class MatchMoveService: IMatchMoveService
  9. {
  10. private readonly string _baseUrl;
  11. private readonly string _clientId;
  12. private readonly string _clientSecret;
  13. private readonly AppSettingsConfiguration _configuration;
  14. public MatchMoveService(AppSettingsConfiguration configuration)
  15. {
  16. _configuration = configuration;
  17. _baseUrl = configuration.MatchMoveSettings.BaseUrl;
  18. _clientId = configuration.MatchMoveSettings.ClientId;
  19. _clientSecret = configuration.MatchMoveSettings.ClientSecret;
  20. }
  21. /// <summary>
  22. /// Retrieves an OAuth token from MatchMove.
  23. /// </summary>
  24. /// <returns>Access token string.</returns>
  25. public async Task<string> GetAccessTokenAsync()
  26. {
  27. var client = new RestClient(_baseUrl);
  28. var request = new RestRequest("/oauth/token")
  29. {
  30. Method = Method.Post
  31. };
  32. // Add required headers and body parameters
  33. request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
  34. request.AddParameter("grant_type", "client_credentials");
  35. request.AddParameter("client_id", _clientId);
  36. request.AddParameter("client_secret", _clientSecret);
  37. var response = await client.ExecuteAsync(request);
  38. if (!response.IsSuccessful)
  39. {
  40. throw new Exception($"Error retrieving access token: {response.StatusCode} - {response.ErrorMessage}");
  41. }
  42. var token = response.Content; // Parse JSON response to extract the token if needed
  43. return token;
  44. }
  45. /// <summary>
  46. /// Processes a payment using MatchMove's API.
  47. /// </summary>
  48. /// <param name="token">OAuth token.</param>
  49. /// <param name="paymentRequest">Payment details as an object.</param>
  50. /// <returns>Response from MatchMove API.</returns>
  51. public async Task<string> ProcessPaymentAsync(string token, object paymentRequest)
  52. {
  53. var client = new RestClient(_baseUrl);
  54. var request = new RestRequest("/payments")
  55. {
  56. Method = Method.Post
  57. };
  58. // Add required headers
  59. request.AddHeader("Authorization", $"Bearer {token}");
  60. request.AddHeader("Content-Type", "application/json");
  61. // Add JSON body
  62. request.AddJsonBody(paymentRequest);
  63. var response = await client.ExecuteAsync(request);
  64. if (!response.IsSuccessful)
  65. {
  66. throw new Exception($"Error processing payment: {response.StatusCode} - {response.ErrorMessage}");
  67. }
  68. return response.Content;
  69. }
  70. }
  71. }