Skip to content

Commit

Permalink
feat: 新增服务商发票部分接口
Browse files Browse the repository at this point in the history
1. 获取用户填写的抬头
2. 开具电子发票
3. 查询电子发票
4. 冲红电子发票
  • Loading branch information
jay committed Aug 15, 2024
1 parent 5173644 commit 47a5e76
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/types/wepay-partner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequireOnlyOne } from '.';
import { DevelopmentConfigRequest, FapiaoNotifyResult, RefundNotifyResult, RefundParameters, Trade, TransactionOrder } from './wepay';
import { DevelopmentConfigRequest, FapiaoNotifyResult, IssueFapiaoRequest, RefundNotifyResult, RefundParameters, ReverseFapiaoRequest, Trade, TransactionOrder } from './wepay';

/**
* 微信支付服务务下单数据结构
Expand Down Expand Up @@ -108,4 +108,12 @@ export interface FapiaoNotifyResultOfPartner extends FapiaoNotifyResult {
sub_mchid: string;
}

export interface IssueFapiaoRequestOfPartner extends IssueFapiaoRequest {
sub_mchid: string;
}

export interface ReverseFapiaoRequestOfPartner extends ReverseFapiaoRequest {
sub_mchid: string;
}

/** 电子发票 **/
70 changes: 69 additions & 1 deletion lib/wepay.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { createNonceStr } from './utils';

import type { Request, Response } from 'express';
import { XMLBuilder } from 'fast-xml-parser';
import { DevelopmentConfigRequestOfPartner, FapiaoNotifyResultOfPartner, RefundNotifyResultOfPartner, RefundParametersOfPartner, TradeOfPartner, TransactionOrderOfPartner } from './types/wepay-partner';
import { DevelopmentConfigRequestOfPartner, FapiaoNotifyResultOfPartner, IssueFapiaoRequestOfPartner, RefundNotifyResultOfPartner, RefundParametersOfPartner, ReverseFapiaoRequestOfPartner, TradeOfPartner, TransactionOrderOfPartner } from './types/wepay-partner';
@Injectable()
export class WePayService {

Expand Down Expand Up @@ -504,6 +504,21 @@ export class WePayService {
});
}

/**
* 服务商获取用户填写的抬头
*
* @link https://pay.weixin.qq.com/docs/partner/apis/fapiao/user-title/get-user-title.html
*/
async getUserTitleOfPartner (params: GetUserTitleParams, spMchId: string, subMchId: string, serialNo: string, privateKey: Buffer | string) {
const url = `/v3/new-tax-control-fapiao/user-title?sub_mchid=${subMchId}&fapiao_apply_id=${params.fapiao_apply_id}&scene=${params.scene}`;
const nonceStr = createNonceStr();
const timestamp = Math.floor(Date.now() / 1000);
const signature = this.generateSignature('GET', url, timestamp, nonceStr, privateKey);
return axios.get<UserTitleEntity>(this.API_ROOT + url, {
headers: this.generateHeader(spMchId, nonceStr, timestamp, serialNo, signature),
});
}

/**
* 开具电子发票
*
Expand All @@ -523,6 +538,25 @@ export class WePayService {
});
}

/**
* 服务商开具电子发票
*
* @link https://pay.weixin.qq.com/docs/partner/apis/fapiao/fapiao-applications/issue-fapiao-applications.html
*/
async issueFapiaoOfPartner (data: IssueFapiaoRequestOfPartner, spMchId: string, serialNo: string, privateKey: Buffer | string, platformSerial: string) {
const url = '/v3/new-tax-control-fapiao/fapiao-applications';
const nonceStr = createNonceStr();
const timestamp = Math.floor(Date.now() / 1000);
const signature = this.generateSignature('POST', url, timestamp, nonceStr, privateKey, data);
const headers = {
...this.generateHeader(spMchId, nonceStr, timestamp, serialNo, signature),
'Wechatpay-Serial': platformSerial,
};
return axios.post<void>(this.API_ROOT + url, data, {
headers,
});
}

/**
* 查询电子发票
*
Expand All @@ -540,6 +574,25 @@ export class WePayService {
headers: this.generateHeader(mchId, nonceStr, timestamp, serialNo, signature),
});
}

/**
* 服务商查询电子发票
*
* @link https://pay.weixin.qq.com/docs/partner/apis/fapiao/fapiao-applications/get-fapiao-applications.html
*/
async getIssueFapiaoOfPartner (fapiaoApplyId: string, fapiaoId: string | null | undefined, spMchId: string, subMchid: string, serialNo: string, privateKey: Buffer | string) {
let url = `/v3/new-tax-control-fapiao/fapiao-applications/${fapiaoApplyId}?sub_mchid=${subMchid}`;
if (fapiaoId) {
url += `fapiao_id=${fapiaoId}`;
}
const nonceStr = createNonceStr();
const timestamp = Math.floor(Date.now() / 1000);
const signature = this.generateSignature('GET', url, timestamp, nonceStr, privateKey);
return axios.get<GetIssueFapiaoResponse>(this.API_ROOT + url, {
headers: this.generateHeader(spMchId, nonceStr, timestamp, serialNo, signature),
});
}

/**
* 冲红电子发票
*
Expand All @@ -555,6 +608,21 @@ export class WePayService {
});
}

/**
* 服务商冲红电子发票
*
* @link https://pay.weixin.qq.com/docs/partner/apis/fapiao/fapiao-applications/reverse-fapiao-applications.html
*/
async reverseFapiaoOfPartner (fapiaoApplyId: string, data: ReverseFapiaoRequestOfPartner, spMchId: string, serialNo: string, privateKey: Buffer | string) {
const url = `/v3/new-tax-control-fapiao/fapiao-applications/${fapiaoApplyId}/reverse`;
const nonceStr = createNonceStr();
const timestamp = Math.floor(Date.now() / 1000);
const signature = this.generateSignature('POST', url, timestamp, nonceStr, privateKey, data);
return axios.post<void>(this.API_ROOT + url, data, {
headers: this.generateHeader(spMchId, nonceStr, timestamp, serialNo, signature),
});
}

/** 电子发票 **/

/**
Expand Down

0 comments on commit 47a5e76

Please sign in to comment.