import { CustomerEntity } from './../entity/customer'; import { SunPayAdapter } from './../adapter/sunpay.adapter'; import { IndividualEntity } from './../entity/individual'; import { ALL, Config, Init, Inject, Provide } from '@midwayjs/decorator'; import { BaseService } from '@cool-midway/core'; import { InjectEntityModel } from '@midwayjs/typeorm'; import { Repository } from 'typeorm'; import { PaymentService } from './payment'; import {MerchantEntity} from "../entity/merchant"; @Provide() export class IndividualService extends BaseService { @InjectEntityModel(IndividualEntity) individualEntity: Repository; @Inject() paymentService: PaymentService; @Inject() ctx; @InjectEntityModel(CustomerEntity) customerEntity: Repository; @InjectEntityModel(MerchantEntity) merchantEntity: Repository; @Config(ALL) config; @Init() async init() { await super.init(); this.setEntity(this.individualEntity); } private _isOpenApi: boolean = false /** * 添加或者更新当前商户的 individual 客户信息 * @param param * @returns */ async add(param) { let merchantId = await this.getMerchantId(param); const custom = { ...param, out_user_id: merchantId, webhook_url: this.getWebhook_url(param), customer_type: 'INDIVIDUAL', }; let res = await this.paymentService .setChannel('SUNPAY') .setCustomerInfo(custom); param.merchantId = merchantId await super.add({ ...param, ...param.company, customer_id: res.data.customer_id, }); return res; } async update(param) { let merchantId = this.getMerchantId(param); const custom = { ...param, out_user_id: merchantId, webhook_url: this.getWebhook_url(param), customer_type: 'INDIVIDUAL', }; let res = await this.paymentService .setChannel('SUNPAY') .setCustomerInfo(custom); await super.update(param); return res; } setIsOpenApi(payload = false) { this._isOpenApi = payload } async page(params: any) { const merchantId = await this.getMerchantId(params); const query = `SELECT * FROM individual WHERE merchantId = '${merchantId}' ORDER BY merchantId ASC`; return this.sqlRenderPage(query, params, false); } async getMerchantId(params) { let merchantId; if (this._isOpenApi) { merchantId = params.out_user_id; } else if (params?.mch_id || params?.mchId) { // const merchantInfo = await this.merchantEntity.findOneBy({ // mchId: params?.mch_id // }) merchantId = params?.mch_id || params?.mchId } else { const { merchant } = this.ctx.admin; merchantId = merchant.mchId; } return merchantId } getWebhook_url(params) { if (this._isOpenApi) { return params.webhook_url } return this.config.callback.sunpay } }