LinePay.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package com.ruoyi.web.line;
  2. import java.io.IOException;
  3. import java.io.Serializable;
  4. import java.net.URISyntaxException;
  5. import java.text.MessageFormat;
  6. import java.util.UUID;
  7. import org.apache.commons.codec.binary.Base64;
  8. import org.apache.commons.codec.digest.HmacAlgorithms;
  9. import org.apache.commons.codec.digest.HmacUtils;
  10. import org.apache.hc.client5.http.classic.methods.HttpPost;
  11. import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
  12. import org.apache.hc.client5.http.impl.classic.HttpClients;
  13. import org.apache.hc.core5.http.ContentType;
  14. import org.apache.hc.core5.http.io.entity.EntityUtils;
  15. import org.apache.hc.core5.http.io.entity.StringEntity;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Component;
  20. import com.fasterxml.jackson.databind.ObjectMapper;
  21. import com.ruoyi.web.pcApi.domain.TOrderManage;
  22. import com.ruoyi.web.pcApi.domain.TOrderManageTotal;
  23. import com.ruoyi.web.pcApi.service.ITOrderManageService;
  24. @Component
  25. public class LinePay implements Serializable {
  26. private static final long serialVersionUID = 1L;
  27. /** log */
  28. private static final Logger log = LoggerFactory.getLogger(LinePay.class);
  29. @Autowired
  30. private LineConfig lineConfig;
  31. @Autowired
  32. private ITOrderManageService orderManageService;
  33. /**
  34. * create the line pay request body.
  35. *
  36. * @param orderManageTotal 订单管理-总对象
  37. * @return line pay request body
  38. */
  39. public SendBody createSendBody(TOrderManageTotal orderManageTotal, String deptName) {
  40. //create line pay request body
  41. SendBody sendBody = new SendBody();
  42. //订单金额
  43. sendBody.setAmount(orderManageTotal.getOrderMoney().intValue());
  44. //order id
  45. sendBody.setOrderId(String.valueOf(orderManageTotal.getId()));
  46. for (TOrderManage orderManage : orderManageTotal.getOrderList()) {
  47. TOrderManage order = orderManageService.selectTOrderManageById(orderManage.getId());
  48. //send order
  49. Orders ord = new Orders();
  50. //注文番号
  51. ord.setId(order.getOrderNo());
  52. //決済金額
  53. ord.setAmount(order.getOrderMoney().intValue());
  54. //パッケージの名前(または、加盟店内の出店業者の名前)
  55. ord.setName(deptName);
  56. //add the packages
  57. sendBody.add(ord);
  58. OrderDetail detail = new OrderDetail();
  59. //販売商品の名前
  60. detail.setName(order.getCommodityName());
  61. //販売商品の画像URL
  62. detail.setImageUrl(order.getFaceImg());
  63. //商品数
  64. detail.setQuantity(1);
  65. //各商品の決済金額
  66. detail.setPrice(order.getOrderMoney().intValue());
  67. ord.add(detail);
  68. }
  69. //call back urls
  70. RedirectUrls callBack = new RedirectUrls();
  71. //confirm url
  72. callBack.setConfirmUrl(lineConfig.callbankConfirmUrl);
  73. //cancel url
  74. callBack.setCancelUrl(lineConfig.callbankCancelUrl);
  75. sendBody.setRedirectUrls(callBack);
  76. return sendBody;
  77. }
  78. /**
  79. * set the header info
  80. * @param request
  81. * @throws URISyntaxException
  82. */
  83. private void setHeaderAndEntity(HttpPost request, String sendBody) throws URISyntaxException {
  84. String nonce = UUID.randomUUID().toString();
  85. request.setHeader("Content-Type", "application/json");
  86. request.setHeader("X-LINE-ChannelId", lineConfig.channelId);
  87. request.setHeader("X-LINE-Authorization-Nonce", nonce);
  88. StringBuffer message = new StringBuffer();
  89. //channelSecret
  90. message.append(lineConfig.channelSecret);
  91. //path
  92. message.append(request.getUri().getPath());
  93. //body
  94. message.append(sendBody);
  95. //nonce
  96. message.append(nonce);
  97. byte[] bytes = HmacUtils.getInitializedMac(HmacAlgorithms.HMAC_SHA_256, lineConfig.channelSecret.getBytes()).doFinal(message.toString().getBytes());
  98. byte[] byteArray = Base64.encodeBase64(bytes);
  99. request.setHeader("X-LINE-Authorization", new String(byteArray));
  100. request.setEntity(new StringEntity(sendBody, ContentType.APPLICATION_JSON));
  101. }
  102. /**
  103. * line pay
  104. * @param orderManageTotal
  105. */
  106. public LinePayAPOD pay(TOrderManageTotal orderManageTotal, String deptName) {
  107. //create request body
  108. SendBody sendBody = createSendBody(orderManageTotal, deptName);
  109. ObjectMapper mapper = new ObjectMapper();
  110. LinePayAPOD response = null;
  111. try (CloseableHttpClient client = HttpClients.createDefault()) {
  112. HttpPost request = new HttpPost(lineConfig.payUrl);
  113. setHeaderAndEntity(request, mapper.writeValueAsString(sendBody));
  114. response = client.execute(request, httpResponse ->
  115. mapper.readValue(EntityUtils.toString(httpResponse.getEntity()), LinePayAPOD.class));
  116. log.info(mapper.writeValueAsString(response));
  117. } catch (IOException | URISyntaxException e) {
  118. log.error("line pay Request API エラーが発生しました。", e);
  119. }
  120. return response;
  121. }
  122. /**
  123. * confirm line pay
  124. * @param transactionId
  125. * @param total
  126. */
  127. public LinePayConfirmAPOD payConfirm(String transactionId, long total) {
  128. //
  129. ConfirmBody body = new ConfirmBody();
  130. body.setAmount(total);
  131. ObjectMapper mapper = new ObjectMapper();
  132. MessageFormat form = new MessageFormat(lineConfig.confirmUrl);
  133. String formatStr = form.format(new Object[] {transactionId});
  134. LinePayConfirmAPOD response = null;
  135. try (CloseableHttpClient client = HttpClients.createDefault()) {
  136. HttpPost request = new HttpPost(formatStr);
  137. setHeaderAndEntity(request, mapper.writeValueAsString(body));
  138. response = client.execute(request, httpResponse ->
  139. mapper.readValue(EntityUtils.toString(httpResponse.getEntity()), LinePayConfirmAPOD.class));
  140. log.info(mapper.writeValueAsString(response));
  141. } catch (IOException | URISyntaxException e) {
  142. log.error("line pay Confirm API エラーが発生しました。", e);
  143. }
  144. return response;
  145. }
  146. /**
  147. * 決済完了(売上確定済み)された取引を返金します。
  148. * @param transactionId
  149. * @param refundAmount
  150. */
  151. public LinePayRefundAPOD payRefund(String transactionId, Long refundAmount) {
  152. //
  153. RefundBody body = new RefundBody();
  154. body.setRefundAmount(refundAmount);
  155. ObjectMapper mapper = new ObjectMapper();
  156. MessageFormat form = new MessageFormat(lineConfig.confirmUrl);
  157. String formatStr = form.format(new Object[] {transactionId});
  158. LinePayRefundAPOD response = null;
  159. try (CloseableHttpClient client = HttpClients.createDefault()) {
  160. HttpPost request = new HttpPost(formatStr);
  161. setHeaderAndEntity(request, mapper.writeValueAsString(body));
  162. response = client.execute(request, httpResponse ->
  163. mapper.readValue(EntityUtils.toString(httpResponse.getEntity()), LinePayRefundAPOD.class));
  164. log.info(mapper.writeValueAsString(response));
  165. } catch (IOException | URISyntaxException e) {
  166. log.error("line pay Refund API エラーが発生しました。", e);
  167. }
  168. return response;
  169. }
  170. }