diff --git a/example/package.json b/example/package.json index 21948c0..c89f8ce 100644 --- a/example/package.json +++ b/example/package.json @@ -1,6 +1,6 @@ { "dependencies": { - "bytedance-mini-pay": "^0.0.3", + "bytedance-mini-pay": "^0.0.4", "express": "^4.17.1" } } diff --git a/lib/index.js b/lib/index.js index 0021db2..ad05fe2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1 +1 @@ -"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var crypto=require("crypto"),fetch=require("node-fetch");function _interopDefaultLegacy(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var fetch__default=_interopDefaultLegacy(fetch);class TTPay{API_URL="https://developer.toutiao.com/api/apps/ecpay/v1";skipArr=["app_id","thirdparty_id","sign"];config={appId:"",appSecret:"",mchId:"",SALT:"",TOKEN:"",notifyURL:""};constructor(e){if(!e.appId)throw Error("config.appId is required");if(!e.appSecret)throw Error("config.appSecret is required");if(!e.SALT)throw Error("config.SALT is required");this.config={TOKEN:"",...e}}_genSign(e){const r=[];for(var t in e)-1==this.skipArr.indexOf(t)&&r.push(e[t]);return r.push(this.config.SALT),crypto.createHash("md5").update(r.sort().join("&")).digest("hex")}async _request(e,r={}){if(!e)throw Error("uri is not found");return fetch__default.default(`${this.API_URL}${e}`,{method:"POST",body:JSON.stringify(r),headers:{"Content-Type":"application/json","User-Agent":"flxxyz/bytedance-mini-pay"}}).then(e=>e.json())}_genParams(e,r={}){const t={app_id:this.config.appId,...r};return"create"===e&&(r.notifyURL||this.config.notifyURL)&&(t.notify_url=this.config.notifyURL),{...t,sign:this._genSign({...t})}}createOrder(e,r,t,i,n={}){n.valid_time||(n.valid_time=1800);n=this._genParams("create",{out_order_no:e,total_amount:r,subject:t,body:i,...n});return this._request("/create_order",n)}queryOrder(e,r={}){r=this._genParams("query",{out_order_no:e,...r});return this._request("/query_order",r)}createRefund(e,r,t,i,n={}){n=this._genParams("create",{out_order_no:e,out_refund_no:r,refund_amount:t,reason:i,...n});return this._request("/create_refund",n)}queryRefund(e,r={}){r=this._genParams("create",{out_refund_no:e,...r});return this._request("/query_refund",r)}checkNotifySign(e={}){const{msg_signature:r,timestamp:t,msg:i="",nonce:n}=e;e=[this.config.TOKEN,t,n,i].sort().join("");return r===crypto.createHash("sha1").update(e).digest("hex")}ackNotify=(e=()=>{})=>e('{"err_no":0,"err_tips":"success"}')}exports.default=TTPay; +"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var crypto=require("crypto"),fetch=require("node-fetch");function _interopDefaultLegacy(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var fetch__default=_interopDefaultLegacy(fetch);class TTPay{API_URL="https://developer.toutiao.com/api/apps/ecpay/v1";skipArr=["app_id","thirdparty_id","sign"];config={appId:"",appSecret:"",mchId:"",SALT:"",TOKEN:"",notifyURL:""};constructor(e){if(!e.appId)throw Error("config.appId is required");if(!e.appSecret)throw Error("config.appSecret is required");if(!e.SALT)throw Error("config.SALT is required");this.config={TOKEN:"",...e}}_genSign(e){const r=[];for(var t in e)-1==this.skipArr.indexOf(t)&&r.push(e[t]);return r.push(this.config.SALT),crypto.createHash("md5").update(r.sort().join("&")).digest("hex")}async _request(e,r={}){if(!e)throw Error("uri is not found");return fetch__default.default(`${this.API_URL}${e}`,{method:"POST",body:JSON.stringify(r),headers:{"Content-Type":"application/json","User-Agent":"flxxyz/bytedance-mini-pay"}}).then(e=>e.json())}_genParams(e,r={}){const t={app_id:this.config.appId,...r};return"create"===e&&(r.notifyURL||this.config.notifyURL)&&(t.notify_url=this.config.notifyURL),{...t,sign:this._genSign({...t})}}createOrder(e,r,t,i,o={}){o.valid_time||(o.valid_time=1800);o=this._genParams("create",{out_order_no:e,total_amount:r,subject:t,body:i,...o});return this._request("/create_order",o)}queryOrder(e,r={}){r=this._genParams("query",{out_order_no:e,...r});return this._request("/query_order",r)}createRefund(e,r,t,i,o={}){o=this._genParams("create",{out_order_no:e,out_refund_no:r,refund_amount:t,reason:i,...o});return this._request("/create_refund",o)}queryRefund(e,r={}){r=this._genParams("create",{out_refund_no:e,...r});return this._request("/query_refund",r)}checkNotifySign(e={}){const{msg_signature:r,timestamp:t,msg:i="",nonce:o}=e;e=[this.config.TOKEN,t,o,i].sort().join("");return r===crypto.createHash("sha1").update(e).digest("hex")}ackNotify=(e=()=>{})=>e('{"err_no":0,"err_tips":"success"}')}exports.TTPay=TTPay,exports.default=TTPay; diff --git a/package.json b/package.json index 54cb234..73d3be8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bytedance-mini-pay", - "version": "0.0.3", + "version": "0.0.4", "description": "字节跳动小程序支付SDK", "main": "lib/index.js", "scripts": {