Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
pannanxu committed Oct 16, 2024
1 parent a3fd247 commit 925d825
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 112 deletions.
14 changes: 2 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,15 @@ public class StarterPlugin extends BasePlugin {

@Override
public void start() {
// 1. 注册支付通道
Payment.register(IPayment payment);
// 2. 注册业务回调通知
Payment.register(INotification notification);

// 用户选择在页面点击立即购买后,通过业务插件接口创建订单,然后跳转到收银台页面(/payment/{orderNo})
Payment.createOrder(Order order);
System.out.println("插件启动成功!");
}

@Override
public void stop() {
// 1. 卸载支付通道
Payment.unregister(IPayment payment);
// 2. 卸载业务回调通知
Payment.unregister(INotification notification);

System.out.println("插件停止!");
}
}

// 3. 用户选择在页面点击立即购买后,通过业务插件接口创建订单,然后跳转到收银台页面(/payment/{orderNo})
Payment.createOrder(Order order);
```
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repositories {
}

dependencies {
implementation platform('run.halo.tools.platform:plugin:2.11.0-SNAPSHOT')
implementation platform('run.halo.tools.platform:plugin:2.20.0-SNAPSHOT')
compileOnly 'run.halo.app:api'

testImplementation 'run.halo.app:api'
Expand Down Expand Up @@ -55,7 +55,7 @@ build {
}

halo {
version = '2.17'
version = '2.20'
superAdminUsername = 'admin'
superAdminPassword = '123123'
externalUrl = 'http://localhost:8090'
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/net/nanxu/payment/ExternalLinkCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.nanxu.payment;

import org.springframework.stereotype.Component;
import run.halo.app.infra.ExternalLinkProcessor;

/**
* ExternalLinkCreator.
*
* @author: P
**/
@Component
public class ExternalLinkCreator {
private final ExternalLinkProcessor externalLinkProcessor;

public ExternalLinkCreator(ExternalLinkProcessor externalLinkProcessor) {
this.externalLinkProcessor = externalLinkProcessor;
}

public String paymentUrl(String orderNo, String channel) {
return externalLinkProcessor.processLink("/payment/" + orderNo + "/pay/" + channel);
}

public String callbackUrl(String internal, String orderNo, String channel) {
return externalLinkProcessor.processLink("/payment/" + internal + "/" + orderNo + "/callback/" + channel);
}
}
23 changes: 23 additions & 0 deletions src/main/java/net/nanxu/payment/NotificationExtensionGetter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.nanxu.payment;

import java.util.List;
import lombok.RequiredArgsConstructor;
import net.nanxu.payment.infra.INotification;
import org.springframework.stereotype.Component;
import run.halo.app.plugin.extensionpoint.ExtensionGetter;

/**
* NotificationExtensionGetter.
*
* @author: P
**/
@RequiredArgsConstructor
@Component
public class NotificationExtensionGetter {
private final ExtensionGetter extensionGetter;

public List<INotification> getNotificationExtensions() {
return extensionGetter.getExtensionList(INotification.class);
}

}
17 changes: 0 additions & 17 deletions src/main/java/net/nanxu/payment/Payment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import lombok.NonNull;
import net.nanxu.payment.account.IAccount;
import net.nanxu.payment.infra.INotification;
import net.nanxu.payment.infra.IPayment;
import net.nanxu.payment.infra.PaymentProfile;
import net.nanxu.payment.infra.model.Order;
Expand Down Expand Up @@ -45,22 +44,6 @@ public static Mono<IPayment> getPayment(String name) {
return factory.getPayment(name);
}

public static void register(IPayment payment) {
factory.register(payment);
}

public static void unregister(IPayment payment) {
factory.unregister(payment);
}

public static void register(INotification notification) {
factory.register(notification);
}

public static void unregister(INotification notification) {
factory.unregister(notification);
}

public static ServiceFactory getServiceFactory() {
return factory.getServiceFactory();
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/net/nanxu/payment/PaymentExtensionGetter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.nanxu.payment;

import lombok.RequiredArgsConstructor;
import net.nanxu.payment.infra.IPayment;
import org.springframework.stereotype.Component;
import run.halo.app.plugin.extensionpoint.ExtensionGetter;
import java.util.List;

/**
* PaymentExtensionGetter.
*
* @author: P
**/
@RequiredArgsConstructor
@Component
public class PaymentExtensionGetter {
private final ExtensionGetter extensionGetter;

public List<IPayment> getPaymentExtensions() {
return extensionGetter.getExtensionList(IPayment.class);
}

}
28 changes: 5 additions & 23 deletions src/main/java/net/nanxu/payment/PaymentFactory.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.nanxu.payment;

import lombok.Getter;
import net.nanxu.payment.infra.INotification;
import net.nanxu.payment.infra.IPayment;
import net.nanxu.payment.infra.PaymentProfile;
import net.nanxu.payment.infra.model.PaymentSupport;
Expand Down Expand Up @@ -32,12 +31,14 @@ public final class PaymentFactory {
@Getter
private final ServiceFactory serviceFactory;

public PaymentFactory(ReactiveExtensionClient client) {
public PaymentFactory(ReactiveExtensionClient client,
PaymentExtensionGetter paymentExtensionGetter,
NotificationExtensionGetter notificationExtensionGetter) {
this.client = client;
this.paymentRegistry = new PaymentRegistry();
this.paymentRegistry = new PaymentRegistry(paymentExtensionGetter);
this.router = new PaymentRouter(paymentRegistry);
this.security = new SecurityRegistry();
this.notificationRegistry = new NotificationRegistry();
this.notificationRegistry = new NotificationRegistry(notificationExtensionGetter);
this.serviceFactory =
ServiceFactory.create(paymentRegistry, notificationRegistry, security, client);
}
Expand All @@ -56,23 +57,4 @@ public Mono<IPayment> getPayment(String name) {
return Mono.justOrEmpty(paymentRegistry.get(name));
}

/**
* 注册支付方式
*/
public void register(IPayment payment) {
paymentRegistry.register(payment);
}

public void unregister(IPayment payment) {
paymentRegistry.unregister(payment);
}

public void register(INotification notification) {
notificationRegistry.register(notification);
}

public void unregister(INotification notification) {
notificationRegistry.unregister(notification);
}

}
14 changes: 0 additions & 14 deletions src/main/java/net/nanxu/payment/PaymentPlugin.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package net.nanxu.payment;

import net.nanxu.payment.infra.IPayment;
import net.nanxu.payment.infra.model.Order;
import net.nanxu.testplugin.AliPayment;
import net.nanxu.testplugin.WeChatPayment;
import org.springframework.stereotype.Component;
import run.halo.app.extension.SchemeManager;
import run.halo.app.plugin.BasePlugin;
Expand All @@ -22,9 +19,6 @@ public class PaymentPlugin extends BasePlugin {

private final SchemeManager schemeManager;

private final IPayment wechat = new WeChatPayment();
private final IPayment ali = new AliPayment();

public PaymentPlugin(PluginContext pluginContext, SchemeManager schemeManager) {
super(pluginContext);
this.schemeManager = schemeManager;
Expand All @@ -33,20 +27,12 @@ public PaymentPlugin(PluginContext pluginContext, SchemeManager schemeManager) {
@Override
public void start() {
schemeManager.register(Order.class);

Payment.register(wechat);
Payment.register(ali);

System.out.println("插件启动成功!");
}

@Override
public void stop() {
schemeManager.unregister(schemeManager.get(Order.class));

Payment.unregister(wechat);
Payment.unregister(ali);

System.out.println("插件停止!");
}
}
3 changes: 2 additions & 1 deletion src/main/java/net/nanxu/payment/infra/INotification.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package net.nanxu.payment.infra;

import net.nanxu.payment.infra.model.Order;
import org.pf4j.ExtensionPoint;
import reactor.core.publisher.Mono;

/**
* 通知第三方业务插件.
*
* @author: P
**/
public interface INotification {
public interface INotification extends ExtensionPoint {

String getName();

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/nanxu/payment/infra/IPayment.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import net.nanxu.payment.infra.model.QueryResult;
import net.nanxu.payment.infra.model.RefundRequest;
import net.nanxu.payment.infra.model.RefundResult;
import org.pf4j.ExtensionPoint;
import reactor.core.publisher.Mono;

/**
* IPayment.
*
* @author: P
**/
public interface IPayment {
public interface IPayment extends ExtensionPoint {

String getName();

Expand Down
19 changes: 9 additions & 10 deletions src/main/java/net/nanxu/payment/registry/NotificationRegistry.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.nanxu.payment.registry;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import net.nanxu.payment.NotificationExtensionGetter;
import net.nanxu.payment.exception.NotificationException;
import net.nanxu.payment.infra.INotification;

/**
Expand All @@ -11,17 +11,16 @@
**/
public class NotificationRegistry {

private final Map<String, INotification> notification = new ConcurrentHashMap<>();
private final NotificationExtensionGetter notificationExtensionGetter;

public void register(INotification business) {
notification.put(business.getName(), business);
}

public void unregister(INotification business) {
notification.remove(business.getName());
public NotificationRegistry(NotificationExtensionGetter notificationExtensionGetter) {
this.notificationExtensionGetter = notificationExtensionGetter;
}

public INotification getNotification(String name) {
return notification.get(name);
return notificationExtensionGetter.getNotificationExtensions().stream()
.filter(e -> e.getName().equals(name))
.findFirst()
.orElseThrow(() -> new NotificationException("不支持此通知类型"));
}
}
34 changes: 7 additions & 27 deletions src/main/java/net/nanxu/payment/registry/PaymentRegistry.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package net.nanxu.payment.registry;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import lombok.extern.slf4j.Slf4j;
import net.nanxu.payment.PaymentExtensionGetter;
import net.nanxu.payment.exception.PaymentException;
import net.nanxu.payment.infra.IPayment;

/**
Expand All @@ -14,37 +14,17 @@
@Slf4j
public class PaymentRegistry {

private final Map<String, IPayment> payments = new ConcurrentHashMap<>();
private final PaymentExtensionGetter paymentExtensionGetter;

public PaymentRegistry() {
}

public void register(IPayment payment) {
IPayment pay = payments.get(payment.getName());
unregister(pay);
payment.register();
payments.put(payment.getName(), payment);
}

public void unregister(IPayment payment) {
if (null != payment && payments.containsKey(payment.getName())) {
payments.remove(payment.getName()).unregister();
}
}

public void unregisterAll() {
getPayments().forEach(this::unregister);
}

public void unregister(String name) {
payments.remove(name);
public PaymentRegistry(PaymentExtensionGetter paymentExtensionGetter) {
this.paymentExtensionGetter = paymentExtensionGetter;
}

public IPayment get(String name) {
return payments.get(name);
return getPayments().stream().filter(e -> e.getName().equals(name)).findFirst().orElseThrow(() -> new PaymentException("不支持此支付通道"));
}

public List<IPayment> getPayments() {
return payments.values().stream().toList();
return paymentExtensionGetter.getPaymentExtensions();
}
}
2 changes: 2 additions & 0 deletions src/main/java/net/nanxu/testplugin/AliPayment.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
import net.nanxu.payment.infra.model.QueryResult;
import net.nanxu.payment.infra.model.RefundRequest;
import net.nanxu.payment.infra.model.RefundResult;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
* AliPayment.
*
* @author: P
**/
@Component
public class AliPayment extends AbstractPayment {

public static final String NAME = "Ali";
Expand Down
Loading

0 comments on commit 925d825

Please sign in to comment.