applications.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { BaseService } from '@cool-midway/core';
  2. import { ILogger, Inject, Provide } from '@midwayjs/core';
  3. import { InjectEntityModel } from '@midwayjs/typeorm';
  4. import { PayeeEntity } from '../../../payment/entity/payee';
  5. import { Repository } from 'typeorm';
  6. import { CustomerEntity } from '../../../payment/entity/customer';
  7. import { PaymentService } from '../../../payment/service/payment';
  8. import { SunPayAdapter } from '../../../payment/adapter/sunpay.adapter';
  9. import { OpenPaymentAccountEntity } from '../../entity/open_payment_account';
  10. import { OpenAccountEntity } from '../../entity/open_account';
  11. import { EasyPayAdapter } from '../../../payment/adapter/easypay.adapter';
  12. import { OpenApplicationsEntity } from '../../entity/open_applications';
  13. import { Utils } from '../../../../comm/utils';
  14. import { OrderTypeEnum, StatusTypeEnum } from '../../entity/open_payment_order';
  15. import * as md5 from 'md5';
  16. /**
  17. * 开户管理
  18. */
  19. @Provide()
  20. export class applicationsService extends BaseService {
  21. @InjectEntityModel(PayeeEntity)
  22. payeeEntity: Repository<PayeeEntity>;
  23. @InjectEntityModel(CustomerEntity)
  24. customerEntity: Repository<CustomerEntity>;
  25. @Inject()
  26. paymentService: PaymentService;
  27. @Inject()
  28. sunPayAdapter: SunPayAdapter;
  29. @Inject()
  30. ctx;
  31. @Inject()
  32. logger: ILogger;
  33. @Inject()
  34. utils: Utils;
  35. @InjectEntityModel(OpenPaymentAccountEntity)
  36. openPaymentAccountEntity: Repository<OpenPaymentAccountEntity>;
  37. @InjectEntityModel(OpenAccountEntity)
  38. openAccountEntity: Repository<OpenAccountEntity>;
  39. @Inject()
  40. easyPayAdapter: EasyPayAdapter;
  41. @InjectEntityModel(OpenApplicationsEntity)
  42. openApplicationsEntity: Repository<OpenApplicationsEntity>;
  43. async addApplications(params, { fail, ok }) {
  44. const mchInFo = await this.openAccountEntity.findOne({
  45. where: {
  46. mch_id: params.mchId,
  47. },
  48. });
  49. console.log(58, mchInFo);
  50. const applicationsParams = {
  51. request_id: `fusionget_${new Date().getTime()}`,
  52. ...params,
  53. account_id: mchInFo.account_id,
  54. };
  55. const res = await this.easyPayAdapter.request(
  56. 'POST',
  57. '/v3/applications',
  58. applicationsParams
  59. );
  60. console.log(71, res);
  61. if (res.hasOwnProperty('errors') && res.errors.length > 0) {
  62. return fail(res.errors);
  63. }
  64. // 存入数据库
  65. await this.openApplicationsEntity.insert({
  66. mch_id: params.mch_id, //商户编号
  67. account_id: mchInFo.account_id,
  68. request_id: applicationsParams.request_id,
  69. purpose: params.purpose, // 开户目的
  70. currency: params.currency, // 币种
  71. payment_type: params.payment_type, // 支付方式
  72. status: res.data.status, // 申请状态
  73. source: 'EASYPAY', // 来源
  74. application_id: res.data.id,
  75. });
  76. return ok('添加成功');
  77. // 创建商户
  78. }
  79. async getApplicationsList(params) {
  80. const res = await this.easyPayAdapter.request(
  81. 'GET',
  82. '/v3/applications',
  83. params
  84. );
  85. return res;
  86. }
  87. /*
  88. 查询账户余额信息
  89. */
  90. async getApplicationsListByMchId() {
  91. const merchantInfo = await this.getMerchantInfo()
  92. const bank_accounts_res = await this.easyPayAdapter.request(
  93. 'GET',
  94. `/v1/bank_accounts`,
  95. {
  96. page: 1,
  97. size: 10,
  98. account_id: merchantInfo.account_id,
  99. }
  100. );
  101. const balances_res = await this.easyPayAdapter.request(
  102. 'GET',
  103. `/v3/accounts/${merchantInfo.account_id}/balances`
  104. );
  105. bank_accounts_res.data = bank_accounts_res.data.map(elm => {
  106. let amount = 0;
  107. if (balances_res.data.length > 0) {
  108. balances_res.data.forEach(item => {
  109. if (!amount && item.currency === elm.currency) {
  110. amount = item.amount;
  111. }
  112. });
  113. }
  114. return {
  115. ...elm,
  116. amount,
  117. };
  118. });
  119. return bank_accounts_res;
  120. }
  121. async getTransactionsListByMchId(params) {
  122. const merchantInfo = await this.getMerchantInfo()
  123. const res = await this.easyPayAdapter.request(
  124. 'GET',
  125. `/v3/accounts/${merchantInfo.account_id}/transactions`,
  126. params
  127. );
  128. res.data = res.data.map(elm => {
  129. return {
  130. ...elm,
  131. create_time: this.utils.formatNewTime(elm.create_time),
  132. order_type: OrderTypeEnum[elm.order_type] || elm.order_type,
  133. status: StatusTypeEnum[elm.status] || elm.status,
  134. };
  135. });
  136. return res;
  137. }
  138. // 获取收款账户
  139. async getBankAccountsBy(params) {
  140. const merchantInfo = await this.getMerchantInfo()
  141. const res = await this.easyPayAdapter.request('GET', `/v1/bank_accounts`, {
  142. page: 1,
  143. size: 10,
  144. account_id: merchantInfo.account_id,
  145. });
  146. const bankAccountsIndex = res.data.findIndex(
  147. elm => elm.currency === params.currency
  148. );
  149. return res.data[bankAccountsIndex];
  150. }
  151. // 查询汇率
  152. async getExchangeCurrency(params) {
  153. const res = await this.easyPayAdapter.request('GET', `/v1/exchange_rates`, params);
  154. return res
  155. }
  156. // 换汇
  157. async exchanges(params) {
  158. const merchantInfo = await this.getMerchantInfo()
  159. const res = await this.easyPayAdapter.request('POST', `/v1/exchanges`, {
  160. account_id: merchantInfo.account_id,
  161. ...params,
  162. buy_amount:params.buy_amount * 100,
  163. });
  164. // 费率拦截
  165. return res
  166. }
  167. // 转账
  168. async transfer(params) {
  169. console.log(186, params);
  170. const to_merchantInfo = await this.getMerchantInfo(params.to_mch_id);
  171. const merchantInfo = await this.getMerchantInfo();
  172. const resParams = {
  173. request_id: md5(`transfer_${merchantInfo.account_id}_${new Date().getTime()}`),
  174. from_account_id: merchantInfo.account_id,
  175. to_account_id: to_merchantInfo.account_id,
  176. currency: params.currency,
  177. amount: params.amount*100,
  178. purpose: params.purpose
  179. }
  180. const res = await this.easyPayAdapter.request('POST', `/v1/transfers`, resParams);
  181. console.log(202, resParams);
  182. console.log(203, res);
  183. return res
  184. }
  185. // 获取商户信息
  186. async getMerchantInfo(mch_id= 'ep001@fusion.com') {
  187. const merchantInfo = await this.openAccountEntity.findOne({
  188. where: {
  189. mch_id: this.ctx.admin.merchant.mchId,
  190. // mch_id: 'easypay@qq.com',
  191. // mch_id: 'ep001@fusion.com',
  192. // mch_id: 'easypay003@fusion.com',
  193. // mch_id
  194. },
  195. });
  196. return merchantInfo
  197. }
  198. }