1967 lines
86 KiB
C#
1967 lines
86 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Domain.Common;
|
|
using gehGassi.Domain.Dogs;
|
|
using gehGassi.Domain.Payment;
|
|
using MangoPay.SDK;
|
|
using MangoPay.SDK.Core;
|
|
using MangoPay.SDK.Core.Enumerations;
|
|
using MangoPay.SDK.Entities;
|
|
using MangoPay.SDK.Entities.POST;
|
|
using MangoPay.SDK.Entities.PUT;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.Graph.Models;
|
|
using Address = MangoPay.SDK.Entities.Address;
|
|
using CardType = gehGassi.Domain.Common.CardType;
|
|
|
|
namespace gehGassi.External.Services
|
|
{
|
|
/// <summary>
|
|
/// Service der die Kommunikation mit MangoPay ermöglicht
|
|
/// </summary>
|
|
public class MangoPayService : IMangoPayService
|
|
{
|
|
private readonly ILogger<MangoPayService> _logger;
|
|
private readonly IWalletService _walletService;
|
|
private readonly IAppUserService _appUserService;
|
|
private readonly IUserService _userService;
|
|
private readonly IPaymentTransactionService _paymentTransactionService;
|
|
private readonly ICustomerService _customerService;
|
|
private readonly IWalkService _walkService;
|
|
|
|
private readonly MangoPayApi _api;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="mangoPayOptions">Instanz von MangoPayOptions</param>
|
|
/// <param name="logger">Instanz eines ILogger</param>
|
|
/// <param name="walletService">Instanz eines IWalletService</param>
|
|
/// <param name="appUserService">Instanz eines IAppUserService</param>
|
|
/// <param name="userService">Instanz eines IUserService</param>
|
|
/// <param name="paymentTransactionService">Instanz eines IPaymentTransactionService</param>
|
|
/// <param name="customerService">Instanz eines ICustomerService</param>
|
|
/// <param name="walkService">Instanz eines IWalkService</param>
|
|
public MangoPayService(IOptions<MangoPayOptions> mangoPayOptions, ILogger<MangoPayService> logger, IWalletService walletService, IAppUserService appUserService, IUserService userService,
|
|
IPaymentTransactionService paymentTransactionService, ICustomerService customerService, IWalkService walkService)
|
|
{
|
|
_logger = logger;
|
|
_walletService = walletService;
|
|
_appUserService = appUserService;
|
|
_userService = userService;
|
|
_paymentTransactionService = paymentTransactionService;
|
|
_customerService = customerService;
|
|
_walkService = walkService;
|
|
|
|
_api = new MangoPayApi
|
|
{
|
|
Config =
|
|
{
|
|
ClientId = mangoPayOptions.Value.ClientId,
|
|
ClientPassword = mangoPayOptions.Value.ApiKey,
|
|
BaseUrl = mangoPayOptions.Value.BaseUrl
|
|
}
|
|
};
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// ruft eine Test-Funktion auf
|
|
/// </summary>
|
|
/// <returns>Task</returns>
|
|
public async Task TestAsync()
|
|
{
|
|
//florian@fm2.at UserId = "160864861", WalletId = "160911719"
|
|
|
|
//var createResult = await _api.Users.CreateOwnerAsync(new UserNaturalOwnerPostDTO()
|
|
//{
|
|
// FirstName = "Flo",
|
|
// LastName = "Joe",
|
|
// Email = "florian@fm2.at",
|
|
// Address = new Address()
|
|
// {
|
|
// AddressLine1 = "Wolfsbauchweg 2",
|
|
// AddressLine2 = string.Empty,
|
|
// City = "Hall in Tirol",
|
|
// Country = CountryIso.AT,
|
|
// PostalCode = "6060",
|
|
// Region = "Tirol"
|
|
// },
|
|
// Birthday = new DateTime(1973, 12, 21, 0, 0, 0, DateTimeKind.Utc),
|
|
// Nationality = CountryIso.AT,
|
|
// CountryOfResidence = CountryIso.AT,
|
|
// TermsAndConditionsAccepted = true,
|
|
// UserCategory = UserCategory.PAYER
|
|
//}, Guid.NewGuid().ToString());
|
|
|
|
//var id = createResult.Id;
|
|
|
|
//var updateResult = await _api.Users.UpdateNaturalAsync(new UserNaturalPutDTO()
|
|
//{
|
|
// Email = "florian@fm2.at",
|
|
// UserCategory = UserCategory.OWNER,
|
|
// Birthday = new DateTime(1973, 12, 21, 0, 0, 0, DateTimeKind.Utc),
|
|
// CountryOfResidence = CountryIso.AT,
|
|
// Nationality = CountryIso.AT
|
|
//}, "160864861");
|
|
|
|
//var users = await _api.Users.GetAllAsync();
|
|
|
|
//var payoutResult = await _api.PayOuts.CreateBankWireAsync(new PayOutBankWirePostDTO("xxx", "yyy", new Money() { Amount = 100, Currency = CurrencyIso.EUR }, new Money() { Amount = 0, Currency = CurrencyIso.EUR },"xxx", "", PayoutModeRequested.STANDARD));
|
|
//payoutResult.
|
|
|
|
//var x = await _api.Refunds.GetAsync("xxx");
|
|
|
|
|
|
//var x = await _api.Kyc.GetAsync("ddd");
|
|
//x.RefusedReasonType
|
|
|
|
//var user = await _api.Users.GetAsync("160864861");
|
|
|
|
//var wallteCreateResult = await _api.Wallets.CreateAsync(new WalletPostDTO(new List<string> { "160864861" }, "Flos Wallet", CurrencyIso.EUR));
|
|
//var walletId = wallteCreateResult.Id;
|
|
|
|
|
|
//var cardRegistrationResult = await _api.CardRegistrations.CreateAsync(new CardRegistrationPostDTO("160864861", CurrencyIso.EUR, (MangoPay.SDK.Core.Enumerations.CardType)CardType.CB_VISA_MASTERCARD));
|
|
|
|
//var dict = new Dictionary<string, string>();
|
|
//dict.Add("Content-Type", "application/x-www-form-urlencoded");
|
|
//dict.Add("accessKeyRef", cardRegistrationResult.AccessKey);
|
|
//dict.Add("data", cardRegistrationResult.PreregistrationData);
|
|
//dict.Add("cardNumber", "4970107111111119");
|
|
//dict.Add("cardExpirationDate", "1225");
|
|
//dict.Add("cardCvx", "111");
|
|
|
|
//// Execute post method
|
|
//var httpClient = new HttpClient();
|
|
//using (var response = await httpClient.PostAsync(cardRegistrationResult.CardRegistrationURL, new FormUrlEncodedContent(dict)))
|
|
//{
|
|
// if (response.IsSuccessStatusCode)
|
|
// {
|
|
// var responseString = await response.Content.ReadAsStringAsync();
|
|
// var updateResult = await _api.CardRegistrations.UpdateAsync(new CardRegistrationPutDTO() { RegistrationData = responseString }, cardRegistrationResult.Id);
|
|
// var card = await _api.Cards.GetAsync(updateResult.CardId);
|
|
|
|
// var muhu = card.Id;
|
|
// }
|
|
//}
|
|
|
|
//var cardEx = await _api.Cards.GetAsync("164009448");
|
|
|
|
//var browserInfo = new BrowserInfo
|
|
//{
|
|
// AcceptHeader = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
|
|
// ColorDepth = 24,
|
|
// JavaEnabled = true,
|
|
// Language = "de-AT",
|
|
// ScreenHeight = 900,
|
|
// ScreenWidth = 1440,
|
|
// TimeZoneOffset = "0",
|
|
// UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
|
|
//};
|
|
|
|
|
|
//var payInDto = new PayInCardDirectPostDTO(cardEx.UserId, cardEx.UserId, new Money() { Amount = 100, Currency = CurrencyIso.EUR }, new Money() { Amount = 1, Currency = CurrencyIso.EUR }, "160911719", "https://www.orf.at", cardEx.Id, "gehgassi", null, browserInfo);
|
|
//payInDto.IpAddress = "127.0.0.1";
|
|
//var payinDirectResult = await _api.PayIns.CreateCardDirectAsync(payInDto);
|
|
|
|
|
|
|
|
var muh = "kuh";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen eines Mangopay-Benutzers für einen App-User
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des AppUsers</param>
|
|
/// <param name="suppressCommit">Soll das Commit unterdrückt werden?</param>
|
|
/// <returns>MangoPayResult mit der ID des Mangopay-Users</returns>
|
|
public async Task<MangoPayResult<string>> CreateOwnerAsync(string appUserId, bool suppressCommit)
|
|
{
|
|
var result = new MangoPayResult<string>();
|
|
|
|
var appUser = await _appUserService.GetAsync(appUserId);
|
|
if (appUser != null)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(appUser.PaymentId))
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_User_Create_Exists;
|
|
return result;
|
|
}
|
|
if (!appUser.BirthDate.HasValue)
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_User_BirthDate;
|
|
return result;
|
|
}
|
|
if (string.IsNullOrWhiteSpace(appUser.NationalityCode))
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_User_Nationality;
|
|
return result;
|
|
}
|
|
if (string.IsNullOrWhiteSpace(appUser.MainResidenceCode))
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_User_MainResidence;
|
|
return result;
|
|
}
|
|
|
|
var user = await _userService.GetByAppUserAsync(appUser.Id);
|
|
if (user != null)
|
|
{
|
|
var request = new UserNaturalOwnerPostDTO()
|
|
{
|
|
FirstName = appUser.FirstName,
|
|
LastName = appUser.LastName,
|
|
Email = user.Email,
|
|
Birthday = new DateTime(appUser.BirthDate.Value.Year, appUser.BirthDate.Value.Month, appUser.BirthDate.Value.Day, 0, 0, 0, DateTimeKind.Utc),
|
|
Nationality = (CountryIso)Enum.Parse(typeof(CountryIso), appUser.NationalityCode),
|
|
CountryOfResidence = (CountryIso)Enum.Parse(typeof(CountryIso), appUser.MainResidenceCode),
|
|
TermsAndConditionsAccepted = appUser.PaymentTermsAccepted,
|
|
UserCategory = UserCategory.OWNER
|
|
};
|
|
if (appUser.Address != null && appUser.Address.ValidForPaymentProvider())
|
|
{
|
|
var address = new Address()
|
|
{
|
|
AddressLine1 = appUser.Address.AddressLine1,
|
|
AddressLine2 = appUser.Address.AddressLine2,
|
|
City = appUser.Address.City,
|
|
Country = (CountryIso)Enum.Parse(typeof(CountryIso), appUser.Address.CountryCode),
|
|
PostalCode = appUser.Address.Zip,
|
|
Region = appUser.Address.State
|
|
};
|
|
request.Address = address;
|
|
}
|
|
|
|
try
|
|
{
|
|
var response = await _api.Users.CreateOwnerAsync(request);
|
|
if (response != null)
|
|
{
|
|
appUser.PaymentId = response.Id;
|
|
|
|
if (!suppressCommit)
|
|
{
|
|
appUser.UpdatedAt = DateTimeOffset.UtcNow;
|
|
await _appUserService.CommitAsync("System");
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Value = response.Id;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_User_NotFound;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_AppUser_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aktualisieren eines Mangopay-Benutzers für einen App-User
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des AppUsers</param>
|
|
/// <returns>true wenn erfolgreich, false sonst</returns>
|
|
public async Task<MangoPayResult<bool>> UpdateOwnerAsync(string appUserId)
|
|
{
|
|
var result = new MangoPayResult<bool>
|
|
{
|
|
Value = false
|
|
};
|
|
|
|
var appUser = await _appUserService.GetAsync(appUserId);
|
|
if (appUser != null)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(appUser.PaymentId))
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_User_Update_NotExists;
|
|
return result;
|
|
}
|
|
if (!appUser.BirthDate.HasValue)
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_User_BirthDate;
|
|
return result;
|
|
}
|
|
if (string.IsNullOrWhiteSpace(appUser.NationalityCode))
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_User_Nationality;
|
|
return result;
|
|
}
|
|
if (string.IsNullOrWhiteSpace(appUser.MainResidenceCode))
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_User_MainResidence;
|
|
return result;
|
|
}
|
|
|
|
var user = await _userService.GetByAppUserAsync(appUser.Id);
|
|
if (user != null)
|
|
{
|
|
var request = new UserNaturalPutDTO()
|
|
{
|
|
FirstName = appUser.FirstName,
|
|
LastName = appUser.LastName,
|
|
Email = user.Email,
|
|
Birthday = new DateTime(appUser.BirthDate.Value.Year, appUser.BirthDate.Value.Month, appUser.BirthDate.Value.Day, 0, 0, 0, DateTimeKind.Utc),
|
|
Nationality = (CountryIso)Enum.Parse(typeof(CountryIso), appUser.NationalityCode),
|
|
CountryOfResidence = (CountryIso)Enum.Parse(typeof(CountryIso), appUser.MainResidenceCode),
|
|
TermsAndConditionsAccepted = appUser.PaymentTermsAccepted,
|
|
UserCategory = UserCategory.OWNER
|
|
};
|
|
|
|
if (appUser.Address != null && appUser.Address.ValidForPaymentProvider())
|
|
{
|
|
var address = new Address()
|
|
{
|
|
AddressLine1 = appUser.Address.AddressLine1,
|
|
AddressLine2 = appUser.Address.AddressLine2,
|
|
City = appUser.Address.City,
|
|
Country = (CountryIso)Enum.Parse(typeof(CountryIso), appUser.Address.CountryCode),
|
|
PostalCode = appUser.Address.Zip,
|
|
Region = appUser.Address.State
|
|
};
|
|
request.Address = address;
|
|
}
|
|
|
|
try
|
|
{
|
|
var response = await _api.Users.UpdateNaturalAsync(request, appUser.PaymentId);
|
|
if (response != null)
|
|
{
|
|
result.Success = true;
|
|
result.Value = true;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_User_NotFound;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_AppUser_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Prüfen ob ein MangoPay-Benutzer existiert
|
|
/// </summary>
|
|
/// <param name="mangoPayUserId">Id des Mangopay-Benutzers</param>
|
|
/// <returns>true wenn existent, false sonst</returns>
|
|
public async Task<MangoPayResult<bool>> OwnerExistsAsync(string mangoPayUserId)
|
|
{
|
|
var result = new MangoPayResult<bool>
|
|
{
|
|
Value = false
|
|
};
|
|
|
|
if (!string.IsNullOrWhiteSpace(mangoPayUserId))
|
|
{
|
|
try
|
|
{
|
|
var response = await _api.Users.GetNaturalAsync(mangoPayUserId);
|
|
if (response != null)
|
|
{
|
|
result.Success = true;
|
|
result.Value = true;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_User_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellt ein Wallet für einen AppUser mit einem bestimmten Typ.
|
|
/// Wenn das Wallet schon existiert, wird es zurückgegeben.
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des AppUsers</param>
|
|
/// <param name="walletType">Typ des Wallets</param>
|
|
/// <returns>Wallet</returns>
|
|
public async Task<MangoPayResult<Wallet>> CreateWalletAsync(string appUserId, WalletType walletType)
|
|
{
|
|
var result = new MangoPayResult<Wallet>();
|
|
|
|
var appUser = await _appUserService.GetAsync(appUserId);
|
|
if (appUser != null)
|
|
{
|
|
//Prüfen ob es bereits ein Wallet von diesem Typ für den AppUser gibt
|
|
var existing = await _walletService.GetAsync(appUser.Id, walletType);
|
|
if (existing == null)
|
|
{
|
|
//Zuerst versuchen das Wallet bei MangoPay anzulegen...
|
|
//Wallet noch nicht vorhanden, also anlegen
|
|
var request = new WalletPostDTO(new List<string>() { appUser.PaymentId }, walletType.ToString(), CurrencyIso.EUR);
|
|
try
|
|
{
|
|
var response = await _api.Wallets.CreateAsync(request);
|
|
if (response != null)
|
|
{
|
|
var wallet = _walletService.Create(appUser.Id, response.Id, walletType, appUser.PaymentId, "EUR", walletType.ToString());
|
|
wallet.Balance = response.Balance.Amount;
|
|
wallet.UpdatedAt = DateTimeOffset.UtcNow;
|
|
_walletService.Add(wallet);
|
|
await _walletService.CommitAsync("System");
|
|
|
|
result.Success = true;
|
|
result.Value = wallet;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.Success = true;
|
|
result.Value = existing;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_AppUser_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aktualisiert den Kontostand eines Wallets und gibt diesen zurück
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des AppUsers</param>
|
|
/// <param name="walletType">Typ des Wallets</param>
|
|
/// <returns>Wallet mit neuem Kontostand</returns>
|
|
public async Task<MangoPayResult<Wallet>> GetWalletBalanceAsync(string appUserId, WalletType walletType)
|
|
{
|
|
var result = new MangoPayResult<Wallet>()
|
|
{
|
|
Value = null
|
|
};
|
|
|
|
var appUser = await _appUserService.GetAsync(appUserId);
|
|
if (appUser != null)
|
|
{
|
|
//Prüfen ob es bereits ein Wallet von diesem Typ für den AppUser gibt
|
|
var wallet = await _walletService.GetAsync(appUser.Id, walletType);
|
|
if (wallet != null)
|
|
{
|
|
try
|
|
{
|
|
var response = await _api.Wallets.GetAsync(wallet.WalletId);
|
|
if (response != null)
|
|
{
|
|
wallet.Balance = response.Balance.Amount;
|
|
wallet.UpdatedAt = DateTimeOffset.UtcNow;
|
|
await _walletService.CommitAsync("System");
|
|
|
|
result.Success = true;
|
|
result.Value = wallet;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_Wallet_NotExists;
|
|
result.Value = null;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_AppUser_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aktualisiert den Kontostand eines Wallets und gibt diesen zurück
|
|
/// </summary>
|
|
/// <param name="walletId">Id des Wallets bei MangoPay</param>
|
|
/// <returns>Kontostand</returns>
|
|
public async Task<MangoPayResult<long>> GetWalletBalanceAsync(string walletId)
|
|
{
|
|
var result = new MangoPayResult<long>();
|
|
|
|
try
|
|
{
|
|
var response = await _api.Wallets.GetAsync(walletId);
|
|
if (response != null)
|
|
{
|
|
result.Success = true;
|
|
result.Value = response.Balance.Amount;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Prüfen ob ein MangoPay-Wallet existiert
|
|
/// </summary>
|
|
/// <param name="walletId">Id des Mangopay-Wallets</param>
|
|
/// <returns>true wenn existent, false sonst</returns>
|
|
public async Task<MangoPayResult<bool>> WalletExistsAsync(string walletId)
|
|
{
|
|
var result = new MangoPayResult<bool>
|
|
{
|
|
Value = false
|
|
};
|
|
|
|
if (!string.IsNullOrWhiteSpace(walletId))
|
|
{
|
|
try
|
|
{
|
|
var response = await _api.Wallets.GetAsync(walletId);
|
|
if (response != null)
|
|
{
|
|
result.Success = true;
|
|
result.Value = true;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_Wallet_NotExists;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellt ein Bankkonto für einen AppUser
|
|
/// </summary>
|
|
/// <param name="mangoPayUserId">Id des Benutzers bei MangoPay</param>
|
|
/// <param name="ownerName">Name des Inhabers des Bankkontos</param>
|
|
/// <param name="address">Adresse des Inhabers des Bankkontos</param>
|
|
/// <param name="iban">Iban des Bankkontos</param>
|
|
/// <param name="bic">BIC des Bankkontos</param>
|
|
/// <returns>MangoPayResult mit der ID des Bankkontos</returns>
|
|
public async Task<MangoPayResult<string>> CreateBankAccountAsync(string mangoPayUserId, string ownerName, Domain.Common.Address address, string iban, string bic)
|
|
{
|
|
var result = new MangoPayResult<string>
|
|
{
|
|
Value = string.Empty
|
|
};
|
|
|
|
if (!string.IsNullOrWhiteSpace(mangoPayUserId))
|
|
{
|
|
try
|
|
{
|
|
var addressDto = new Address()
|
|
{
|
|
AddressLine1 = address.AddressLine1,
|
|
AddressLine2 = address.AddressLine2,
|
|
City = address.City,
|
|
PostalCode = address.Zip,
|
|
Country = (CountryIso)Enum.Parse(typeof(CountryIso), address.CountryCode),
|
|
Region = address.State
|
|
};
|
|
|
|
var request = new BankAccountIbanPostDTO(ownerName, addressDto, iban)
|
|
{
|
|
BIC = bic,
|
|
Type = BankAccountType.IBAN,
|
|
UserId = mangoPayUserId
|
|
};
|
|
|
|
var response = await _api.Users.CreateBankAccountIbanAsync(mangoPayUserId, request);
|
|
if (response != null)
|
|
{
|
|
result.Success = true;
|
|
result.Value = response.Id;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_User_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt ein Bankkonto für einen App-User zurück
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des App-Users</param>
|
|
/// <param name="mangoPayUserId">Id des Benutzers bei MangoPay</param>
|
|
/// <param name="bankAccountId">Id des Bankkontos bei MangoPay</param>
|
|
/// <returns>MangoPayResult mit Bankkonto oder null, wenn keines gefunden</returns>
|
|
public async Task<MangoPayResult<BankAccount>> GetBankAccountAsync(string appUserId, string mangoPayUserId, string bankAccountId)
|
|
{
|
|
var result = new MangoPayResult<BankAccount>
|
|
{
|
|
Value = null
|
|
};
|
|
|
|
try
|
|
{
|
|
var response = await _api.Users.GetBankAccountIbanAsync(mangoPayUserId, bankAccountId);
|
|
if (response != null)
|
|
{
|
|
result.Success = true;
|
|
|
|
var bankAccount = new BankAccount
|
|
{
|
|
AppUserId = appUserId,
|
|
PaymentId = mangoPayUserId,
|
|
BankId = response.Id,
|
|
OwnerName = response.OwnerName,
|
|
Iban = response.IBAN,
|
|
Bic = response.BIC,
|
|
Address = new Domain.Common.Address()
|
|
{
|
|
AddressLine1 = response.OwnerAddress.AddressLine1,
|
|
AddressLine2 = response.OwnerAddress.AddressLine2,
|
|
City = response.OwnerAddress.City,
|
|
Zip = response.OwnerAddress.PostalCode,
|
|
CountryCode = response.OwnerAddress.Country.ToString(),
|
|
State = response.OwnerAddress.Region
|
|
}
|
|
};
|
|
|
|
result.Value = bankAccount;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deaktiveren eines Bankkontos für einen App-User
|
|
/// </summary>
|
|
/// <param name="mangoPayUserId">Id des Benutzers bei MangoPay</param>
|
|
/// <param name="bankAccountId">Id des Bankkontos bei MangoPay</param>
|
|
/// <returns>true wenn erfolgreich, false sonst</returns>
|
|
public async Task<MangoPayResult<bool>> DeactivateBankAccountAsync(string mangoPayUserId, string bankAccountId)
|
|
{
|
|
var result = new MangoPayResult<bool>
|
|
{
|
|
Value = false
|
|
};
|
|
|
|
try
|
|
{
|
|
var putDto = new DisactivateBankAccountPutDTO()
|
|
{
|
|
Active = false
|
|
};
|
|
var response = await _api.Users.UpdateBankAccountAsync(mangoPayUserId, putDto, bankAccountId);
|
|
if (response != null)
|
|
{
|
|
result.Success = true;
|
|
result.Value = response.Active == false;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Transferieren von Geld vom Guthaben-Konto zum Transaktions-Konto
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des App-Users</param>
|
|
/// <param name="ammount">Betrag der transferiert werden soll</param>
|
|
/// <param name="tag">Tag der zum Transfer abgelegt werden soll</param>
|
|
/// <returns>true wenn erfolgreich, false sonst</returns>
|
|
public async Task<MangoPayResult<bool>> TransferMoneyFromCreditToFeeAccountAsync(string appUserId, long ammount, string tag)
|
|
{
|
|
var result = new MangoPayResult<bool>();
|
|
|
|
var appUser = await _appUserService.GetAsync(appUserId);
|
|
if (appUser != null)
|
|
{
|
|
var creditsWalletResult = await GetWalletBalanceAsync(appUserId, WalletType.Credits);
|
|
var feesWalletResult = await GetWalletBalanceAsync(appUserId, WalletType.Fees);
|
|
|
|
if (creditsWalletResult.Success && feesWalletResult.Success)
|
|
{
|
|
var creditsWallet = creditsWalletResult.Value;
|
|
var feesWallet = feesWalletResult.Value;
|
|
|
|
if (creditsWallet.Balance >= ammount)
|
|
{
|
|
//Die Summe ist gedeckt. Transferieren
|
|
try
|
|
{
|
|
var transferDto = new TransferPostDTO(appUser.PaymentId, appUser.PaymentId,
|
|
new Money() { Amount = ammount, Currency = CurrencyIso.EUR },
|
|
new Money() { Amount = 0, Currency = CurrencyIso.EUR }, creditsWallet.WalletId, feesWallet.WalletId);
|
|
transferDto.Tag = tag;
|
|
|
|
var transferResponse = await _api.Transfers.CreateAsync(transferDto);
|
|
if (transferResponse != null)
|
|
{
|
|
result.Success = true;
|
|
result.Value = true;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_Transfer_NotEnoughMoney;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorMessage = creditsWalletResult.ErrorMessage;
|
|
result.ErrorType = creditsWalletResult.ErrorType;
|
|
result.ErrorMessages = creditsWalletResult.ErrorMessages;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_AppUser_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Transferieren von Geld vom Transaktions-Konto zum Guthaben-Konto
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des App-Users</param>
|
|
/// <param name="ammount">Betrag der transferiert werden soll</param>
|
|
/// <param name="tag">Tag der zum Transfer abgelegt werden soll</param>
|
|
/// <returns>true wenn erfolgreich, false sonst</returns>
|
|
public async Task<MangoPayResult<bool>> TransferMoneyFromFeeToCreditAccountAsync(string appUserId, long ammount, string tag)
|
|
{
|
|
var result = new MangoPayResult<bool>();
|
|
|
|
var appUser = await _appUserService.GetAsync(appUserId);
|
|
if (appUser != null)
|
|
{
|
|
var creditsWalletResult = await GetWalletBalanceAsync(appUserId, WalletType.Credits);
|
|
var feesWalletResult = await GetWalletBalanceAsync(appUserId, WalletType.Fees);
|
|
|
|
if (creditsWalletResult.Success && feesWalletResult.Success)
|
|
{
|
|
var creditsWallet = creditsWalletResult.Value;
|
|
var feesWallet = feesWalletResult.Value;
|
|
|
|
if (feesWallet.Balance >= ammount)
|
|
{
|
|
//Die Summe ist gedeckt. Transferieren
|
|
try
|
|
{
|
|
var transferDto = new TransferPostDTO(appUser.PaymentId, appUser.PaymentId,
|
|
new Money() { Amount = ammount, Currency = CurrencyIso.EUR },
|
|
new Money() { Amount = 0, Currency = CurrencyIso.EUR }, feesWallet.WalletId, creditsWallet.WalletId);
|
|
transferDto.Tag = tag;
|
|
|
|
var transferResponse = await _api.Transfers.CreateAsync(transferDto);
|
|
if (transferResponse != null)
|
|
{
|
|
result.Success = true;
|
|
result.Value = true;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_Transfer_NotEnoughMoney;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorMessage = creditsWalletResult.ErrorMessage;
|
|
result.ErrorType = creditsWalletResult.ErrorType;
|
|
result.ErrorMessages = creditsWalletResult.ErrorMessages;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_AppUser_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Transferieren von Geld von einem Wallet zu einem anderen
|
|
/// </summary>
|
|
/// <param name="sourceAppUserId">Quell-App-UserId</param>
|
|
/// <param name="targetAppUserId">Ziel-App-UserId</param>
|
|
/// <param name="walkId">Id des Walks</param>
|
|
/// <param name="ammount">Betrag der transferiert werden soll</param>
|
|
/// <param name="fees">Gebühren an gehGassi</param>
|
|
/// <param name="tag">Tag der zum Transfer abgelegt werden soll</param>
|
|
/// <returns>true wenn erfolgreich, false sonst</returns>
|
|
public async Task<MangoPayResult<bool>> TransferMoneyAsync(string sourceAppUserId, string targetAppUserId, string walkId, long ammount, long fees, string tag)
|
|
{
|
|
var result = new MangoPayResult<bool>();
|
|
|
|
var sourceAppUser = await _appUserService.GetAsync(sourceAppUserId);
|
|
var targetAppUser = await _appUserService.GetAsync(targetAppUserId);
|
|
|
|
if (sourceAppUser != null && targetAppUser != null)
|
|
{
|
|
var sourceWallet = await _walletService.GetAsync(sourceAppUserId, WalletType.Fees);
|
|
var targetWallet = await _walletService.GetAsync(targetAppUserId, WalletType.Credits);
|
|
|
|
if (sourceWallet != null && targetWallet != null)
|
|
{
|
|
try
|
|
{
|
|
var transferDto = new TransferPostDTO(sourceAppUser.PaymentId, targetAppUser.PaymentId,
|
|
new Money() { Amount = ammount, Currency = CurrencyIso.EUR },
|
|
new Money() { Amount = fees, Currency = CurrencyIso.EUR }, sourceWallet.WalletId, targetWallet.WalletId);
|
|
transferDto.Tag = tag;
|
|
|
|
var transferResponse = await _api.Transfers.CreateAsync(transferDto);
|
|
if (transferResponse != null)
|
|
{
|
|
result.Success = true;
|
|
result.Value = true;
|
|
|
|
var paymentTransaction = _paymentTransactionService.Create(transferResponse.Id, PaymentTransactionType.Pay, PaymentTransactionStatus.SUCCEEDED, walkId, targetAppUser.Id, targetWallet.Id, sourceAppUser.Id, sourceWallet.Id, ammount - fees, 0, CurrencyIso.EUR.ToString(), null);
|
|
_paymentTransactionService.Add(paymentTransaction);
|
|
await _paymentTransactionService.CommitAsync("System");
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_Wallet_NotExists;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_AppUser_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Transferiert Geld von einem Wallet zu einem anderen
|
|
/// </summary>
|
|
/// <param name="sourceMangopayUserId">Id des Mangopayusers der zahlt</param>
|
|
/// <param name="sourceWalletId">Quellwallet Id</param>
|
|
/// <param name="targetMangopayUserId">Id des Mangopayusers der die Zahlung erhält</param>
|
|
/// <param name="targetWalletId">Zielwallet Id</param>
|
|
/// <param name="walkId">Id des Walks - oder leer</param>
|
|
/// <param name="ammount">Betrag</param>
|
|
/// <param name="fees">Optinal: Gebühren an gehGassi</param>
|
|
/// <param name="tag">Tag der zum Transfer abgelegt werden soll</param>
|
|
/// <param name="sourceAppUserId">Optional: Quell-App-UserId</param>
|
|
/// <param name="targetAppUserId">Optional: Ziel-App-UserId</param>
|
|
/// <returns>true wenn erfolgreich, false sonst</returns>
|
|
public async Task<MangoPayResult<bool>> TransferMoneyAsync(string sourceMangopayUserId, string sourceWalletId, string targetMangopayUserId, string targetWalletId, string walkId, long ammount, long fees, string tag, string sourceAppUserId = "", string targetAppUserId = "")
|
|
{
|
|
var result = new MangoPayResult<bool>();
|
|
|
|
var sourceWallet = await _walletService.GetAsync(sourceWalletId);
|
|
var targetWallet = await _walletService.GetAsync(targetWalletId);
|
|
|
|
if (sourceWallet != null && targetWallet != null)
|
|
{
|
|
try
|
|
{
|
|
var transferDto = new TransferPostDTO(sourceMangopayUserId, targetMangopayUserId,
|
|
new Money() { Amount = ammount, Currency = CurrencyIso.EUR },
|
|
new Money() { Amount = fees, Currency = CurrencyIso.EUR }, sourceWallet.WalletId, targetWallet.WalletId);
|
|
transferDto.Tag = tag;
|
|
|
|
var transferResponse = await _api.Transfers.CreateAsync(transferDto);
|
|
if (transferResponse != null)
|
|
{
|
|
result.Success = true;
|
|
result.Value = true;
|
|
|
|
var paymentTransaction = _paymentTransactionService.Create(transferResponse.Id, PaymentTransactionType.Pay, PaymentTransactionStatus.SUCCEEDED,
|
|
walkId, targetAppUserId, targetWallet.Id, sourceAppUserId, sourceWallet.Id, ammount - fees, 0, CurrencyIso.EUR.ToString(), null);
|
|
_paymentTransactionService.Add(paymentTransaction);
|
|
await _paymentTransactionService.CommitAsync("System");
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen einer Kreditkarten-Einzahlung
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des App-Users</param>
|
|
/// <param name="walkId">Id des Walks</param>
|
|
/// <param name="ammount">Betrag der eingezahlt werden soll</param>
|
|
/// <param name="fees">Gebühren die an gehgassi abgeführt werden</param>
|
|
/// <param name="fromCredit">Betrag der zusätzlich vom Guthabenkonto genommen werden muss</param>
|
|
/// <param name="tag">Tag der zur Einzahlung abgelegt werden soll</param>
|
|
/// <param name="returnUrl">Return-Url für MangoPay</param>
|
|
/// <returns>MangopayResult mit einem string der eine ReturnUrl beinhaltet die die App öffnen muss</returns>
|
|
public async Task<MangoPayResult<string>> CreatePayInCardAsync(string appUserId, string walkId, long ammount, long fees, long fromCredit, string tag, string returnUrl)
|
|
{
|
|
var result = new MangoPayResult<string>();
|
|
|
|
var appUser = await _appUserService.GetAsync(appUserId);
|
|
if (appUser != null)
|
|
{
|
|
var wallet = await _walletService.GetAsync(appUserId, WalletType.Fees);
|
|
if (wallet != null)
|
|
{
|
|
try
|
|
{
|
|
var payInDto = new PayInCardWebPostDTO(appUser.PaymentId,
|
|
new Money() { Amount = ammount, Currency = CurrencyIso.EUR },
|
|
new Money() { Amount = fees, Currency = CurrencyIso.EUR },
|
|
wallet.WalletId, returnUrl, CultureCode.DE, (MangoPay.SDK.Core.Enumerations.CardType)CardType.CB_VISA_MASTERCARD, "gehgassi")
|
|
{
|
|
Tag = tag,
|
|
SecureMode = SecureMode.FORCE
|
|
};
|
|
|
|
var payInResult = await _api.PayIns.CreateCardWebAsync(payInDto);
|
|
|
|
if (payInResult != null)
|
|
{
|
|
if (payInResult.Status == TransactionStatus.CREATED)
|
|
{
|
|
result.Success = true;
|
|
result.Value = payInResult.RedirectURL;
|
|
|
|
var paymentTransaction = _paymentTransactionService.Create(payInResult.Id, PaymentTransactionType.PayIn, PaymentTransactionStatus.CREATED, walkId, appUserId, wallet.Id, string.Empty, string.Empty, ammount - fees, fromCredit, CurrencyIso.EUR.ToString(), PayInType.CB_VISA_MASTERCARD);
|
|
_paymentTransactionService.Add(paymentTransaction);
|
|
await _paymentTransactionService.CommitAsync("System");
|
|
}
|
|
else
|
|
{
|
|
result.Success = false;
|
|
result.ErrorType = MangoPayErrorCodes.Err_PayIn_Failed;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_PayIn_Failed;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_Wallet_NotExists;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_AppUser_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen einer Maestro-Einzahlung
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des App-Users</param>
|
|
/// <param name="walkId">Id des Walks</param>
|
|
/// <param name="ammount">Betrag der eingezahlt werden soll</param>
|
|
/// <param name="fees">Gebühren die an gehgassi abgeführt werden</param>
|
|
/// <param name="fromCredit">Betrag der zusätzlich vom Guthabenkonto genommen werden muss</param>
|
|
/// <param name="tag">Tag der zur Einzahlung abgelegt werden soll</param>
|
|
/// <param name="returnUrl">Return-Url für MangoPay</param>
|
|
/// <returns>MangopayResult mit einem string der eine ReturnUrl beinhaltet die die App öffnen muss</returns>
|
|
public async Task<MangoPayResult<string>> CreatePayInMaestroAsync(string appUserId, string walkId, long ammount, long fees, long fromCredit, string tag, string returnUrl)
|
|
{
|
|
var result = new MangoPayResult<string>();
|
|
|
|
var appUser = await _appUserService.GetAsync(appUserId);
|
|
if (appUser != null)
|
|
{
|
|
var wallet = await _walletService.GetAsync(appUserId, WalletType.Fees);
|
|
if (wallet != null)
|
|
{
|
|
try
|
|
{
|
|
var payInDto = new PayInCardWebPostDTO(appUser.PaymentId,
|
|
new Money() { Amount = ammount, Currency = CurrencyIso.EUR },
|
|
new Money() { Amount = fees, Currency = CurrencyIso.EUR },
|
|
wallet.WalletId, returnUrl, CultureCode.DE, (MangoPay.SDK.Core.Enumerations.CardType)CardType.MAESTRO, "gehgassi")
|
|
{
|
|
Tag = tag,
|
|
SecureMode = SecureMode.FORCE
|
|
};
|
|
|
|
var payInResult = await _api.PayIns.CreateCardWebAsync(payInDto);
|
|
|
|
if (payInResult != null)
|
|
{
|
|
if (payInResult.Status == TransactionStatus.CREATED)
|
|
{
|
|
result.Success = true;
|
|
result.Value = payInResult.RedirectURL;
|
|
|
|
var paymentTransaction = _paymentTransactionService.Create(payInResult.Id, PaymentTransactionType.PayIn, PaymentTransactionStatus.CREATED, walkId, appUserId, wallet.Id, string.Empty, string.Empty, ammount - fees, fromCredit, CurrencyIso.EUR.ToString(), PayInType.MAESTRO);
|
|
_paymentTransactionService.Add(paymentTransaction);
|
|
await _paymentTransactionService.CommitAsync("System");
|
|
}
|
|
else
|
|
{
|
|
result.Success = false;
|
|
result.ErrorType = MangoPayErrorCodes.Err_PayIn_Failed;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_PayIn_Failed;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_Wallet_NotExists;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_AppUser_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen einer Klarna-Einzahlung
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des App-Users</param>
|
|
/// <param name="walkId">Id des Walks</param>
|
|
/// <param name="ammount">Betrag der eingezahlt werden soll</param>
|
|
/// <param name="fees">Gebühren die an gehgassi abgeführt werden</param>
|
|
/// <param name="fromCredit">Betrag der zusätzlich vom Guthabenkonto genommen werden muss</param>
|
|
/// <param name="tag">Tag der zur Einzahlung abgelegt werden soll</param>
|
|
/// <param name="returnUrl">Return-Url für MangoPay</param>
|
|
/// <returns>MangopayResult mit einem string der eine ReturnUrl beinhaltet die die App öffnen muss</returns>
|
|
public async Task<MangoPayResult<string>> CreatePayInKlarnaAsync(string appUserId, string walkId, long ammount, long fees, long fromCredit, string tag, string returnUrl)
|
|
{
|
|
var result = new MangoPayResult<string>();
|
|
|
|
var appUser = await _appUserService.GetAsync(appUserId);
|
|
if (appUser != null)
|
|
{
|
|
var wallet = await _walletService.GetAsync(appUserId, WalletType.Fees);
|
|
if (wallet != null)
|
|
{
|
|
try
|
|
{
|
|
var payInDto = new PayInDirectDebitPostDTO(appUser.PaymentId,
|
|
new Money() { Amount = ammount, Currency = CurrencyIso.EUR },
|
|
new Money() { Amount = fees, Currency = CurrencyIso.EUR },
|
|
wallet.WalletId, returnUrl, CultureCode.DE, DirectDebitType.SOFORT)
|
|
{
|
|
Tag = tag
|
|
};
|
|
|
|
var payInResult = await _api.PayIns.CreateDirectDebitAsync(payInDto);
|
|
|
|
if (payInResult != null)
|
|
{
|
|
if (payInResult.Status == TransactionStatus.CREATED)
|
|
{
|
|
result.Success = true;
|
|
result.Value = payInResult.RedirectURL;
|
|
|
|
var paymentTransaction = _paymentTransactionService.Create(payInResult.Id, PaymentTransactionType.PayIn, PaymentTransactionStatus.CREATED, walkId, appUserId, wallet.Id, string.Empty, string.Empty, ammount - fees, fromCredit, CurrencyIso.EUR.ToString(), PayInType.KLARNA);
|
|
_paymentTransactionService.Add(paymentTransaction);
|
|
await _paymentTransactionService.CommitAsync("System");
|
|
}
|
|
else
|
|
{
|
|
result.Success = false;
|
|
result.ErrorType = MangoPayErrorCodes.Err_PayIn_Failed;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_PayIn_Failed;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_Wallet_NotExists;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_AppUser_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen einer PayPal-Einzahlung
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des App-Users</param>
|
|
/// <param name="walkId">Id des Walks</param>
|
|
/// <param name="ammount">Betrag der eingezahlt werden soll</param>
|
|
/// <param name="fees">Gebühren die an gehgassi abgeführt werden</param>
|
|
/// <param name="fromCredit">Betrag der zusätzlich vom Guthabenkonto genommen werden muss</param>
|
|
/// <param name="tag">Tag der zur Einzahlung abgelegt werden soll</param>
|
|
/// <param name="returnUrl">Return-Url für MangoPay</param>
|
|
/// <returns>MangopayResult mit einem string der eine ReturnUrl beinhaltet die die App öffnen muss</returns>
|
|
public async Task<MangoPayResult<string>> CreatePayInPayPalAsync(string appUserId, string walkId, long ammount, long fees, long fromCredit, string tag, string returnUrl)
|
|
{
|
|
var result = new MangoPayResult<string>();
|
|
|
|
var appUser = await _appUserService.GetAsync(appUserId);
|
|
if (appUser != null)
|
|
{
|
|
var wallet = await _walletService.GetAsync(appUserId, WalletType.Fees);
|
|
if (wallet != null)
|
|
{
|
|
var walk = await _walkService.GetWalkWithNamesAsync(walkId);
|
|
if (walk != null)
|
|
{
|
|
try
|
|
{
|
|
var lineItem = new LineItem
|
|
{
|
|
Name = $"gehgassi {walk.ServiceType} - {walk.Start:g}",
|
|
Quantity = 1,
|
|
UnitAmount = ammount,
|
|
TaxAmount = 0,
|
|
Description = "gehgassi GmbH"
|
|
};
|
|
|
|
var payInDto = new PayInPayPalWebPostDTO(appUser.PaymentId,
|
|
new Money() { Amount = ammount, Currency = CurrencyIso.EUR },
|
|
new Money() { Amount = fees, Currency = CurrencyIso.EUR },
|
|
wallet.WalletId, returnUrl,
|
|
[lineItem],
|
|
null, "gehgassi",
|
|
CultureCode.DE, ShippingPreference.NO_SHIPPING, tag, null);
|
|
|
|
var payInResult = await _api.PayIns.CreatePayPalWebAsync(payInDto);
|
|
|
|
if (payInResult != null)
|
|
{
|
|
if (payInResult.Status == TransactionStatus.CREATED)
|
|
{
|
|
result.Success = true;
|
|
result.Value = payInResult.RedirectURL;
|
|
|
|
var paymentTransaction = _paymentTransactionService.Create(payInResult.Id, PaymentTransactionType.PayIn, PaymentTransactionStatus.CREATED, walkId, appUserId, wallet.Id, string.Empty, string.Empty, ammount - fees, fromCredit, CurrencyIso.EUR.ToString(), PayInType.PAYPAL);
|
|
_paymentTransactionService.Add(paymentTransaction);
|
|
await _paymentTransactionService.CommitAsync("System");
|
|
}
|
|
else
|
|
{
|
|
result.Success = false;
|
|
result.ErrorType = MangoPayErrorCodes.Err_PayIn_Failed;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_PayIn_Failed;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_AppUser_NotFound;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_Wallet_NotExists;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_AppUser_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Transaktion bei Mangopay zurück.
|
|
/// Kann zum Prüfen des Status einer Transaktion verwendet werden.
|
|
/// </summary>
|
|
/// <param name="mangoPayTransactionId">Id der Transaktion bei MangoPay</param>
|
|
/// <returns>MangoPayResult mit Transaktion</returns>
|
|
public async Task<MangoPayResult<MangoPayTransaction>> GetTransactionAsync(string mangoPayTransactionId)
|
|
{
|
|
var result = new MangoPayResult<MangoPayTransaction>() { Value = null };
|
|
|
|
try
|
|
{
|
|
var transactionResult = await _api.PayIns.GetAsync(mangoPayTransactionId);
|
|
|
|
if (transactionResult != null)
|
|
{
|
|
var transaction = new MangoPayTransaction
|
|
{
|
|
TransactionId = transactionResult.Id,
|
|
AuthorId = transactionResult.AuthorId,
|
|
CreditedUserId = transactionResult.CreditedUserId,
|
|
CreditedWalletId = transactionResult.CreditedWalletId,
|
|
AmmountCredited = transactionResult.CreditedFunds.Amount,
|
|
AmmountDebited = transactionResult.DebitedFunds.Amount,
|
|
Fees = transactionResult.Fees.Amount,
|
|
Status = (PaymentTransactionStatus)transactionResult.Status,
|
|
ResultCode = transactionResult.ResultCode,
|
|
ResultMessage = transactionResult.ResultMessage
|
|
};
|
|
if (transactionResult.ExecutionDate.HasValue)
|
|
transaction.ExecutionDate = transactionResult.ExecutionDate.Value.ToUniversalTime();
|
|
else
|
|
transaction.ExecutionDate = null;
|
|
transaction.TransactionType = (MangoPayTransactionType)transactionResult.PaymentType;
|
|
transaction.CreditedWalletId = transactionResult.CreditedWalletId;
|
|
transaction.DebitedWalletId = transactionResult.DebitedWalletId;
|
|
transaction.PayInType = (PayInType)transactionResult.PaymentType;
|
|
|
|
result.Success = true;
|
|
result.Value = transaction;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen eines Kyc-Dokuments
|
|
/// </summary>
|
|
/// <param name="mangoPayUserId">Id des Users bei MangoPay</param>
|
|
/// <param name="type">Typ des Dokumentes</param>
|
|
/// <param name="files">Liste von Dateien als byte-Array</param>
|
|
/// <returns>MangoPayResult mit der ID des Dokumentes</returns>
|
|
public async Task<MangoPayResult<string>> CreateKycDocumentAsync(string mangoPayUserId, IdentityDocumentType type, List<byte[]> files)
|
|
{
|
|
var result = new MangoPayResult<string>() { Value = string.Empty };
|
|
|
|
try
|
|
{
|
|
//Erstellen eines Kyc-Dokuments
|
|
var createKycDocumentResult = await _api.Users.CreateKycDocumentAsync(mangoPayUserId, (KycDocumentType)type);
|
|
if (createKycDocumentResult != null && createKycDocumentResult.Status == KycStatus.CREATED)
|
|
{
|
|
var documentId = createKycDocumentResult.Id;
|
|
var pagesSuccess = true;
|
|
foreach (var file in files)
|
|
{
|
|
if (pagesSuccess)
|
|
{
|
|
try
|
|
{
|
|
await _api.Users.CreateKycPageAsync(mangoPayUserId, documentId, file);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
pagesSuccess = false;
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Kyc_PageFailed;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (pagesSuccess)
|
|
{
|
|
var updateKycDocumentResult = await _api.Users.UpdateKycDocumentAsync(mangoPayUserId, new KycDocumentPutDTO() { Status = KycStatus.VALIDATION_ASKED }, documentId);
|
|
if (updateKycDocumentResult != null && updateKycDocumentResult.Status == KycStatus.VALIDATION_ASKED)
|
|
{
|
|
result.Success = true;
|
|
result.Value = documentId;
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_Kyc_DocumentFailed;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt ein Kyc-Dokument zurück
|
|
/// </summary>
|
|
/// <param name="documentId">Id des Dokumentes bei MangoPay</param>
|
|
/// <returns>Kyc-Dokument</returns>
|
|
public async Task<MangoPayResult<MangoPayKycDocument>> GetKycDocumentAsync(string documentId)
|
|
{
|
|
var result = new MangoPayResult<MangoPayKycDocument>() { Value = null };
|
|
|
|
try
|
|
{
|
|
var getDocumentResult = await _api.Kyc.GetAsync(documentId);
|
|
if (getDocumentResult != null)
|
|
{
|
|
var document = new MangoPayKycDocument();
|
|
document.DocumentId = getDocumentResult.Id;
|
|
document.UserId = getDocumentResult.UserId;
|
|
document.Type = (IdentityDocumentType)getDocumentResult.Type;
|
|
document.Status = (IdentityDocumentStatus)getDocumentResult.Status;
|
|
document.RefusedReasonType = getDocumentResult.RefusedReasonType;
|
|
document.RefusedReasonMessage = getDocumentResult.RefusedReasonMessage;
|
|
if(getDocumentResult.ProcessedDate.HasValue)
|
|
document.ProcessedDate = getDocumentResult.ProcessedDate.Value.ToUniversalTime();
|
|
else
|
|
document.ProcessedDate = null;
|
|
document.Flags = getDocumentResult.Flags;
|
|
result.Success = true;
|
|
result.Value = document;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Auszahlung zurück
|
|
/// </summary>
|
|
/// <param name="payoutId">Id der Auszalhung</param>
|
|
/// <returns>MangoPayResult mit Auszahlung oder null, wenn nicht vorhanden</returns>
|
|
public async Task<MangoPayResult<MangoPayPayout>> GetPayoutAsync(string payoutId)
|
|
{
|
|
var result = new MangoPayResult<MangoPayPayout>() { Value = null };
|
|
|
|
try
|
|
{
|
|
var getPayoutResult = await _api.PayOuts.GetAsync(payoutId);
|
|
if (getPayoutResult != null)
|
|
{
|
|
var payout = new MangoPayPayout();
|
|
payout.PayoutId = getPayoutResult.Id;
|
|
payout.UserId = getPayoutResult.CreditedUserId;
|
|
payout.Status = (PaymentTransactionStatus)getPayoutResult.Status;
|
|
payout.ResultCode = getPayoutResult.ResultCode;
|
|
payout.ResultMessage = getPayoutResult.ResultMessage;
|
|
if (getPayoutResult.ExecutionDate.HasValue)
|
|
payout.ExecutionDate = getPayoutResult.ExecutionDate.Value.ToUniversalTime();
|
|
else
|
|
payout.ExecutionDate = null;
|
|
result.Success = true;
|
|
result.Value = payout;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen einer Auszahlung
|
|
/// </summary>
|
|
/// <param name="mangoPayUserId">Id des Users bei Mangopay</param>
|
|
/// <param name="mangoPayWalletId">Id des Wallets bei MangoPay</param>
|
|
/// <param name="mangoPayBankId">Id der Bankverbindung bei MangoPay</param>
|
|
/// <param name="ammount">Summe die Ausgezahlt werden soll in der kleinsten Einheit</param>
|
|
/// <param name="tag">TAG mit Referenz auf gehgassi</param>
|
|
/// <returns>MangoPayResult mit erstellter Auszahlung</returns>
|
|
public async Task<MangoPayResult<MangoPayPayout>> CreatePayoutAsync(string mangoPayUserId, string mangoPayWalletId, string mangoPayBankId, long ammount, string tag)
|
|
{
|
|
var result = new MangoPayResult<MangoPayPayout>();
|
|
|
|
try
|
|
{
|
|
var payOutDto = new PayOutBankWirePostDTO(mangoPayUserId, mangoPayWalletId,
|
|
new Money() { Amount = ammount, Currency = CurrencyIso.EUR },
|
|
new Money() { Amount = 0, Currency = CurrencyIso.EUR },
|
|
mangoPayBankId, "gehgassi", PayoutModeRequested.STANDARD)
|
|
{
|
|
Tag = tag
|
|
};
|
|
|
|
var payOutResult = await _api.PayOuts.CreateBankWireAsync(payOutDto);
|
|
|
|
if (payOutResult != null)
|
|
{
|
|
var payout = new MangoPayPayout();
|
|
payout.PayoutId = payOutResult.Id;
|
|
payout.UserId = payOutResult.CreditedUserId;
|
|
payout.Status = (PaymentTransactionStatus)payOutResult.Status;
|
|
payout.ResultCode = payOutResult.ResultCode;
|
|
payout.ResultMessage = payOutResult.ResultMessage;
|
|
if (payOutResult.ExecutionDate.HasValue)
|
|
payout.ExecutionDate = payOutResult.ExecutionDate.Value.ToUniversalTime();
|
|
else
|
|
payout.ExecutionDate = null;
|
|
|
|
result.Success = true;
|
|
result.Value = payout;
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_PayOut_Failed;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt Infos zu einer Rückzahlung zurück
|
|
/// </summary>
|
|
/// <param name="refundId">Id der Rückzahlung</param>
|
|
/// <returns>MangoPayResult</returns>
|
|
public async Task<MangoPayResult<MangoPayRefund>> GetRefundAsync(string refundId)
|
|
{
|
|
var result = new MangoPayResult<MangoPayRefund>();
|
|
|
|
try
|
|
{
|
|
var refundResult = await _api.Refunds.GetAsync(refundId);
|
|
|
|
if (refundResult != null)
|
|
{
|
|
if (refundResult.Status == TransactionStatus.SUCCEEDED)
|
|
{
|
|
var refund = new MangoPayRefund();
|
|
refund.RefundId = refundResult.Id;
|
|
refund.OriginalTransactionId = refundResult.InitialTransactionId;
|
|
refund.RefundReasonType = refundResult.RefundReason.RefundReasonType;
|
|
refund.RefundReasonMessage = refundResult.RefundReason.RefundReasonMessage;
|
|
refund.Status = (PaymentTransactionStatus)refundResult.Status;
|
|
|
|
result.Success = true;
|
|
result.Value = refund;
|
|
|
|
}
|
|
else
|
|
{
|
|
result.Success = false;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Refund_Failed;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_Refund_Failed;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen eines Legal-Mangopay-Benutzers für einen Kunden
|
|
/// </summary>
|
|
/// <param name="customerId">Id des Kunden</param>
|
|
/// <param name="suppressCommit">Soll das Commit unterdrückt werden?</param>
|
|
/// <returns>MangoPayResult mit der ID des Mangopay-Users</returns>
|
|
public async Task<MangoPayResult<string>> CreateLegalOwnerAsync(long customerId, bool suppressCommit)
|
|
{
|
|
var result = new MangoPayResult<string>();
|
|
|
|
var customer = await _customerService.GetAsync(customerId);
|
|
if (customer != null)
|
|
{
|
|
var hasMangopayData = customer.HasDataForMangopay();
|
|
if (!hasMangopayData)
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_Customer_DataMissing;
|
|
return result;
|
|
}
|
|
|
|
var request = new UserLegalOwnerPostDTO()
|
|
{
|
|
HeadquartersAddress = new Address()
|
|
{
|
|
AddressLine1 = customer.Address.AddressLine1,
|
|
AddressLine2 = customer.Address.AddressLine2,
|
|
City = customer.Address.City,
|
|
Country = (CountryIso)Enum.Parse(typeof(CountryIso), customer.Address.CountryCode),
|
|
PostalCode = customer.Address.Zip,
|
|
Region = customer.Address.State
|
|
},
|
|
Name = customer.MangopayCompanyName,
|
|
LegalPersonType = LegalPersonType.ORGANIZATION,
|
|
LegalRepresentativeFirstName = customer.MangopayContactFirstName,
|
|
LegalRepresentativeLastName = customer.MangopayContactLastName,
|
|
LegalRepresentativeEmail = customer.MangopayContactEmail,
|
|
LegalRepresentativeBirthday = new DateTime(customer.MangopayContactBirthdate.Value.Year, customer.MangopayContactBirthdate.Value.Month, customer.MangopayContactBirthdate.Value.Day, 0, 0, 0, DateTimeKind.Utc),
|
|
LegalRepresentativeNationality = (CountryIso)Enum.Parse(typeof(CountryIso), customer.MangopayContactNationality),
|
|
LegalRepresentativeCountryOfResidence = (CountryIso)Enum.Parse(typeof(CountryIso), customer.MangopayContactMainResidenceCode),
|
|
CompanyNumber = customer.MangopayRegistrationNumber,
|
|
Email = customer.MangopayEmail,
|
|
TermsAndConditionsAccepted = customer.MangopayTermsAccepted,
|
|
UserCategory = UserCategory.OWNER
|
|
};
|
|
|
|
try
|
|
{
|
|
var response = await _api.Users.CreateOwnerAsync(request);
|
|
if (response != null)
|
|
{
|
|
customer.MangopayPaymentId = response.Id;
|
|
customer.MangopayIdCreated = true;
|
|
|
|
if (!suppressCommit)
|
|
{
|
|
await _appUserService.CommitAsync("System");
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Value = response.Id;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_AppUser_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aktualisieren eines Mangopay-Benutzers für einen Kunden
|
|
/// </summary>
|
|
/// <param name="customerId">Id des Kunden</param>
|
|
/// <returns>true wenn erfolgreich, false sonst</returns>
|
|
public async Task<MangoPayResult<bool>> UpdateLegalOwnerAsync(long customerId)
|
|
{
|
|
var result = new MangoPayResult<bool>
|
|
{
|
|
Value = false
|
|
};
|
|
|
|
var customer = await _customerService.GetAsync(customerId);
|
|
if (customer != null)
|
|
{
|
|
var hasMangopayData = customer.HasDataForMangopay();
|
|
if (!hasMangopayData)
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_Customer_DataMissing;
|
|
return result;
|
|
}
|
|
|
|
var request = new UserLegalPutDTO()
|
|
{
|
|
HeadquartersAddress = new Address()
|
|
{
|
|
AddressLine1 = customer.Address.AddressLine1,
|
|
AddressLine2 = customer.Address.AddressLine2,
|
|
City = customer.Address.City,
|
|
Country = (CountryIso)Enum.Parse(typeof(CountryIso), customer.Address.CountryCode),
|
|
PostalCode = customer.Address.Zip,
|
|
Region = customer.Address.State
|
|
},
|
|
Name = customer.MangopayCompanyName,
|
|
LegalPersonType = LegalPersonType.ORGANIZATION,
|
|
LegalRepresentativeFirstName = customer.MangopayContactFirstName,
|
|
LegalRepresentativeLastName = customer.MangopayContactLastName,
|
|
LegalRepresentativeEmail = customer.MangopayContactEmail,
|
|
LegalRepresentativeBirthday = new DateTime(customer.MangopayContactBirthdate.Value.Year, customer.MangopayContactBirthdate.Value.Month, customer.MangopayContactBirthdate.Value.Day, 0, 0, 0, DateTimeKind.Utc),
|
|
LegalRepresentativeNationality = (CountryIso)Enum.Parse(typeof(CountryIso), customer.MangopayContactNationality),
|
|
LegalRepresentativeCountryOfResidence = (CountryIso)Enum.Parse(typeof(CountryIso), customer.MangopayContactMainResidenceCode),
|
|
CompanyNumber = customer.MangopayRegistrationNumber,
|
|
Email = customer.MangopayEmail,
|
|
TermsAndConditionsAccepted = customer.MangopayTermsAccepted,
|
|
UserCategory = UserCategory.OWNER
|
|
};
|
|
|
|
try
|
|
{
|
|
var response = await _api.Users.UpdateLegalAsync(request, customer.MangopayPaymentId);
|
|
if (response != null)
|
|
{
|
|
result.Success = true;
|
|
result.Value = true;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_AppUser_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Prüfen ob ein MangoPay-Benutzer für einen Kunden existiert
|
|
/// </summary>
|
|
/// <param name="mangoPayUserId">Id des Mangopay-Benutzers des Kunden</param>
|
|
/// <returns>true wenn existent, false sonst</returns>
|
|
public async Task<MangoPayResult<bool>> LegalOwnerExistsAsync(string mangoPayUserId)
|
|
{
|
|
var result = new MangoPayResult<bool>
|
|
{
|
|
Value = false
|
|
};
|
|
|
|
if (!string.IsNullOrWhiteSpace(mangoPayUserId))
|
|
{
|
|
try
|
|
{
|
|
var response = await _api.Users.GetLegalAsync(mangoPayUserId);
|
|
if (response != null)
|
|
{
|
|
result.Success = true;
|
|
result.Value = true;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_User_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellt ein Wallet für einen Kunden mit einem bestimmten Typ.
|
|
/// </summary>
|
|
/// <param name="customerUniqueId">Unique Id des Kunden</param>
|
|
/// <param name="walletType">Typ des Wallets</param>
|
|
/// <param name="description">Beschreibung</param>
|
|
/// <returns>Wallet</returns>
|
|
public async Task<MangoPayResult<Wallet>> CreateWalletForCustomerAsync(string customerUniqueId, WalletType walletType, string description)
|
|
{
|
|
var result = new MangoPayResult<Wallet>();
|
|
|
|
var customer = await _customerService.GetByUniqueIdAsync(Guid.Parse(customerUniqueId));
|
|
if (customer != null)
|
|
{
|
|
//Zuerst versuchen das Wallet bei MangoPay anzulegen...
|
|
//Wallet noch nicht vorhanden, also anlegen
|
|
var request = new WalletPostDTO(new List<string>() { customer.MangopayPaymentId },description, CurrencyIso.EUR);
|
|
try
|
|
{
|
|
var response = await _api.Wallets.CreateAsync(request);
|
|
if (response != null)
|
|
{
|
|
var wallet = _walletService.Create(customerUniqueId, response.Id, walletType, customer.MangopayPaymentId, "EUR", description);
|
|
wallet.Balance = response.Balance.Amount;
|
|
wallet.UpdatedAt = DateTimeOffset.UtcNow;
|
|
_walletService.Add(wallet);
|
|
await _walletService.CommitAsync("System");
|
|
|
|
result.Success = true;
|
|
result.Value = wallet;
|
|
}
|
|
}
|
|
catch (ResponseException ex)
|
|
{
|
|
result.ErrorMessage = ex.ResponseError.Message;
|
|
result.ErrorType = ex.ResponseError.Type;
|
|
result.ErrorMessages = ex.ResponseError.errors;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = ex.Message;
|
|
result.ErrorType = MangoPayErrorCodes.Err_Unknown;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.ErrorType = MangoPayErrorCodes.Err_Customer_NotFound;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|