applications.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 {
  15. OrderType,
  16. OrderTypeEnum,
  17. StatusTypeEnum,
  18. } from '../../entity/open_payment_order';
  19. import * as md5 from 'md5';
  20. import { PayeeAddressEntity } from '../../../payment/entity/payee_address';
  21. import { EasyOpenService } from '../open';
  22. /**
  23. * 开户管理
  24. */
  25. @Provide()
  26. export class applicationsService extends BaseService {
  27. @InjectEntityModel(PayeeEntity)
  28. payeeEntity: Repository<PayeeEntity>;
  29. @InjectEntityModel(CustomerEntity)
  30. customerEntity: Repository<CustomerEntity>;
  31. @Inject()
  32. paymentService: PaymentService;
  33. @InjectEntityModel(PayeeAddressEntity)
  34. payeeAddressEntity: Repository<PayeeAddressEntity>;
  35. @Inject()
  36. sunPayAdapter: SunPayAdapter;
  37. @Inject()
  38. ctx;
  39. @Inject()
  40. logger: ILogger;
  41. @Inject()
  42. utils: Utils;
  43. @InjectEntityModel(OpenPaymentAccountEntity)
  44. openPaymentAccountEntity: Repository<OpenPaymentAccountEntity>;
  45. @InjectEntityModel(OpenAccountEntity)
  46. openAccountEntity: Repository<OpenAccountEntity>;
  47. @Inject()
  48. easyPayAdapter: EasyPayAdapter;
  49. @Inject()
  50. easyOpenService: EasyOpenService;
  51. @InjectEntityModel(OpenApplicationsEntity)
  52. openApplicationsEntity: Repository<OpenApplicationsEntity>;
  53. async addApplications(params, { fail, ok }) {
  54. const mchInFo = await this.openAccountEntity.findOne({
  55. where: {
  56. mch_id: params.mch_id,
  57. },
  58. });
  59. const applicationsParams = {
  60. request_id: `fusionget_${new Date().getTime()}`,
  61. ...params,
  62. account_id: mchInFo.account_id,
  63. };
  64. const res = await this.easyPayAdapter.request(
  65. 'POST',
  66. '/v3/applications',
  67. applicationsParams
  68. );
  69. if (res.hasOwnProperty('errors') && res.errors.length > 0) {
  70. return fail(res.errors);
  71. }
  72. // 存入数据库
  73. await this.openApplicationsEntity.insert({
  74. mch_id: params.mch_id, //商户编号
  75. account_id: mchInFo.account_id,
  76. request_id: applicationsParams.request_id,
  77. purpose: params.purpose, // 开户目的
  78. currency: params.currency, // 币种
  79. payment_type: params.payment_type, // 支付方式
  80. status: res.data.status, // 申请状态
  81. source: 'EASYPAY', // 来源
  82. application_id: res.data.id,
  83. });
  84. return ok('添加成功');
  85. // 创建商户
  86. }
  87. async getApplicationsList(params) {
  88. const res = await this.easyPayAdapter.request(
  89. 'GET',
  90. '/v3/applications',
  91. params
  92. );
  93. console.log(106, params);
  94. return res;
  95. }
  96. /*
  97. 查询账户余额信息
  98. */
  99. async getApplicationsListByMchId() {
  100. const merchantInfo = await this.getMerchantInfo();
  101. const bank_accounts_res = await this.easyPayAdapter.request(
  102. 'GET',
  103. `/v1/bank_accounts`,
  104. {
  105. page: 1,
  106. size: 10,
  107. account_id: merchantInfo.account_id,
  108. }
  109. );
  110. const balances_res = await this.easyPayAdapter.request(
  111. 'GET',
  112. `/v3/accounts/${merchantInfo.account_id}/balances`
  113. );
  114. bank_accounts_res.data = bank_accounts_res.data.map(elm => {
  115. let amount = 0;
  116. if (balances_res.data.length > 0) {
  117. balances_res.data.forEach(item => {
  118. if (!amount && item.currency === elm.currency) {
  119. amount = item.amount;
  120. }
  121. });
  122. }
  123. return {
  124. ...elm,
  125. amount,
  126. };
  127. });
  128. return bank_accounts_res;
  129. }
  130. async getTransactionsListByMchId(params) {
  131. const merchantInfo = await this.getMerchantInfo();
  132. const res = await this.easyPayAdapter.request(
  133. 'GET',
  134. `/v3/accounts/${merchantInfo.account_id}/transactions`,
  135. params
  136. );
  137. res.data = res.data.map(elm => {
  138. return {
  139. ...elm,
  140. create_time: this.utils.formatNewTime(elm.create_time),
  141. order_type: OrderTypeEnum[elm.order_type] || elm.order_type,
  142. status: StatusTypeEnum[elm.status] || elm.status,
  143. };
  144. });
  145. return res;
  146. }
  147. // 获取收款账户
  148. async getBankAccountsBy(params) {
  149. const merchantInfo = await this.getMerchantInfo();
  150. const res = await this.easyPayAdapter.request('GET', `/v1/bank_accounts`, {
  151. page: 1,
  152. size: 10,
  153. account_id: merchantInfo.account_id,
  154. });
  155. const bankAccountsIndex = res.data.findIndex(
  156. elm => elm.currency === params.currency
  157. );
  158. return res.data[bankAccountsIndex];
  159. }
  160. // 查询汇率
  161. async getExchangeCurrency(params) {
  162. const res = await this.easyPayAdapter.request(
  163. 'GET',
  164. `/v1/exchange_rates`,
  165. params
  166. );
  167. return res;
  168. }
  169. // 换汇
  170. async exchanges(params) {
  171. const merchantInfo = await this.getMerchantInfo();
  172. const res = await this.easyPayAdapter.request('POST', `/v1/exchanges`, {
  173. account_id: merchantInfo.account_id,
  174. ...params,
  175. buy_amount: params.buy_amount * 100,
  176. });
  177. // 费率拦截
  178. return res;
  179. }
  180. // 转账
  181. async transfer(params) {
  182. console.log(186, params);
  183. const to_merchantInfo = await this.getMerchantInfo(params.to_mch_id);
  184. const merchantInfo = await this.getMerchantInfo();
  185. const resParams = {
  186. request_id: md5(
  187. `transfer_${merchantInfo.account_id}_${new Date().getTime()}`
  188. ),
  189. from_account_id: merchantInfo.account_id,
  190. to_account_id: to_merchantInfo.account_id,
  191. currency: params.currency,
  192. amount: params.amount * 100,
  193. purpose: params.purpose,
  194. };
  195. const res = await this.easyPayAdapter.request(
  196. 'POST',
  197. `/v1/transfers`,
  198. resParams
  199. );
  200. console.log(202, resParams);
  201. console.log(203, res);
  202. return res;
  203. }
  204. // 付款
  205. async payments(params) {
  206. // 余额 判断是否足以支持交易手续费
  207. // 获取指定币种的余额
  208. /*
  209. amount : 12312
  210. beneficiary_id : 10
  211. comment : "123"
  212. currency : "AUD"
  213. purpose : "123123"
  214. */
  215. const merchantInfo = await this.getMerchantInfo();
  216. // 获取收款地址
  217. const mchPayeeAddress = await this.payeeAddressEntity.findOne({
  218. where: {
  219. id: params.beneficiary_id,
  220. },
  221. });
  222. const paymentsParams = {
  223. request_id: md5(
  224. `payments_${merchantInfo.account_id}_${new Date().getTime()}`
  225. ),
  226. account_id: merchantInfo.account_id,
  227. currency: params.currency,
  228. amount: params.amount * 100,
  229. purpose: params.purpose,
  230. comment: params.comment,
  231. beneficiary: {
  232. legal_entity_type: 'COMPANY',
  233. payment_type: mchPayeeAddress.payment_type, // 支付方式
  234. region: mchPayeeAddress.region, // 地区
  235. currency: params.currency,
  236. bank_name: mchPayeeAddress.bank_name,
  237. account_holder_name: mchPayeeAddress.account_holder_name, // 银行账户持有人
  238. account_number: mchPayeeAddress.account_number, // 银行账号/IBAN
  239. bic_number: mchPayeeAddress.bank_routing_code_key, // 路由号/SWIFT
  240. address: mchPayeeAddress.address, // 地址
  241. },
  242. };
  243. console.log(241, paymentsParams);
  244. const res = await this.easyPayAdapter.request(
  245. 'POST',
  246. `/v1/payments`,
  247. paymentsParams
  248. );
  249. // 付款拦截利润 改为订单交易成功之后收取
  250. // this.easyOpenService.save_user_order(res, params, OrderType.PAYMENT);
  251. console.log(243, res);
  252. return res;
  253. }
  254. // 获取商户信息
  255. async getMerchantInfo(mch_id = 'ep001@fusion.com') {
  256. const merchantInfo = await this.openAccountEntity.findOne({
  257. where: {
  258. mch_id: this.ctx.admin.merchant.mchId,
  259. // mch_id: 'easypay@qq.com',
  260. // mch_id: 'ep001@fusion.com',
  261. // mch_id: 'easypay003@fusion.com',
  262. // mch_id
  263. },
  264. });
  265. return merchantInfo;
  266. }
  267. // amount 单位为分
  268. async getAccountsBalancesByCurrency(account_id, currency) {
  269. const balances = await this.easyPayAdapter.request(
  270. 'GET',
  271. `/v3/accounts/${account_id}/balances`,
  272. {}
  273. );
  274. let amount = 0;
  275. balances.data.forEach(elm => {
  276. if(elm.currency === currency && !amount ) {
  277. amount = elm.amount
  278. }
  279. })
  280. return amount
  281. }
  282. }