easypay.adapter.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { Provide, Inject, Config, ALL } from '@midwayjs/decorator';
  2. import { Repository } from 'typeorm';
  3. import { InjectEntityModel } from '@midwayjs/typeorm';
  4. import { PaymentChannelEntity } from '../entity/channel';
  5. import axios from 'axios';
  6. import { CustomerEntity } from '../entity/customer';
  7. import qs = require('qs');
  8. import { ILogger } from '@midwayjs/core';
  9. class TokenManager {
  10. authUrl = ''; // 认证URL
  11. clientId = ''; // 客户端ID
  12. clientSecret = ''; // 客户端密钥
  13. refreshToken = null; // 刷新令牌
  14. accessToken = null; // 访问令牌
  15. expiresIn = 0; // 令牌过期时间
  16. constructor(authUrl, clientId, clientSecret) {
  17. this.authUrl = authUrl; // 初始化认证URL
  18. this.clientId = clientId; // 初始化客户端ID
  19. this.clientSecret = clientSecret; // 初始化客户端密钥
  20. this.accessToken = null; // 初始化访问令牌为空
  21. this.refreshToken = null; // 初始化刷新令牌为空
  22. this.expiresIn = 0; // 初始化过期时间为0
  23. }
  24. // 登录方法,用于获取访问令牌和刷新令牌
  25. async login() {
  26. try {
  27. const dataString = qs.stringify({
  28. client_id: this.clientId, // 传递客户端ID
  29. client_secret: this.clientSecret, // 传递客户端密钥
  30. grant_type: 'client_credentials', // 假设使用密码授权类型进行初始登录
  31. });
  32. const config = {
  33. method: 'post',
  34. url: this.authUrl,
  35. data: dataString,
  36. };
  37. const response = await axios(config);
  38. const data = response.data;
  39. this.accessToken = data.access_token; // 获取访问令牌
  40. this.refreshToken = data.refresh_token; // 获取刷新令牌
  41. this.expiresIn = Math.floor(Date.now() / 1000) + data.expires_in; // 计算令牌过期时间
  42. console.log('Login successful. Access Token acquired.'); // 登录成功日志
  43. } catch (error) {
  44. console.error('Login failed:', error); // 登录失败日志
  45. throw error; // 抛出错误
  46. }
  47. }
  48. // 获取访问令牌的方法
  49. async getAccessToken() {
  50. const currentTime = Math.floor(Date.now() / 1000); // 获取当前时间戳
  51. if (this.accessToken && this.expiresIn > currentTime) {
  52. return this.accessToken; // 如果令牌有效,返回访问令牌
  53. }
  54. if (!this.refreshToken) {
  55. throw new Error('No refresh token available. Please login first.'); // 如果没有刷新令牌,抛出错误
  56. }
  57. try {
  58. const response = await axios.post(this.authUrl, {
  59. client_id: this.clientId, // 传递客户端ID
  60. client_secret: this.clientSecret, // 传递客户端密钥
  61. refresh_token: this.refreshToken, // 传递刷新令牌
  62. grant_type: 'refresh_token', // 使用刷新令牌授权类型
  63. });
  64. const data = response.data;
  65. this.accessToken = data.access_token; // 更新访问令牌
  66. this.expiresIn = currentTime + data.expires_in; // 更新过期时间
  67. return this.accessToken; // 返回新的访问令牌
  68. } catch (error) {
  69. console.error('Error fetching access token:', error); // 获取令牌错误日志
  70. throw error; // 抛出错误
  71. }
  72. }
  73. }
  74. @Provide()
  75. export class EasyPayAdapter {
  76. @InjectEntityModel(PaymentChannelEntity)
  77. channelEntity: Repository<PaymentChannelEntity>;
  78. @InjectEntityModel(CustomerEntity)
  79. customerEntity: Repository<CustomerEntity>;
  80. @Config(ALL)
  81. globalConfig;
  82. @Inject()
  83. ctx;
  84. @Inject()
  85. logger: ILogger;
  86. private config: {
  87. apiKey: string;
  88. apiSecret: string;
  89. apiUrl: string;
  90. chainApiKey: string;
  91. chainApiSecret: string;
  92. chainApiUrl: string;
  93. tokenManager: TokenManager;
  94. };
  95. baseInfo: {
  96. account_id?: string
  97. }
  98. Init() {
  99. this.initConfig()
  100. }
  101. /**
  102. * 初始化渠道配置
  103. */
  104. private async initConfig() {
  105. if (!this.config) {
  106. const channel = await this.channelEntity.findOne({
  107. where: { code: 'EASYPAY', isEnabled: true },
  108. });
  109. if (!channel) {
  110. throw new Error('FusionPay channel not found or disabled');
  111. }
  112. const tokenManager = new TokenManager(
  113. `${channel.apiUrl}/auth`,
  114. channel.apiKey,
  115. channel.apiSecret
  116. );
  117. await tokenManager.login();
  118. this.config = {
  119. apiKey: channel.apiKey,
  120. apiSecret: channel.apiSecret,
  121. apiUrl: channel.apiUrl,
  122. chainApiKey: channel.chainApiKey,
  123. chainApiSecret: channel.chainApiSecret,
  124. chainApiUrl: channel.chainApiUrl,
  125. tokenManager: tokenManager,
  126. };
  127. this.baseInfo = channel.config
  128. }
  129. }
  130. /**
  131. * 发送请求到 EasyPay API
  132. */
  133. async request(method: string, endpoint: string, data?: any) {
  134. await this.initConfig();
  135. // return Promise.resolve(`https://api.easypayx.com${endpoint.replace('/api/open', '')}`);
  136. let url = this.config.apiUrl;
  137. try {
  138. url = `${url}${endpoint.replace('/api/open', '')}`;
  139. console.log(158, data)
  140. const accessToken = await this.config.tokenManager.getAccessToken();
  141. const axiosParams = {
  142. method,
  143. url,
  144. data: method !== 'GET' ? data : undefined,
  145. params: method === 'GET' ? data : undefined,
  146. headers: {
  147. 'Content-Type': 'application/json',
  148. Authorization: `Bearer ${accessToken}`,
  149. },
  150. };
  151. this.logger.info('向easyPay发送请求', axiosParams);
  152. const response = await axios(axiosParams);
  153. // 检查响应
  154. // if (response.data.code !== 200) {
  155. // console.log(response.data);
  156. // throw new Error(`FusionPay API ${response.data.msg}`);
  157. // }
  158. // console.log('response', response.data);
  159. return response.data;
  160. } catch (error) {
  161. // console.log(error.response.data);
  162. if (axios.isAxiosError(error) && error.response) {
  163. // console.log(error.response.data);
  164. // throw new Error(`FusionPay API Network ${error.response.data.msg}`);
  165. this.ctx.status = error.response.status; // 服务器错误
  166. // this.ctx.body = error.response.data;
  167. // return this.res.status(500).json({});
  168. this.logger.info('向easyPay发送请求失败了', error.response);
  169. return error.response.data;
  170. // throw this.ctx.body;
  171. }
  172. throw error;
  173. }
  174. }
  175. // 获取签名
  176. private async getAuthorization() {
  177. await this.initConfig();
  178. const res = await axios.post(`${this.config.apiUrl}/auth`, {
  179. client_id: 'client_id',
  180. client_secret: this.config.apiSecret,
  181. grant_type: 'client_credentials',
  182. });
  183. return '';
  184. }
  185. }