* uni-pay-co 统一支付云对象
*/
const service = require('./service');
const { UniCloudError, isUniPayError, ERROR } = require('./common/error');
const middleware = require('./middleware/index');
const uniIdCommon = require('uni-id-common');
module.exports = {
* 中间件(请求前执行)
*/
async _before() {
const params = this.getParams();
let clientInfo;
if (params && params[0] && params[0].clientInfo) {
clientInfo = params[0].clientInfo;
} else {
clientInfo = this.getClientInfo();
}
this.uniIdCommon = uniIdCommon.createInstance({
clientInfo
});
const i18n = uniCloud.initI18n({
locale: clientInfo.locale || 'zh-Hans',
fallbackLocale: 'zh-Hans',
messages: require('./lang/index')
})
this.t = i18n.t.bind(i18n);
this.middleware = {}
for (const mwName in middleware) {
this.middleware[mwName] = middleware[mwName].bind(this);
}
const methodName = this.getMethodName();
if (methodName !== "payNotify") {
await this.middleware.auth(false);
await this.middleware.accessControl();
}
this.getUserId = () => {
return this.authInfo && this.authInfo.uid ? this.authInfo.uid : undefined;
}
},
* 中间件(请求后执行)
*/
_after(error, result) {
if (error) {
if (error.errCode) {
let errCode = error.errCode
if (!isUniPayError(errCode)) {
return error;
}
return new UniCloudError({
code: errCode,
message: error.errMsg || this.t(errCode, error.errMsgValue),
});
}
throw error
}
return result;
},
* 创建支付订单
*/
async createOrder(data) {
let {
provider,
total_fee,
openid,
order_no,
out_trade_no,
description,
type,
qr_code,
custom,
other,
clientInfo,
cloudInfo,
wxpay_virtual,
apple_virtual,
} = data;
if (!clientInfo) clientInfo = this.getClientInfo();
if (!cloudInfo) cloudInfo = this.getCloudInfo();
let user_uid = this.getUserId();
let res = await service.pay.createOrder({
provider,
total_fee,
user_uid,
openid,
order_no,
out_trade_no,
description,
type,
qr_code,
custom,
other,
clientInfo,
cloudInfo,
wxpay_virtual,
apple_virtual,
});
if (typeof res.order === "object" && typeof res.order["timestamp"] === "string") {
res.order["timestamp"] = parseFloat(res.order["timestamp"]);
}
return res;
},
* 接收支付异步通知
*/
async payNotify(data) {
const httpInfo = this.getHttpInfo();
const clientInfo = this.getClientInfo();
const cloudInfo = this.getCloudInfo();
return service.pay.paymentNotify({
httpInfo,
clientInfo,
cloudInfo
});
},
* 查询支付状态
*/
async getOrder(data) {
let {
out_trade_no,
transaction_id,
await_notify = false,
} = data;
res = await service.pay.getOrder({
out_trade_no,
transaction_id,
await_notify
});
return res;
},
* 发起退款
* 此api只有admin角色可以访问
*/
async refund(data) {
let {
out_trade_no,
out_refund_no,
refund_desc,
refund_fee,
} = data;
res = await service.pay.refund({
out_trade_no,
out_refund_no,
refund_desc,
refund_fee,
});
return res;
},
* 查询退款(查询退款情况)
*/
async getRefund(data) {
let {
out_trade_no,
} = data;
res = await service.pay.getRefund({
out_trade_no,
});
return res;
},
* 关闭订单(只有订单未支付时,才可以关闭,关闭后,用户即使在付款页面也无法付款)
*/
async closeOrder(data) {
let {
out_trade_no,
} = data;
res = await service.pay.closeOrder({
out_trade_no,
});
return res;
},
* 根据code获取openid
*/
async getOpenid(data = {}) {
let {
provider,
code,
clientInfo,
} = data;
if (!clientInfo) clientInfo = this.getClientInfo();
res = await service.pay.getOpenid({
provider,
code,
clientInfo
});
return res;
},
* 获取当前支持的支付方式
*/
async getPayProviderFromCloud() {
return await service.pay.getPayProviderFromCloud();
},
* 获取支付配置内的appid(主要用于获取获取微信公众号的appid,用以获取code)
*/
async getProviderAppId(data) {
let {
provider,
provider_pay_type
} = data;
let conifg = service.pay.getConfig();
try {
return {
errorCode: 0,
appid: conifg[provider][provider_pay_type].appId,
}
} catch (err) {
return {
errorCode: 0,
appid: null
};
}
},
* 验证iosIap苹果内购支付凭据
*/
async verifyReceiptFromAppleiap(data) {
let {
out_trade_no,
appleiap_account_token,
transaction_receipt,
transaction_identifier,
} = data;
const clientInfo = this.getClientInfo();
return await service.pay.verifyReceiptFromAppleiap({
out_trade_no,
appleiap_account_token,
transaction_receipt,
transaction_identifier,
clientInfo,
});
},
* 接收微信小程序虚拟支付异步通知
*/
async wxpayVirtualNotify(data) {
const httpInfo = this.getHttpInfo();
const clientInfo = this.getClientInfo();
const cloudInfo = this.getCloudInfo();
return service.pay.wxpayVirtualNotify({
httpInfo,
clientInfo,
cloudInfo
});
},
* 请求微信小程序虚拟支付API
*/
async requestWxpayVirtualApi(data) {
const clientInfo = this.getClientInfo();
if (clientInfo.source !== "function") {
throw new Error("requestWxpayVirtualApi只能通过云端调云端的方式调用");
}
let res = await service.pay.requestWxpayVirtualApi(data);
return res;
},
* 测试请求,仅为了确保是否请求能调通
*/
async test(data) {
return {
errCode: 0,
errMsg: "ok"
};
},
}