JSAPI 支付
提示
- 本文档均以提前注册的子商户用法为例。
- 需要开通 JSAPI 支付,登录特约商户的商户平台,在授权产品哪里查看是否已授权即可,如下所示:
下单
方法定义:
/**
* 统一下单接口
* @description 该方法对接[官方文档](https://pay.weixin.qq.com/docs/partner/apis/partner-jsapi-payment/partner-jsons/partner-jsapi-prepay.html)
* @param params JSAPI支付下单参数
* @param subMerchantInfo 特约商户应用配置
* @returns 统一下单结果
*/
async placeOrder(
params: WechatProviderPayPlaceOrderJSAPIParams,
subMerchantInfo?: WechatProviderSubMerchantInfo
): Promise<WechatProviderPayPlaceOrderJSAPIResponse> {}
使用示例:
const { success, data, error } = await this.wechatProviderPayJSAPIService.placeOrder({
out_trade_no: order.tradeNo,
description: 'xxx',
notify_url: this.payConfig.wechatProvider.notifyUrlPrefix + '/wechat/pay/provider',
amount: {
total: order.price
},
payer: {
sub_openid: userInfo.openId // 子应用的openid
}
});
返回的data
类型定义如下:
interface PlaceOrderJSAPIResponseData {
/** 服务商公众号AppId */
appId: string;
/** 特约商户公众号AppId */
subAppId?: string;
/**
* 时间戳
* @description 从1970年1月1日00:00:00至今的秒数,即当前的时间
*/
timeStamp: string;
/** 随机字符串,不长于32位 */
nonceStr: string;
/**
* 统一下单接口返回的 prepay_id 参数值
* @description 提交格式如:prepay_id=wx2017033010242291fcfe0db70013231072
*/
package: string;
/** 签名类型,因为本包采用v3版接口,故目前只接受RSA类型 */
signType: WechatProviderPaySignType;
/** 支付签名信息 */
paySign: string;
}
用里面的timeStamp
、nonceStr
、nonceStr
、package
、signType
和paySign
字段在微信公众号网页内调起微信支付即可!
提示
在微信公众号网页中调起支付时,要把公众号网页调起微信支付的域名路径配置好, 如下所示:
展开查看传入参数params
类型定义
/** 单品列表类型 */
interface DetailGoodDetail {
/**
* 商户侧商品编码
* @description 由半角的大小写字母、 数字、中划线、下划线中的一种或几种组成。
* @exports 商品编码
*/
merchant_goods_id: string;
/**
* 商品数量
* @description 用户购买的数量
* @example 1
*/
quantity: number;
/**
* 商品单价
* @description 商品单价,单位为分
* @example 828800
*/
unit_price: number;
/**
* 微信侧商品编码
* @description 微信支付定义的统一商品编号(没有可不传)
* @example 1001
*/
wechatpay_goods_id?: string;
/**
* 商品名称
* @description 商品的实际名称
* @example iPhoneX 256G
*/
goods_name?: string;
}
/** 优惠功能类型 */
interface Detail {
/**
* 订单原价
* @description 1、商户侧一张小票订单可能被分多次支付,订单原价用于记录整张小票的交易金额。
* @description 2、当订单原价与支付金额不相等,则不享受优惠。
* @description 3、该字段主要用于防止同一张小票分多次支付,以享受多次优惠的情况,正常支付订单不必上传此参数。
* @example 608800
*/
cost_price?: number;
/**
* 商家小票ID
* @example 微信123
*/
invoice_id?: number;
/**
* 单品列表
* @description 条目个数限制:1-6000
*/
goods_detail?: DetailGoodDetail[];
}
/** 商户门店信息类型 */
interface SceneInfoStoreInfo {
/**
* 门店编号
* @description 商户侧门店编号
* @example 0001
*/
id: string;
/**
* 门店名称
* @description 商户侧门店名称
* @example 腾讯大厦分店
*/
name?: string;
/**
* 地区编码
* @see https://pay.weixin.qq.com/wiki/doc/apiv3/terms_definition/chapter1_1_3.shtml#part-5
* @example 440305
*/
area_code?: string;
/**
* 详细地址
* @description 详细的商户门店地址
* @example 广东省深圳市南山区科技中一道10000号
*/
address?: string;
}
/** 支付场景描述 */
interface SceneInfo {
/**
* 用户终端IP
* @description 用户的客户端IP,支持IPv4和IPv6两种格式的IP地址。
* @example 14.23.150.211
*/
payer_client_ip: string;
/**
* 商户端设备号
* @description 商户端设备号(门店号或收银设备ID)
* @example 013467007045764
*/
device_id?: string;
/** 商户门店信息 */
store_info?: SceneInfoStoreInfo;
}
/**
* JSAPI 统一下单请求参数(微信服务商支付)
* @author 张冰 at 2023-12-8
*/
export interface WechatProviderPayPlaceOrderJSAPIParams {
/**
* 商品描述
* @example Image形象店-深圳腾大-QQ公仔
*/
description: string;
/**
* 商户订单号
* @description 商户系统内部订单号,只能是数字、大小写字母_-*且在同一个商户号下唯一
* @example 1217752501201407033233368018
*/
out_trade_no: string;
/**
* 通知地址
* @description 通知URL必须为直接可访问的URL,不允许携带查询串,要求必须为https地址。
* @exports https://www.weixin.qq.com/wxpay/pay.php
*/
notify_url: string;
/** 订单金额信息 */
amount: {
/**
* 总金额
* @description 订单总金额,单位为分。
* @example 100
*/
total: number;
/**
* 货币类型
* @description 境内商户号仅支持人民币。
* @example CNY
*/
currency?: string | 'CNY';
};
/**
* 支付者
* @description 支付者信息
*/
payer: {
/**
* 用户在服务商appid下的唯一标识
* @description 下单前需获取到用户的Openid
* @example oUpF8uMuAJO_M2pxb1Q9zNjWeS6o
*/
sp_openid?: string;
/**
* 用户在子商户appid下的唯一标识
* @description 若传`sub_openid`,那`sub_appid`必填。下单前需获取到用户的Openid
* @example oUpF8uMuAJO_M2pxb1Q9zNjWeS6o
*/
sub_openid?: string;
};
/**
* 交易结束时间
* @description 该字段传入的是JS的时间,最终发送给微信支付服务器会自动转换为[rfc3339](https://datatracker.ietf.org/doc/html/rfc3339)时间格式,开发者无需手动转换
*/
time_expire?: Date;
/**
* 附加数据
* @description 附加数据,在查询API和支付通知中原样返回,可作为自定义参数使用
*/
attach?: string;
/**
* 订单优惠标记
* @example WXG
*/
goods_tag?: string;
/** 优惠功能 */
detail?: Detail;
/**
* 场景信息
* @description 支付场景描述
*/
scene_info?: SceneInfo;
/** 结算信息 */
settle_info?: {
/**
* 是否指定分账
* @example false
*/
profit_sharing?: boolean;
};
}