diff --git a/pom.xml b/pom.xml index 3ab279d..4173c0a 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,8 @@ 1.6.2 2.9.11 1.33 + 1.0.0 + 29.0.0 3.8.1 ${env.NACOS_SERVER_NAME} ${env.NACOS_SERVER_PORT} @@ -358,6 +360,20 @@ truelicense-core ${turelicense.version} + + + + com.paypal.sdk + paypal-server-sdk + ${paypal.version} + + + + + com.stripe + stripe-java + ${stripe.version} + diff --git a/sql/wfc_config_db/wfc_config_db.sql b/sql/wfc_config_db/wfc_config_db.sql index ba8ca68..34cd05a 100644 --- a/sql/wfc_config_db/wfc_config_db.sql +++ b/sql/wfc_config_db/wfc_config_db.sql @@ -87,7 +87,7 @@ INSERT INTO `config_info` VALUES (50, 'wfc-payment-test.yml', 'DEFAULT_GROUP', ' INSERT INTO `config_info` VALUES (51, 'sentinel-wfc-gateway-test', 'DEFAULT_GROUP', '[\r\n {\r\n \"resource\": \"wfc-auth\",\r\n \"count\": 500,\r\n \"grade\": 1,\r\n \"limitApp\": \"default\",\r\n \"strategy\": 0,\r\n \"controlBehavior\": 0\r\n },\r\n {\r\n \"resource\": \"wfc-system\",\r\n \"count\": 1000,\r\n \"grade\": 1,\r\n \"limitApp\": \"default\",\r\n \"strategy\": 0,\r\n \"controlBehavior\": 0\r\n },\r\n {\r\n \"resource\": \"wfc-gen\",\r\n \"count\": 200,\r\n \"grade\": 1,\r\n \"limitApp\": \"default\",\r\n \"strategy\": 0,\r\n \"controlBehavior\": 0\r\n },\r\n {\r\n \"resource\": \"wfc-job\",\r\n \"count\": 300,\r\n \"grade\": 1,\r\n \"limitApp\": \"default\",\r\n \"strategy\": 0,\r\n \"controlBehavior\": 0\r\n }\r\n]', 'a8b3ec396dd09d5f2ba494cc80a7afa0', '2024-12-28 19:28:44', '2024-12-28 19:28:44', NULL, '172.19.0.1', '', 'wfc-test', '限流策略', NULL, NULL, 'json', NULL, ''); INSERT INTO `config_info` VALUES (61, 'application-prod.yml', 'DEFAULT_GROUP', 'spring:\n autoconfigure:\n exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure\n mvc:\n pathmatch:\n matching-strategy: ant_path_matcher\n # 资源信息\n #messages:\n # 国际化资源文件路径\n #basename: i18n/messages\n \n# feign 配置\nfeign:\n sentinel:\n enabled: true\n okhttp:\n enabled: false\n httpclient:\n enabled: true\n disable-ssl-validation: true\n client:\n config:\n default:\n connectTimeout: 60000\n readTimeout: 60000\n compression:\n request:\n enabled: true\n min-request-size: 8192\n response:\n enabled: true\n\n# 暴露监控端点\nmanagement:\n endpoints:\n web:\n exposure:\n include: \'*\'\n \n\n \n', '35d3cd36aeef1b24d4fdb9f73e0fb2c6', '2024-12-13 11:52:34', '2025-02-08 09:39:47', NULL, '172.20.0.1', '', 'wfc-prod', '通用配置', '', '', 'yaml', '', ''); -INSERT INTO `config_info` VALUES (62, 'wfc-gateway-prod.yml', 'DEFAULT_GROUP', 'spring:\n redis:\n host: wfc-redis\n port: 6379\n password:\n cloud:\n gateway:\n discovery:\n locator:\n lowerCaseServiceId: true\n enabled: true\n routes:\n # 认证中心\n - id: wfc-auth\n uri: lb://wfc-auth\n predicates:\n - Path=/auth/**\n filters:\n # 验证码处理\n - CacheRequestFilter\n - ValidateCodeFilter\n - StripPrefix=1\n # 代码生成\n - id: wfc-gen\n uri: lb://wfc-gen\n predicates:\n - Path=/code/**\n filters:\n - StripPrefix=1\n # 定时任务\n - id: wfc-job\n uri: lb://wfc-job\n predicates:\n - Path=/schedule/**\n filters:\n - StripPrefix=1\n # 系统模块\n - id: wfc-system\n uri: lb://wfc-system\n predicates:\n - Path=/system/**\n filters:\n - StripPrefix=1\n # 用户模块\n - id: wfc-user\n uri: lb://wfc-user\n predicates:\n - Path=/u/**\n filters:\n - StripPrefix=1 \n # 文件服务\n - id: wfc-file\n uri: lb://wfc-file\n predicates:\n - Path=/file/**\n filters:\n - StripPrefix=1\n # payment\n - id: wfc-payment\n uri: lb://wfc-payment\n predicates:\n - Path=/payment/**\n filters:\n - StripPrefix=1 \n\n# 安全配置\nsecurity:\n # 验证码\n captcha:\n enabled: true\n mailEnabled: true\n type: math\n # 防止XSS攻击\n xss:\n enabled: true\n excludeUrls:\n - /system/notice\n # 不校验白名单\n ignore:\n whites:\n - /auth/logout\n - /auth/login\n - /auth/register\n - /auth/checkRepeat\n - /*/v2/api-docs\n - /csrf\n - /u/email/code\n - /system/email/code\n - /payment/aliPay/callback\n - /payment/wxPay/callback\n - /u/user/profile/forgotPwd\n - /system/user/profile/forgotPwd', '7c2ff7c096ccba897a41dbb18e979fec', '2024-12-13 11:52:34', '2025-02-13 03:14:41', NULL, '192.168.2.158', '', 'wfc-prod', 'wfc-gateway production', '', '', 'yaml', '', ''); +INSERT INTO `config_info` VALUES (62, 'wfc-gateway-prod.yml', 'DEFAULT_GROUP', 'spring:\n redis:\n host: wfc-redis\n port: 6379\n password:\n cloud:\n gateway:\n discovery:\n locator:\n lowerCaseServiceId: true\n enabled: true\n routes:\n # 认证中心\n - id: wfc-auth\n uri: lb://wfc-auth\n predicates:\n - Path=/auth/**\n filters:\n # 验证码处理\n - CacheRequestFilter\n - ValidateCodeFilter\n - StripPrefix=1\n # 代码生成\n - id: wfc-gen\n uri: lb://wfc-gen\n predicates:\n - Path=/code/**\n filters:\n - StripPrefix=1\n # 定时任务\n - id: wfc-job\n uri: lb://wfc-job\n predicates:\n - Path=/schedule/**\n filters:\n - StripPrefix=1\n # 系统模块\n - id: wfc-system\n uri: lb://wfc-system\n predicates:\n - Path=/system/**\n filters:\n - StripPrefix=1\n # 用户模块\n - id: wfc-user\n uri: lb://wfc-user\n predicates:\n - Path=/u/**\n filters:\n - StripPrefix=1 \n # 文件服务\n - id: wfc-file\n uri: lb://wfc-file\n predicates:\n - Path=/file/**\n filters:\n - StripPrefix=1\n # payment\n - id: wfc-payment\n uri: lb://wfc-payment\n predicates:\n - Path=/payment/**\n filters:\n - StripPrefix=1 \n\n# 安全配置\nsecurity:\n # 验证码\n captcha:\n enabled: true\n mailEnabled: true\n type: math\n # 防止XSS攻击\n xss:\n enabled: true\n excludeUrls:\n - /system/notice\n # 不校验白名单\n ignore:\n whites:\n - /auth/logout\n - /auth/login\n - /auth/register\n - /auth/checkRepeat\n - /*/v2/api-docs\n - /csrf\n - /u/email/code\n - /system/email/code\n - /payment/aliPay/callback\n - /payment/wxPay/callback\n - /u/user/profile/forgotPwd\n - /system/user/profile/forgotPwd\n - /payment/stripe/callback', '6da5647d73fe6680ed197fa2f2a2b310', '2024-12-13 11:52:34', '2025-04-25 10:44:09', NULL, '192.168.2.177', '', 'wfc-prod', 'wfc-gateway production', '', '', 'yaml', '', ''); INSERT INTO `config_info` VALUES (63, 'wfc-auth-prod.yml', 'DEFAULT_GROUP', 'spring:\n redis:\n host: wfc-redis\n port: 6379\n password:\n', 'c3f5481240e7581cc397f5c7918fd785', '2024-12-13 11:52:34', '2024-12-13 11:53:35', NULL, '192.168.2.116', '', 'wfc-prod', '认证中心', '', '', 'yaml', '', ''); INSERT INTO `config_info` VALUES (64, 'wfc-monitor-prod.yml', 'DEFAULT_GROUP', '# spring\nspring:\n security:\n user:\n name: wfc\n password: 123456\n boot:\n admin:\n ui:\n title: visual monitor\n', 'c8f896d284d5328aab16baeb81a3685c', '2024-12-13 11:52:34', '2024-12-13 11:54:04', NULL, '192.168.2.116', '', 'wfc-prod', '监控中心', '', '', 'yaml', '', ''); INSERT INTO `config_info` VALUES (65, 'wfc-system-prod.yml', 'DEFAULT_GROUP', '# spring配置\nspring:\n redis:\n host: wfc-redis\n port: 6379\n password:\n datasource:\n druid:\n stat-view-servlet:\n enabled: true\n loginUsername: admin\n loginPassword: 123456\n dynamic:\n druid:\n initial-size: 5\n min-idle: 5\n maxActive: 20\n maxWait: 60000\n connectTimeout: 30000\n socketTimeout: 60000\n timeBetweenEvictionRunsMillis: 60000\n minEvictableIdleTimeMillis: 300000\n validationQuery: SELECT 1 FROM DUAL\n testWhileIdle: true\n testOnBorrow: false\n testOnReturn: false\n poolPreparedStatements: true\n maxPoolPreparedStatementPerConnectionSize: 20\n filters: stat,slf4j\n connectionProperties: druid.stat.mergeSql\\=true;druid.stat.slowSqlMillis\\=5000\n datasource:\n # 主库数据源\n master:\n driver-class-name: com.mysql.cj.jdbc.Driver\n url: jdbc:mysql://wfc-mysql:3306/wfc_system_db?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8\n username: root\n password: 123456\n # 从库数据源\n user:\n driver-class-name: com.mysql.cj.jdbc.Driver\n url: jdbc:mysql://wfc-mysql:3306/wfc_user_db?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8\n username: root\n password: 123456\n\n# mybatis配置\nmybatis:\n # 搜索指定包别名\n typeAliasesPackage: org.wfc.system\n # 配置mapper的扫描,找到所有的mapper.xml映射文件\n mapperLocations: classpath:mapper/**/*.xml\n\n# swagger配置\nswagger:\n title: 系统模块接口文档\n license: Powered By wfc\n licenseUrl: https://wfc.vip', '6d6ace553dddd9e9396ea7007e7e8039', '2024-12-13 11:52:34', '2025-04-02 20:31:17', NULL, '192.168.2.158', '', 'wfc-prod', '系统模块', '', '', 'yaml', '', ''); diff --git a/wfc-modules/wfc-payment/pom.xml b/wfc-modules/wfc-payment/pom.xml index c7f4395..dc0e23b 100644 --- a/wfc-modules/wfc-payment/pom.xml +++ b/wfc-modules/wfc-payment/pom.xml @@ -106,17 +106,11 @@ com.paypal.sdk paypal-server-sdk - 1.0.0 - - - org.slf4j - slf4j-api - - - ch.qos.logback - logback-classic - - + + + + com.stripe + stripe-java diff --git a/wfc-modules/wfc-payment/src/main/java/org/wfc/payment/domain/StripeBean.java b/wfc-modules/wfc-payment/src/main/java/org/wfc/payment/domain/StripeBean.java new file mode 100644 index 0000000..ed9de47 --- /dev/null +++ b/wfc-modules/wfc-payment/src/main/java/org/wfc/payment/domain/StripeBean.java @@ -0,0 +1,24 @@ +package org.wfc.payment.domain; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * @description: stripe 配置 + * @author: cyc + * @since: 2025-04-24 + */ +@Data +@Component +@ConfigurationProperties(prefix = "stripe") +public class StripeBean { + + private String secretKey; + + private String publicKey; + + private String endpointSecret; + + private String domain; +} diff --git a/wfc-modules/wfc-payment/src/main/java/org/wfc/payment/pay/stripe/controller/StripeController.java b/wfc-modules/wfc-payment/src/main/java/org/wfc/payment/pay/stripe/controller/StripeController.java new file mode 100644 index 0000000..189e1dd --- /dev/null +++ b/wfc-modules/wfc-payment/src/main/java/org/wfc/payment/pay/stripe/controller/StripeController.java @@ -0,0 +1,37 @@ +package org.wfc.payment.pay.stripe.controller; + +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.wfc.common.core.domain.R; +import org.wfc.payment.pay.stripe.service.IStripeService; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * @description: stripe controller + * @author: cyc + * @since: 2025-04-24 + */ +@RestController +@RequestMapping("/stripe") +public class StripeController { + + @Resource + private IStripeService stripeService; + + @PostMapping("/pay/{orderId}") + public R stripePay(@PathVariable Long orderId) { + + return R.ok(stripeService.stripePay(orderId)); + } + + @PostMapping("/callback") + public String webhook(HttpServletRequest request, HttpServletResponse response) { + String result = stripeService.webhook(request, response); + return result; + } +} diff --git a/wfc-modules/wfc-payment/src/main/java/org/wfc/payment/pay/stripe/service/IStripeService.java b/wfc-modules/wfc-payment/src/main/java/org/wfc/payment/pay/stripe/service/IStripeService.java new file mode 100644 index 0000000..d0efad5 --- /dev/null +++ b/wfc-modules/wfc-payment/src/main/java/org/wfc/payment/pay/stripe/service/IStripeService.java @@ -0,0 +1,15 @@ +package org.wfc.payment.pay.stripe.service; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * @author: cyc + * @since: 2025-04-24 + */ +public interface IStripeService { + + String stripePay(Long orderId); + + String webhook(HttpServletRequest request, HttpServletResponse response); +} diff --git a/wfc-modules/wfc-payment/src/main/java/org/wfc/payment/pay/stripe/service/impl/StripeServiceImpl.java b/wfc-modules/wfc-payment/src/main/java/org/wfc/payment/pay/stripe/service/impl/StripeServiceImpl.java new file mode 100644 index 0000000..1dd0460 --- /dev/null +++ b/wfc-modules/wfc-payment/src/main/java/org/wfc/payment/pay/stripe/service/impl/StripeServiceImpl.java @@ -0,0 +1,215 @@ +package org.wfc.payment.pay.stripe.service.impl; + +import com.stripe.Stripe; +import com.stripe.exception.SignatureVerificationException; +import com.stripe.exception.StripeException; +import com.stripe.model.Event; +import com.stripe.model.EventDataObjectDeserializer; +import com.stripe.model.PaymentIntent; +import com.stripe.model.StripeObject; +import com.stripe.model.checkout.Session; +import com.stripe.net.Webhook; +import com.stripe.param.checkout.SessionCreateParams; +import com.stripe.param.checkout.SessionRetrieveParams; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.IOUtils; +import org.springframework.stereotype.Service; +import org.wfc.common.core.domain.R; +import org.wfc.payment.domain.StripeBean; +import org.wfc.payment.pay.stripe.service.IStripeService; +import org.wfc.user.api.RemoteUUserService; +import org.wfc.user.api.domain.vo.UOrderVo; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.InputStream; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.Objects; + +/** + * @author: cyc + * @since: 2025-04-24 + */ +@Slf4j +@Service +public class StripeServiceImpl implements IStripeService { + + @Resource + private RemoteUUserService remoteUUserService; + + @Resource + private StripeBean stripeBean; + + @Override + public String stripePay(Long orderId) { + + Stripe.apiKey = stripeBean.getSecretKey(); + String YOUR_DOMAIN = stripeBean.getDomain(); + + UOrderVo orderVo = getByOrderId(orderId); + Long totalFee = orderVo.getOrderAmount().multiply(BigDecimal.valueOf(100)).setScale(0, RoundingMode.HALF_UP).longValue(); + String productName = orderVo.getType() == 0 ? "Package" : "Recharge"; + + SessionCreateParams params = + SessionCreateParams.builder() + .setMode(SessionCreateParams.Mode.PAYMENT) + .setSuccessUrl(YOUR_DOMAIN + "") + .setCancelUrl(YOUR_DOMAIN + "") + .setClientReferenceId(orderId.toString()) + .addLineItem( + SessionCreateParams.LineItem.builder() + .setQuantity(1L) + .setPriceData( + SessionCreateParams.LineItem.PriceData.builder() + .setCurrency("usd") + .setUnitAmount(totalFee) + .setProductData( + SessionCreateParams.LineItem.PriceData.ProductData.builder() + .setName(productName) + .build()) + .build()) + .build()) + .build(); + + Session session = null; + try { + session = Session.create(params); + } catch (StripeException e) { + log.error("Stripe create session error", e); + throw new RuntimeException(e); + } + return session.getUrl(); + } + + @Override + public String webhook(HttpServletRequest request, HttpServletResponse response) { + Stripe.apiKey = stripeBean.getSecretKey(); + // Replace this endpoint secret with your endpoint's unique secret + // If you are testing with the CLI, find the secret by running 'stripe listen' + // If you are using an endpoint defined with the API or dashboard, look in your webhook settings + // at https://dashboard.stripe.com/webhooks + String endpointSecret = stripeBean.getEndpointSecret(); + + String payload = null; + try { + InputStream inputStream = request.getInputStream(); + byte[] bytes = IOUtils.toByteArray(inputStream); + payload = new String(bytes, "UTF-8"); + } catch (IOException e) { + log.error("Stripe webhook payload error", e); + response.setStatus(400); + return ""; + } + +// String payload = request.body(); + Event event = null; + +// try { +// event = ApiResource.GSON.fromJson(payload, Event.class); +// } catch (JsonSyntaxException e) { +// // Invalid payload +// System.out.println("⚠️ Webhook error while parsing basic request."); +// response.status(400); +// return ""; +// } + String sigHeader = request.getHeader("Stripe-Signature"); + if (endpointSecret != null && sigHeader != null) { + // Only verify the event if you have an endpoint secret defined. + // Otherwise use the basic event deserialized with GSON. + try { + event = Webhook.constructEvent( + payload, sigHeader, endpointSecret + ); + } catch (SignatureVerificationException e) { + // Invalid signature + log.error("Webhook error while validating signature.", e); + response.setStatus(400); + return ""; + } + } + // Deserialize the nested object inside the event + EventDataObjectDeserializer dataObjectDeserializer = event.getDataObjectDeserializer(); + StripeObject stripeObject = null; + if (dataObjectDeserializer.getObject().isPresent()) { + stripeObject = dataObjectDeserializer.getObject().get(); + } else { + // Deserialization failed, probably due to an API version mismatch. + // Refer to the Javadoc documentation on `EventDataObjectDeserializer` for + // instructions on how to handle this case, or return an error here. + } + // Handle the event + switch (event.getType()) { + case "payment_intent.succeeded": + PaymentIntent paymentIntent = (PaymentIntent) stripeObject; + System.out.println("Payment for " + paymentIntent.getAmount() + " succeeded."); + // Then define and call a method to handle the successful payment intent. + // handlePaymentIntentSucceeded(paymentIntent); + break; + case "checkout.session.completed": +// TreeMap data = JSONObject.parseObject(dataObjectDeserializer.getRawJson(), TreeMap.class); +// String preOrderId = data.get("client_reference_id").toString(); + + Session sessionEvent= (Session) stripeObject; + + fulfillCheckout(sessionEvent); + + // Then define and call a method to handle the successful attachment of a PaymentMethod. + // handlePaymentMethodAttached(paymentMethod); + break; + default: + log.error("Unhandled event type: {}", event.getType()); + break; + } + response.setStatus(200); + return ""; + } + + public void fulfillCheckout(Session sessionEvent) { + // Set your secret key. Remember to switch to your live secret key in production. + // See your keys here: https://dashboard.stripe.com/apikeys + Stripe.apiKey = stripeBean.getSecretKey(); + + String sessionId = sessionEvent.getId(); + log.info("Fulfilling Checkout Session {}", sessionId); + + // Make this function safe to run multiple times, + // even concurrently, with the same session ID + + // Make sure fulfillment hasn't already been + // performed for this Checkout Session + + // Retrieve the Checkout Session from the API with line_items expanded + SessionRetrieveParams params = + SessionRetrieveParams.builder() + .addExpand("line_items") + .build(); + + Session checkoutSession = null; + try { + checkoutSession = Session.retrieve(sessionId, params, null); + } catch (StripeException e) { + log.error("Stripe webhook retrieve error", e); + throw new RuntimeException(e); + } + + // Check the Checkout Session's payment_status property + // to determine if fulfillment should be performed + if (!Objects.equals(checkoutSession.getPaymentStatus(), "unpaid")) { + // Perform fulfillment of the line items + + // Record/save fulfillment status for this + // Checkout Session + remoteUUserService.paySuccess(Long.valueOf(sessionEvent.getClientReferenceId()), "inner"); + } + } + + private UOrderVo getByOrderId(Long orderId) { + R orderRes = remoteUUserService.getOrderById(orderId); + UOrderVo orderVo = orderRes.getData(); + + return orderVo; + } +} diff --git a/wfc-modules/wfc-payment/src/main/resources/application.yml b/wfc-modules/wfc-payment/src/main/resources/application.yml index 78722e6..f7832cf 100644 --- a/wfc-modules/wfc-payment/src/main/resources/application.yml +++ b/wfc-modules/wfc-payment/src/main/resources/application.yml @@ -77,3 +77,8 @@ paypal: client-id: AfPgwFAmo9K7KCqiiGpNRCyQMSxI6V33eH-nEMnVndJNVEYOEOEn5wSPkHUybfzcjDLnBejt-RKnIfqX client_secret: EOYQzSGuMaTMWodcppZUTz9v3H9j38yYiv8bmj4kLZl5NiSUJ0AJJuGlA1CU4oDtEX6jNdGNhsMCiAcN sandBox: true + +stripe: + secret-key: sk_test_51RHGN8FwutpVO5TqqmAkJNYMlWDPgwj4NVKPxcPKEXMGSPpEZ4yKwpGancV1vyPP74Pk3ETPUdAws0CfiH1jTN9v00kQ64suj5 + domain: http://localhost:8085 + endpoint-secret: whsec_rD5GFCx37wIS3Ag67ocPHWoD2WGIfWyC