Przeglądaj źródła

matchMove integration

zinab_elgendy 3 miesięcy temu
rodzic
commit
4fbd4bf4db

+ 40 - 0
MTWorkHR.API/Controllers/PaymentController.cs

@@ -0,0 +1,40 @@
+using Microsoft.AspNetCore.Authorization;
+
+using Microsoft.AspNetCore.Mvc;
+using MTWorkHR.Application.Filters;
+using MTWorkHR.Application.Services.Interfaces;
+
+namespace MTWorkHR.API.Controllers
+{
+    [Route("api/[controller]")]
+    [ApiController]
+    [AppAuthorize]
+    public class PaymentController : ControllerBase
+    {
+        private readonly IMatchMoveService _PaymentService;
+        public PaymentController(IMatchMoveService PaymentService)
+        {
+            this._PaymentService = PaymentService;
+        }
+      
+        [HttpPost]
+        public async Task<IActionResult> ProcessPayment([FromBody] object paymentRequest)
+        {
+            try
+            {
+                // Step 1: Get OAuth token
+                var token = await _PaymentService.GetAccessTokenAsync();
+
+                // Step 2: Process the payment
+                var response = await _PaymentService.ProcessPaymentAsync(token, paymentRequest);
+
+                return Ok(response); // Return the MatchMove API response
+            }
+            catch (Exception ex)
+            {
+                return BadRequest(new { error = ex.Message });
+            }
+        }
+
+    }
+}

+ 3 - 2
MTWorkHR.API/Program.cs

@@ -24,6 +24,7 @@ using DevExpress.AspNetCore.Reporting;
 using MTWorkHR.Infrastructure.Reports;
 using MTWorkHR.API.Chat;
 using Microsoft.AspNetCore.SignalR;
+using MTWorkHR.Application.Services.Payment;
 
 var builder = WebApplication.CreateBuilder(args);
 
@@ -38,7 +39,7 @@ var config = new AppSettingsConfiguration();
 // Add services to the container.
 builder.Services.AddDbContext<HRDataContext>(options =>
 {
-    options.UseSqlServer(config.ConnectionStrings.MTWorkHRConnectionString);
+    options.UseSqlServer(config.ConnectionStrings.LocalConnectionString);
     //  options.UseSqlServer(builder.Configuration.GetSection("ConnectionStrings:MTWorkHRConnectionString").Value);
 });
 
@@ -90,7 +91,7 @@ builder.Services.Configure<ApiBehaviorOptions>(options =>
 });
 
 builder.Services.AddDevExpressControls(); // Add DevExpress Reporting controls
-
+builder.Services.AddSingleton<MatchMoveService>();
 //Reporting
 
 

+ 6 - 0
MTWorkHR.API/appsettings.Development.json

@@ -47,5 +47,11 @@
     "MessageBody": "Dear User, Your OTP is {0}. Use this passcode to proceed.",
     "MessageSubject": "OTP"
   },
+  "MatchMoveSettings": {
+    "BaseUrl": "https://customer-orchestrator.uat.matchmove-beta.com/{product}/v2",
+    "ClientId": "your-client-id",
+    "ClientSecret": "your-client-secret",
+    "GrantType": "client_credentials"
+  },
   "AllowedHosts": "*"
 }

+ 8 - 0
MTWorkHR.API/appsettings.json

@@ -46,5 +46,13 @@
     "MessageBody": "Dear User, Your OTP is {0}. Use this passcode to proceed.",
     "MessageSubject": "OTP"
   },
+
+  "MatchMoveSettings": {
+    "BaseUrl": "https://api.matchmove.com",
+    "ClientId": "your-client-id",
+    "ClientSecret": "your-client-secret",
+    "GrantType": "client_credentials"
+  },
+
   "AllowedHosts": "*"
 }

+ 2 - 0
MTWorkHR.Application/ApplicationServiceRegistration.cs

@@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection;
 using MTWorkHR.Application.Identity;
 using MTWorkHR.Application.Services;
 using MTWorkHR.Application.Services.Interfaces;
+using MTWorkHR.Application.Services.Payment;
 using MTWorkHR.Core.Entities;
 using MTWorkHR.Core.Global;
 using MTWorkHR.Identity.Services;
@@ -38,6 +39,7 @@ namespace MTWorkHR.Application
             services.AddScoped<IContractService, ContractService>();
             services.AddScoped<IOTPService, OTPService>();
             services.AddScoped<ILogService<UserLog>, LogService<UserLog>>();
+            services.AddScoped<IMatchMoveService, MatchMoveService>();
 
             return services;
         }

+ 2 - 0
MTWorkHR.Application/MTWorkHR.Application.csproj

@@ -15,6 +15,8 @@
     <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.1" />
     <PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.1" />
     <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
+    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
+    <PackageReference Include="RestSharp" Version="112.1.0" />
   </ItemGroup>
 
   <ItemGroup>

+ 14 - 0
MTWorkHR.Application/Services/Interfaces/Payment/IMatchMoveService.cs

@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MTWorkHR.Application.Services.Interfaces
+{
+    public interface IMatchMoveService
+    {
+        Task<string> GetAccessTokenAsync();
+        Task<string> ProcessPaymentAsync(string token, object paymentRequest);
+    }
+}

+ 85 - 0
MTWorkHR.Application/Services/Payment/MatchMoveService.cs

@@ -0,0 +1,85 @@
+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;
+        }
+    }
+}
+

+ 10 - 27
MTWorkHR.Core/Global/AppSettingsConfiguration.cs

@@ -16,8 +16,8 @@ namespace MTWorkHR.Core.Global
         public OTPSettings OTPSettings { get; set; }
         public CaptchaSettings CaptchaSettings { get; set; }
         public LoginSettings LoginSettings { get; set; }
+        public MatchMoveSettings MatchMoveSettings { get; set; }
 
-        public IntegrationHttp IntegrationHttp { get; set; }
     }
     public class ConnectionStrings
     {
@@ -73,37 +73,20 @@ namespace MTWorkHR.Core.Global
         public int ExpirePeriodInSeconds { get; set; }
 
     }
-    public class LoginSettings
+    public class MatchMoveSettings
     {
-        public int MaxFailedAccessAttempts { get; set; }
-        public int DefaultLockoutTimeSpanInDays { get; set; }
+        public string BaseUrl { get; set; }
+        public string ClientId { get; set; }
+        public string ClientSecret { get; set; }
+        public string GrantType { get; set; }
 
     }
-
-    public class IntegrationHttp
+    public class LoginSettings
     {
-        public Financial Financial { get; set; }
-    }
+        public int MaxFailedAccessAttempts { get; set; }
+        public int DefaultLockoutTimeSpanInDays { get; set; }
 
-    public class Financial
-    {
-        public string BaseUrl { get; set; }
-        public bool IsIntegrated { get; set; }
-        //public JournalVoucher JournalVoucher { get; set; }
-        public Account Account { get; set; }
     }
 
-    //public class JournalVoucher
-    //{
-    //    public string CreateAutoVoucher { get; set; }
-    //    public string DeleteVoucher { get; set; }
-    //    public string CreateExgratiaVoucher { get; set; }
-    //    public string CreateLoungeVoucher { get; set; }
-    //}
-
-    public class Account
-    {
-        public string CreateAccount { get; set; }
-        public string DeleteAccount { get; set; }
-    }
+    
 }

+ 1 - 1
MTWorkHR.Infrastructure/InfrastructureServiceRegistration.cs

@@ -32,7 +32,7 @@ namespace MTWorkHR.Infrastructure
             
             services.AddDbContext<HRDataContext>(options =>
                 options.UseSqlServer(
-                    config.ConnectionStrings.MTWorkHRConnectionString  //configuration.GetSection("ConnectionString:MTWorkHRConnectionString").Value
+                    config.ConnectionStrings.LocalConnectionString  //configuration.GetSection("ConnectionString:MTWorkHRConnectionString").Value
                     ));
            
             services.AddIdentity<ApplicationUser, ApplicationRole>().AddEntityFrameworkStores<HRDataContext>().AddDefaultTokenProviders();