Skip to content

Commit

Permalink
Close #6: Remove IPaymentAuthorizer
Browse files Browse the repository at this point in the history
  • Loading branch information
gdlcf88 committed Jun 3, 2020
1 parent 0f365d4 commit aeb7327
Show file tree
Hide file tree
Showing 22 changed files with 123 additions and 78 deletions.
2 changes: 1 addition & 1 deletion modules/EasyAbp.PaymentService.WeChatPay/common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>0.1.8</Version>
<Version>0.2.0</Version>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>EasyAbp Team</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public class WeChatPayPaymentServiceProvider : IPaymentServiceProvider, ITransie
_paymentRepository = paymentRepository;
}

public async Task<Payment> PayAsync(Payment payment, Dictionary<string, object> inputExtraProperties,
Dictionary<string, object> payeeConfigurations)
public async Task<Payment> PayAsync(Payment payment, Dictionary<string, object> payeeConfigurations)
{
if (payment.Currency != "CNY")
{
Expand All @@ -60,7 +59,7 @@ public class WeChatPayPaymentServiceProvider : IPaymentServiceProvider, ITransie

payment.SetPayeeAccount(payeeAccount);

var appId = inputExtraProperties.GetOrDefault("appid") as string;
var appId = payment.ExtraProperties.GetOrDefault("appid") as string;

var openId = await _paymentOpenIdProvider.FindUserOpenIdAsync(appId, payment.UserId);

Expand All @@ -84,7 +83,7 @@ public class WeChatPayPaymentServiceProvider : IPaymentServiceProvider, ITransie
goodsTag: payeeConfigurations.GetOrDefault("goods_tag") as string,
notifyUrl: payeeConfigurations.GetOrDefault("notify_url") as string
?? _configuration["App:SelfUrl"].EnsureEndsWith('/') + "WeChatPay/Notify",
tradeType: inputExtraProperties.GetOrDefault("trade_type") as string,
tradeType: payment.ExtraProperties.GetOrDefault("trade_type") as string,
productId: null,
limitPay: payeeConfigurations.GetOrDefault("limit_pay") as string,
openId: openId,
Expand Down
2 changes: 1 addition & 1 deletion modules/EasyAbp.PaymentService/common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>0.1.8</Version>
<Version>0.2.0</Version>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>EasyAbp Team</Authors>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Volo.Abp;

namespace EasyAbp.PaymentService.Payments
{
public class DuplicatePaymentRequestException : BusinessException
{
public DuplicatePaymentRequestException() : base(message: $"An payment item in the payment request is already in progress.")
{
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Data;
using Volo.Abp.Users;

namespace EasyAbp.PaymentService.Payments
Expand Down Expand Up @@ -56,25 +57,45 @@ public override async Task<PaymentDto> CreateAsync(CreatePaymentDto input)
throw new MultiCurrencyNotSupportedException();
}

if (await HasDuplicatePaymentItemInProgressAsync(paymentItems))
{
throw new DuplicatePaymentRequestException();
}

var payment = new Payment(GuidGenerator.Create(), CurrentTenant.Id, CurrentUser.GetId(),
input.PaymentMethod, input.Currency, paymentItems.Select(item => item.OriginalPaymentAmount).Sum(),
paymentItems);

foreach (var property in input.ExtraProperties)
{
payment.SetProperty(property.Key, property.Value);
}

await Repository.InsertAsync(payment, autoSave: true);

await CheckPayableAsync(payment, input.ExtraProperties);

var payeeConfigurations = await GetPayeeConfigurationsAsync(payment, input.ExtraProperties);
var payeeConfigurations = await GetPayeeConfigurationsAsync(payment);

// Todo: payment discount

await provider.PayAsync(payment, input.ExtraProperties, payeeConfigurations);
await provider.PayAsync(payment, payeeConfigurations);

return MapToGetOutputDto(payment);
}

protected virtual Task<Dictionary<string, object>> GetPayeeConfigurationsAsync(Payment payment,
Dictionary<string, object> inputExtraProperties)
protected virtual async Task<bool> HasDuplicatePaymentItemInProgressAsync(IEnumerable<PaymentItem> paymentItems)
{
foreach (var item in paymentItems)
{
if (await _repository.FindPaymentInProgressByPaymentItem(item.ItemType, item.ItemKey) != null)
{
return true;
}
}

return false;
}

protected virtual Task<Dictionary<string, object>> GetPayeeConfigurationsAsync(Payment payment)
{
// Todo: use payee configurations provider.
// Todo: get store side payee configurations.
Expand All @@ -84,27 +105,6 @@ public override async Task<PaymentDto> CreateAsync(CreatePaymentDto input)
return Task.FromResult(payeeConfigurations);
}

protected virtual async Task CheckPayableAsync(Payment payment, Dictionary<string, object> inputExtraProperties)
{
var itemSet = new HashSet<PaymentItem>(payment.PaymentItems);

foreach (var authorizer in ServiceProvider.GetServices<IPaymentAuthorizer>())
{
foreach (var item in itemSet.ToList())
{
if (await authorizer.IsPaymentItemAllowedAsync(payment, item, inputExtraProperties))
{
itemSet.Remove(item);
}
}
}

if (!itemSet.IsNullOrEmpty())
{
throw new PaymentItemNotPayableException(itemSet.Select(item => item.ItemKey).ToList());
}
}

[RemoteService(false)]
public override Task<PaymentDto> UpdateAsync(Guid id, object input)
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using Volo.Abp.Domain.Entities;
using System.Collections.Generic;
using Volo.Abp.MultiTenancy;

namespace EasyAbp.PaymentService.Payments
{
public interface IPayment : IAggregateRoot<Guid>, IMultiTenant
public interface IPayment
{
Guid UserId { get; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using JetBrains.Annotations;

namespace EasyAbp.PaymentService.Payments
{
public interface IPaymentItem
{
string ItemType { get; }

Guid ItemKey { get; }

string Currency { get; }

decimal OriginalPaymentAmount { get; }

decimal PaymentDiscount { get; }

decimal ActualPaymentAmount { get; }

decimal RefundAmount { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;
using Volo.Abp.Data;

namespace EasyAbp.PaymentService.Payments
{
[Serializable]
public class PaymentEto
public class PaymentEto : IPayment, IHasExtraProperties
{
public Guid UserId { get; set; }

Expand All @@ -14,6 +15,8 @@ public class PaymentEto

public string PaymentMethod { get; set; }

public string PayeeAccount { get; set; }

public string ExternalTradingCode { get; set; }

public string Currency { get; set; }
Expand All @@ -29,7 +32,9 @@ public class PaymentEto
public DateTime? CompletionTime { get; set; }

public DateTime? CancelledTime { get; set; }


public Dictionary<string, object> ExtraProperties { get; set; }

public List<PaymentItemEto> PaymentItems { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace EasyAbp.PaymentService.Payments
{
[Serializable]
public class PaymentItemEto
public class PaymentItemEto : IPaymentItem
{
public Guid Id { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using Volo.Abp.Domain.Entities;
using Volo.Abp.MultiTenancy;

namespace EasyAbp.PaymentService.Refunds
{
public interface IRefund : IAggregateRoot<Guid>, IMultiTenant
public interface IRefund
{
Guid PaymentId { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public class FreePaymentServiceProvider : IPaymentServiceProvider, ITransientDep
_paymentRepository = paymentRepository;
}

public async Task<Payment> PayAsync(Payment payment, Dictionary<string, object> inputExtraProperties,
Dictionary<string, object> payeeConfigurations)
public async Task<Payment> PayAsync(Payment payment, Dictionary<string, object> payeeConfigurations)
{
if (payment.ActualPaymentAmount != decimal.Zero)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using Volo.Abp.Domain.Entities;
using Volo.Abp.MultiTenancy;

namespace EasyAbp.PaymentService.Payments
{
public interface IPaymentEntity : IPayment, IAggregateRoot<Guid>, IMultiTenant
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using Volo.Abp.Domain.Entities;

namespace EasyAbp.PaymentService.Payments
{
public interface IPaymentItemEntity : IPaymentItem, IEntity<Guid>
{

}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace EasyAbp.PaymentService.Payments
{
public interface IPaymentRepository : IRepository<Payment, Guid>
{
Task<Payment> FindPaymentInProgressByPaymentItem(string paymentItemType, Guid paymentItemKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace EasyAbp.PaymentService.Payments
{
public interface IPaymentServiceProvider
{
Task<Payment> PayAsync(Payment payment, Dictionary<string, object> inputExtraProperties,
Dictionary<string, object> payeeConfigurations);
Task<Payment> PayAsync(Payment payment, Dictionary<string, object> payeeConfigurations);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace EasyAbp.PaymentService.Payments
{
public class Payment : FullAuditedAggregateRoot<Guid>, IPayment
public class Payment : FullAuditedAggregateRoot<Guid>, IPaymentEntity
{
public virtual Guid? TenantId { get; protected set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace EasyAbp.PaymentService.Payments
{
public class PaymentItem : FullAuditedEntity<Guid>
public class PaymentItem : FullAuditedEntity<Guid>, IPaymentItemEntity
{
[NotNull]
public virtual string ItemType { get; protected set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using Volo.Abp.Domain.Entities;
using Volo.Abp.MultiTenancy;

namespace EasyAbp.PaymentService.Refunds
{
public interface IRefundEntity : IRefund, IAggregateRoot<Guid>, IMultiTenant
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace EasyAbp.PaymentService.Refunds
{
public class Refund : FullAuditedAggregateRoot<Guid>, IRefund
public class Refund : FullAuditedAggregateRoot<Guid>, IRefundEntity
{
public virtual Guid? TenantId { get; protected set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using EasyAbp.PaymentService.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
Expand All @@ -17,5 +18,14 @@ public override IQueryable<Payment> WithDetails()
{
return base.WithDetails().Include(x => x.PaymentItems);
}

public virtual async Task<Payment> FindPaymentInProgressByPaymentItem(string paymentItemType, Guid paymentItemKey)
{
return await base.WithDetails()
.Where(payment => !payment.CompletionTime.HasValue && !payment.CancelledTime.HasValue)
.FirstOrDefaultAsync(payment =>
payment.PaymentItems.Any(item =>
item.ItemType == paymentItemType && item.ItemKey == paymentItemKey));
}
}
}

0 comments on commit aeb7327

Please sign in to comment.