payment.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // 必须传入price,show tradeNo
  2. // bind:payResult 返回支付状态
  3. const CONFIG = require("../../config.js");
  4. const Parse = getApp().Parse;
  5. const company = getApp().globalData.company;
  6. Component({
  7. behaviors: [],
  8. properties: {
  9. show: {
  10. type: Boolean,
  11. value: false,
  12. },
  13. //支付价格 *必填
  14. price: {
  15. type: Number,
  16. value: 0,
  17. },
  18. //订单id Order表创建后id
  19. orderId: {
  20. type: String,
  21. },
  22. //支付类型
  23. orderType: {
  24. type: String,
  25. value: 'shopgoods'
  26. },
  27. //订单编号 *必填
  28. tradeNo: {
  29. type: String,
  30. },
  31. //显示公益金支付 Profile — bonus字段
  32. showBonus: {
  33. type: Boolean,
  34. value: false
  35. },
  36. //显示支付类型
  37. showType: {
  38. type: String,
  39. value: 'all'
  40. },
  41. profileId: {
  42. type: String
  43. }
  44. },
  45. data: {
  46. radio: "wxpay",
  47. isPay: false,
  48. balanceTitle:CONFIG.default.payBalanceTitle || '余额'
  49. },
  50. lifetimes: {
  51. created() {
  52. },
  53. attached() {},
  54. moved() {},
  55. detached() {},
  56. },
  57. pageLifetimes: {
  58. show: async function () {
  59. // 页面显示
  60. },
  61. hide: function () {
  62. // 页面被隐藏
  63. this.setData({
  64. show: false,
  65. });
  66. },
  67. resize: function (size) {
  68. // 页面尺寸变化
  69. },
  70. },
  71. methods: {
  72. onChange(event) {
  73. console.log(event)
  74. this.setData({
  75. radio: event.detail,
  76. });
  77. console.log(this.data.radio);
  78. },
  79. onClickHide() {
  80. this.setData({
  81. show: false
  82. });
  83. this.payResult("Cancel the payment", undefined);
  84. },
  85. /* 支付结果返回父组件 */
  86. payResult(state, no) {
  87. let {
  88. radio
  89. } = this.data
  90. this.triggerEvent("payResult", {
  91. params: state,
  92. no: no,
  93. type: radio
  94. });
  95. },
  96. pay() {
  97. if (this.data.isPay) {
  98. wx.showToast({
  99. title: '你已支付,请勿重复提交',
  100. icon: "none"
  101. })
  102. return
  103. }
  104. let payType = this.data.radio;
  105. switch (payType) {
  106. case "wxpay":
  107. console.log(this.properties.tradeNo)
  108. this.wxPay();
  109. break;
  110. case "balance":
  111. this.balance();
  112. break;
  113. case "bonus":
  114. this.bonusPay();
  115. break;
  116. case "offline":
  117. break;
  118. }
  119. },
  120. async wxPay() {
  121. let that = this;
  122. let Account = new Parse.Query("Account");
  123. Account.equalTo("user", Parse.User.current().id);
  124. Account.equalTo("company", company);
  125. let account = await Account.first();
  126. let AccountLog = Parse.Object.extend("AccountLog");
  127. let accountLog = new AccountLog();
  128. accountLog.set("assetType", "wxPay");
  129. accountLog.set("payType", "wxpay-wxapp");
  130. accountLog.set("assetCount", this.data.price);
  131. accountLog.set("desc", "微信支付");
  132. accountLog.set("orderType", this.data.orderType + "-wxpay");
  133. accountLog.set("orderNumber", this.properties.tradeNo);
  134. if (this.properties.orderId) accountLog.set("orderId", this.properties.orderId)
  135. accountLog.set("isVerified", false);
  136. accountLog.set("targetName", "system");
  137. if (account) {
  138. accountLog.set("fromAccount", account.toPointer());
  139. }
  140. accountLog.set("fromName", Parse.User.current().get("nickname"));
  141. accountLog.set("company", {
  142. __type: "Pointer",
  143. className: "Company",
  144. objectId: company,
  145. });
  146. let appId = getApp().globalData.appid;
  147. let openId = wx.getStorageSync("userInfo").openid;
  148. if(!openId && Parse.User.current().get("wxapp")){
  149. openId = Parse.User.current().get("wxapp")?.[appId]?.openid
  150. }
  151. let goodsTotalPrice = this.data.price;
  152. console.log(goodsTotalPrice)
  153. if(goodsTotalPrice == 0){
  154. this.updateLog(accountLog)
  155. return
  156. }
  157. wx.request({
  158. method: "POST",
  159. url: "https://server.fmode.cn/api/wxpay/neworder",
  160. data: {
  161. appid: appId,
  162. openid: openId,
  163. out_trade_no: that.properties.tradeNo,
  164. total_fee: goodsTotalPrice,
  165. company: company
  166. },
  167. success: (payres) => {
  168. console.log("Pay Params");
  169. console.log(payres);
  170. let payinfo = payres.data;
  171. wx.requestPayment({
  172. appId: appId,
  173. timeStamp: payinfo.timeStamp,
  174. nonceStr: payinfo.nonceStr,
  175. package: payinfo.package,
  176. signType: "MD5",
  177. paySign: payinfo.paySign,
  178. async success(data) {
  179. that.updateLog(accountLog)
  180. wx.showToast({
  181. title: "支付成功",
  182. icon: "success",
  183. duration: 1500,
  184. });
  185. that.setData({
  186. show: false
  187. })
  188. },
  189. fail(err) {
  190. that.payResult(err, that.properties.tradeNo);
  191. wx.showToast({
  192. title: "支付失败",
  193. icon: "error",
  194. duration: 1500,
  195. });
  196. },
  197. complete:(data) => {
  198. console.log(111,data)
  199. }
  200. });
  201. },
  202. fail: (err) => {
  203. wx.showToast({
  204. title: "支付失败",
  205. icon: "error",
  206. duration: 1500,
  207. });
  208. return false;
  209. },
  210. });
  211. },
  212. //支付成功更新log
  213. async updateLog(accountLog){
  214. this.payResult("ok", this.properties.tradeNo);
  215. accountLog.set("isVerified", true);
  216. await accountLog.save()
  217. },
  218. async sendOrder(tradeNo) {
  219. let that = this;
  220. let DramaOrderLog = Parse.Object.extend("UserAgentOrder");
  221. let current = Parse.User.current();
  222. let dramaOrderLog = new DramaOrderLog();
  223. dramaOrderLog.set("user", {
  224. __type: "Pointer",
  225. className: "_User",
  226. objectId: current.id,
  227. });
  228. dramaOrderLog.set("orderId", tradeNo);
  229. dramaOrderLog.set("company", {
  230. __type: "Pointer",
  231. className: "Company",
  232. objectId: getApp().globalData.company,
  233. });
  234. await dramaOrderLog.save();
  235. },
  236. async balance() {
  237. let { balanceTitle } = this.data
  238. let _this = this;
  239. try {
  240. let price = this.data.price
  241. let Account = new Parse.Query("Account");
  242. Account.equalTo("user", Parse.User.current().id);
  243. Account.equalTo("company", company);
  244. let account = await Account.first();
  245. console.log(account);
  246. if (!account.get("balance") || price > account.get("balance")) {
  247. wx.showToast({
  248. title: `${balanceTitle == '积分' ? '积分不足' : '余额不足,请充值'}`,
  249. icon: "none"
  250. })
  251. return
  252. }
  253. let AccountLog = Parse.Object.extend("AccountLog");
  254. let accountLog = new AccountLog();
  255. accountLog.set("assetType", "balance");
  256. accountLog.set("payType", "balance");
  257. accountLog.set("assetCount", this.data.price);
  258. accountLog.set("desc", `${balanceTitle}支付`);
  259. accountLog.set("orderType", this.data.orderType + "-balance");
  260. accountLog.set("orderNumber", this.properties.tradeNo);
  261. if (this.properties.orderId) accountLog.set("orderId", this.properties.orderId)
  262. accountLog.set("isVerified", false);
  263. accountLog.set("targetName", "system");
  264. if (account) {
  265. accountLog.set("fromAccount", account.toPointer());
  266. }
  267. accountLog.set("fromName", Parse.User.current().get("nickname"));
  268. accountLog.set("company", {
  269. __type: "Pointer",
  270. className: "Company",
  271. objectId: company,
  272. });
  273. accountLog.save()
  274. .then((res) => {
  275. res.set("isVerified", true);
  276. res.save().then((result) => {
  277. console.log(result)
  278. wx.showToast({
  279. title: "支付成功",
  280. icon: "success",
  281. duration: 1500,
  282. });
  283. _this.payResult("ok", this.properties.tradeNo);
  284. })
  285. })
  286. } catch (error) {
  287. console.log(error)
  288. wx.showToast({
  289. title: "支付失败",
  290. icon: "error",
  291. duration: 1500,
  292. });
  293. _this.payResult(error);
  294. }
  295. this.setData({
  296. isPay: true
  297. })
  298. },
  299. //公益金支付
  300. async bonusPay() {
  301. let _this = this;
  302. let {
  303. profileId
  304. } = this.data
  305. try {
  306. let data = await this.customSQL()
  307. console.log(data);
  308. if (data.code == 200) {
  309. let Account = new Parse.Query("Account");
  310. Account.equalTo("user", Parse.User.current().id);
  311. Account.equalTo("company", company);
  312. let account = await Account.first();
  313. let AccountLog = Parse.Object.extend("AccountLog");
  314. let accountLog = new AccountLog();
  315. accountLog.set("assetType", "bonus");
  316. accountLog.set("payType", "bonus");
  317. accountLog.set("assetCount", this.data.price);
  318. accountLog.set("desc", "公益金支付");
  319. accountLog.set("orderType", this.data.orderType + "-bonus");
  320. accountLog.set("orderNumber", this.properties.tradeNo);
  321. if (this.properties.orderId) accountLog.set("orderId", this.properties.orderId)
  322. accountLog.set("isVerified", true);
  323. accountLog.set("targetName", "system");
  324. if (!profileId && account) {
  325. accountLog.set("fromAccount", account.toPointer());
  326. }
  327. accountLog.set("fromName", Parse.User.current().get("nickname"));
  328. accountLog.set("company", {
  329. __type: "Pointer",
  330. className: "Company",
  331. objectId: company,
  332. });
  333. await accountLog.save()
  334. wx.showToast({
  335. title: data.msg,
  336. icon: 'none',
  337. image: '',
  338. duration: 1500,
  339. mask: false,
  340. });
  341. _this.payResult("ok", this.properties.tradeNo);
  342. }else{
  343. wx.showToast({
  344. title: data.msg,
  345. icon: 'none',
  346. image: '',
  347. duration: 1500,
  348. mask: false,
  349. });
  350. }
  351. } catch (err) {
  352. console.log(err)
  353. wx.showToast({
  354. title: "支付失败",
  355. icon: "error",
  356. duration: 1500,
  357. });
  358. _this.payResult(error);
  359. }
  360. },
  361. async customSQL() {
  362. let price = this.data.price
  363. let {
  364. profileId
  365. } = this.data
  366. let promise = await new Promise((resolve, rej) => {
  367. wx.request({
  368. url: "https://server.fmode.cn/api/food/bonus",
  369. data: {
  370. pid: profileId,
  371. price: price,
  372. company: company
  373. },
  374. method: "POST",
  375. success(data) {
  376. console.log(data);
  377. resolve(data.data)
  378. },
  379. fail(err) {
  380. rej(err);
  381. },
  382. });
  383. });
  384. return promise;
  385. },
  386. },
  387. observers: {
  388. show: function (show) {
  389. console.log(show)
  390. }
  391. }
  392. });