From 4158922e7cade6225ac94fd07b7969f945cecbaa Mon Sep 17 00:00:00 2001 From: "james.zhao" Date: Tue, 30 Oct 2018 18:34:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=87=E5=9C=A3=E8=8A=82=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=B4=BB=E5=8A=A8=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomerHalloweenCountServiceImpl.java | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/main/java/au/com/royalpay/payment/manage/customers/core/impls/CustomerHalloweenCountServiceImpl.java diff --git a/src/main/java/au/com/royalpay/payment/manage/customers/core/impls/CustomerHalloweenCountServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/customers/core/impls/CustomerHalloweenCountServiceImpl.java new file mode 100644 index 000000000..00c44643a --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/customers/core/impls/CustomerHalloweenCountServiceImpl.java @@ -0,0 +1,105 @@ +package au.com.royalpay.payment.manage.customers.core.impls; + +import au.com.royalpay.payment.core.events.AfterPaymentFinishEvent; +import au.com.royalpay.payment.manage.mappers.system.CustomerMapper; +import cn.yixblog.platform.http.HttpRequestGenerator; +import cn.yixblog.platform.http.HttpRequestResult; +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.codec.digest.DigestUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationListener; +import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.util.UriComponentsBuilder; + +import javax.annotation.Resource; +import java.math.BigDecimal; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +@Service +public class CustomerHalloweenCountServiceImpl implements ApplicationListener { + private Logger logger = LoggerFactory.getLogger(getClass()); + + @Value("${app.customer.host}") + private String CUSTOMER_HOST; + + @Value("${customer.app.appid}") + private String CUSTOMER_APP_ID; + + @Value("${customer.app.auth-code}") + private String CUSTOMER_AUTH_CODE; + + @Resource + private CustomerMapper customerMapper; + + @Override + public void onApplicationEvent(AfterPaymentFinishEvent event) { + final JSONObject order = event.getFinishedEvent().getOrder(); + //判断是否是pine测试商户,若不是则判断是否在活动时间 + if (order.getIntValue("client_id") != 9) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); + Date newDay = new Date(); + try { + if (newDay.compareTo(sdf.parse("2018-10-31 00:00:00")) < 0) { + return; + } + if (newDay.compareTo(sdf.parse("2018-11-02 00:00:00")) > 0) { + return; + } + } catch (ParseException e) { + + } + } + BigDecimal clearAmount = event.getFinishedEvent().getAudFee(); + String channel = order.getString("channel"); + if ("Wechat".equals(channel)) { + JSONObject customerRelation = customerMapper.findCustomerByOpenId(order.getString("customer_id")); + if (customerRelation == null) { + customerRelation = customerMapper.findCustomerGlobalpayByOpenId(order.getString("customer_id")); + } + if (customerRelation.getString("kanga_openid") == null) { + return; + } + afterPaymentAddHalloweenCount(customerRelation.getString("kanga_openid"), order.getString("order_id"), customerRelation.getString("redpack_openid"),channel, clearAmount); + } + if ("Alipay".equals(channel)) { + afterPaymentAddHalloweenCount(order.getString("customer_id"), order.getString("order_id"), "alipay",channel, clearAmount); + } + } + + private void afterPaymentAddHalloweenCount(String id, String orderId,String redPackId, String channel, BigDecimal clearAmount) { + String timestamp = System.currentTimeMillis() + ""; + String base = CUSTOMER_APP_ID + timestamp + CUSTOMER_AUTH_CODE; + String sign = DigestUtils.sha256Hex(base).toLowerCase(); + String uri = UriComponentsBuilder.fromHttpUrl(CUSTOMER_HOST + "activity/halloween/payment/orders") + .queryParam("appid", CUSTOMER_APP_ID) + .queryParam("timestamp", timestamp) + .queryParam("sign", sign) + .queryParam("id", id) + .queryParam("orderId", orderId) + .queryParam("channel", channel) + .queryParam("amount", clearAmount) + .queryParam("redpackId", redPackId) + .toUriString(); + if(clearAmount.compareTo(new BigDecimal("1.99"))<0){ + return; + } + HttpRequestGenerator gen = new HttpRequestGenerator(uri, RequestMethod.POST); + try { + HttpRequestResult result = gen.execute(); + if (result.isSuccess()) { + int statusCode = result.getStatusCode(); + if (statusCode == 200) { + logger.info("万圣节活动次数添加成功"); + } + } + + } catch (Exception ignored) { + ignored.printStackTrace(); + } + } +}