123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- import {
- gotoOrderAppPayApi,
- gotoOrderH5PayApi,
- gotoOrderPayApi,
- payOrderSuccessApi
- } from '../api/user'
- const jweixin = require('jweixin-module')
- async function payH5InEquipment(payInfo) {
- try {
- const res = await gotoOrderH5PayApi(payInfo)
- location.replace(res.data.mwebUrl)
- } catch (e) {
- uni.showToast({ title: '支付失败', icon: 'none' })
- if ([1, 2].includes(payInfo.purchaseMode)) {
- uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
- } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
- }
- } finally {
- uni.hideLoading()
- }
- }
- async function payH5InWechat(payInfo) {
- const res = await gotoOrderPayApi(payInfo)
- jweixin.config({
- debug: false,
- appId: res.data.appId,
- timestamp: res.data.timeStamp,
- nonceStr: res.data.nonceStr,
- signature: res.data.paySign,
- jsApiList: [ 'chooseWXPay' ]
- })
- if ([1, 2].includes(payInfo.purchaseMode)) {
- jweixin.ready(function () {
- jweixin.checkJsApi({
- jsApiList: [ 'chooseWXPay' ],
- success(res) { },
- fail(res) { }
- })
- jweixin.chooseWXPay({
- timestamp: res.data.timeStamp,
- nonceStr: res.data.nonceStr,
- package: res.data.package,
- signType: res.data.signType,
- paySign: res.data.paySign,
- success(res) {
- uni.showToast({ icon: 'none', title: '支付成功' })
- uni.redirectTo({ url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId })
- },
- cancel(r) {
- uni.showToast({ icon: 'none', title: '用户取消支付' })
- uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
- },
- fail(res) {
- uni.showToast({ icon: 'none', title: '微信内支付错误' })
- uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
- }
- })
- })
- jweixin.error(function (res) {
- uni.showToast({ icon: 'none', title: '微信内支付加载失败', duration: 3000 })
- uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
- })
- } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
- jweixin.ready(function () {
- jweixin.checkJsApi({
- jsApiList: [ 'chooseWXPay' ],
- success(res) { },
- fail(res) { }
- })
- jweixin.chooseWXPay({
- timestamp: res.data.timeStamp,
- nonceStr: res.data.nonceStr,
- package: res.data.package,
- signType: res.data.signType,
- paySign: res.data.paySign,
- success(res) {
- uni.showToast({ icon: 'none', title: '支付成功' })
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index' })
- },
- cancel(r) {
- uni.showToast({ icon: 'none', title: '用户取消支付' })
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
- },
- fail(res) {
- uni.showToast({ icon: 'none', title: '微信内支付错误' })
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
- }
- })
- })
- jweixin.error(function (res) {
- uni.showToast({ icon: 'none', title: '微信内支付加载失败', duration: 3000 })
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
- })
- }
- }
- async function h5Pay(payInfo) {
- const ua = navigator.userAgent.toLowerCase()
- if (ua.match(/MicroMessenger/i) == 'micromessenger') {
- await payH5InWechat(payInfo)
- } else {
- await payH5InEquipment(payInfo)
- }
- }
- async function mpAliPay(payInfo) {
- if ([1, 2].includes(payInfo.purchaseMode)) {
- try {
- const res = await gotoOrderPayApi(payInfo)
- uni.requestPayment({
- provider: 'alipay',
- orderInfo: res.data.tradeNo,
- success(payRes) {
- if (payRes.resultCode == '6001') {
- uni.showToast({ icon: 'none', title: '取消支付' })
- uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
- } else if (payRes.resultCode == '9000') {
- uni.showToast({ icon: 'none', title: '支付成功' })
- uni.redirectTo({ url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId })
- }
- },
- fail(err) {
- uni.showToast({ icon: 'none', title: '支付取消' })
- uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
- }
- })
- } catch (e) {
- uni.showToast({ title: '支付宝支付异常', icon: 'none' })
- uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
- } finally {
- uni.hideLoading()
- }
- } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
- try {
- const res = await gotoOrderPayApi(payInfo)
- uni.requestPayment({
- provider: 'alipay',
- orderInfo: res.data.tradeNo,
- success(payRes) {
- if (payRes.resultCode == '6001') {
- uni.showToast({ icon: 'none', title: '取消支付' })
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
- } else if (payRes.resultCode == '9000') {
- uni.showToast({ icon: 'none', title: '支付成功' })
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index' })
- }
- },
- fail(err) {
- uni.showToast({ icon: 'none', title: '支付取消' })
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
- }
- })
- } catch (e) {
- uni.showToast({ title: '支付宝支付异常', icon: 'none' })
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
- } finally {
- uni.hideLoading()
- }
- }
- }
- async function mpWechatPay(payInfo) {
- if ([1, 2].includes(payInfo.purchaseMode)) {
- try {
- const res = await gotoOrderPayApi(payInfo)
- uni.requestPayment({
- provider: 'wxpay',
- timeStamp: res.data.timeStamp,
- nonceStr: res.data.nonceStr,
- package: res.data.package,
- signType: res.data.signType,
- paySign: res.data.paySign,
- success: async (payRes) => {
-
- if (payInfo.collageId) await payOrderSuccessApi({ orderId: payInfo.orderId, collageId: payInfo.collageId })
- uni.showToast({ icon: 'none', title: '支付成功' })
- uni.redirectTo({ url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId })
- },
- fail(err) {
- uni.showToast({ icon: 'none', title: '用户取消支付' })
- uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
- }
- })
- } catch (e) {
- uni.showToast({ title: '微信支付拉起失败', icon: 'none' })
- uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
- }
- } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
- try {
- const res = await gotoOrderPayApi(payInfo)
- uni.requestPayment({
- provider: 'wxpay',
- timeStamp: res.data.timeStamp,
- nonceStr: res.data.nonceStr,
- package: res.data.package,
- signType: res.data.signType,
- paySign: res.data.paySign,
- success: async (payRes) => {
-
- if (payInfo.collageId) await payOrderSuccessApi({ orderId: payInfo.orderId, collageId: payInfo.collageId })
- uni.showToast({ icon: 'none', title: '支付成功' })
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index' })
- },
- fail(err) {
- uni.showToast({ icon: 'none', title: '用户取消支付' })
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
- }
- })
- } catch (e) {
- uni.showToast({ title: '微信支付拉起失败', icon: 'none' })
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
- }
- }
- }
- async function appWechatPay(payInfo) {
- if ([1, 2].includes(payInfo.purchaseMode)) {
- try {
- const res = await gotoOrderAppPayApi(payInfo)
- const obj = {
- appid: res.data.appId,
- noncestr: res.data.nonceStr,
- package: 'Sign=WXPay',
- prepayid: res.data.prepayId,
- timestamp: res.data.timeStamp,
- sign: res.data.paySign,
- partnerid: res.data.partnerId
- }
- uni.requestPayment({
- provider: 'wxpay',
- orderInfo: obj,
- success(payRes) {
- uni.showToast({ icon: 'none', title: '支付成功' })
- uni.redirectTo({ url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId })
- },
- fail(err) {
- uni.showToast({ icon: 'none', title: '用户取消支付' })
- uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
- }
- })
- } catch (e) {
- uni.showToast({ title: 'APP拉起微信支付失败', icon: 'none' })
- uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
- } finally {
- uni.hideLoading()
- }
- } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
- try {
- const res = await gotoOrderAppPayApi(payInfo)
- const obj = {
- appid: res.data.appId,
- noncestr: res.data.nonceStr,
- package: 'Sign=WXPay',
- prepayid: res.data.prepayId,
- timestamp: res.data.timeStamp,
- sign: res.data.paySign,
- partnerid: res.data.partnerId
- }
- uni.requestPayment({
- provider: 'wxpay',
- orderInfo: obj,
- success(payRes) {
- uni.showToast({ icon: 'none', title: '支付成功' })
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index' })
- },
- fail(err) {
- uni.showToast({ icon: 'none', title: '用户取消支付' })
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
- }
- })
- } catch (e) {
- uni.showToast({ title: 'APP拉起微信支付失败', icon: 'none' })
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
- } finally {
- uni.hideLoading()
- }
- }
- }
- async function bankCardPay(payInfo) {
- try {
- let res
- if ([1, 2].includes(payInfo.purchaseMode)) {
- } else if (payInfo.purchaseMode === 3) {
-
-
- } else if (payInfo.purchaseMode === 4) {
-
-
- }
- } catch (e) {
- if ([1, 2].includes(payInfo.purchaseMode)) {
- } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
- uni.showToast({ title: '银行卡支付失败', icon: 'none' })
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
- }
- } finally {
- uni.hideLoading()
- }
- }
- async function h5TonglianPay(payInfo) {
- try {
- const res = await gotoOrderH5PayApi(payInfo)
- console.log(res)
- if (res.data === '支付成功') {
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index' })
- } else {
- const payData = JSON.parse(res.data.package)
- const form = document.createElement('form')
- form.setAttribute('action', res.data.mwebUrl)
- form.setAttribute('method', 'POST')
- let input
- for (const key in payData) {
- input = document.createElement('input')
- input.name = key
- input.value = payData[key]
- form.appendChild(input)
- }
- document.body.appendChild(form)
- form.submit()
- document.body.removeChild(form)
- }
- } catch (e) {
- console.log(e)
- uni.showToast({ title: '支付失败', icon: 'none' })
- if ([1, 2].includes(payInfo.purchaseMode)) {
- uni.redirectTo({ url: '/pages_category_page1/orderModule/index?type=1' })
- } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
- uni.redirectTo({ url: '/pages_user/serve/payment-completed/index?state=fail' })
- }
- } finally {
- uni.hideLoading()
- }
- }
- export async function handleDoPay(submitResult, purchaseMode) {
- uni.showLoading({
- mask: true,
- title: '支付中...'
- })
- if (purchaseMode) {
- const submitInfo = { ...submitResult, purchaseMode }
- if (submitInfo.paymentMode === 999) {
- await bankCardPay(submitInfo)
- } else if (submitInfo.paymentMode === 1) {
-
-
- await appWechatPay(submitInfo)
-
-
- await mpWechatPay(submitInfo)
-
-
- await h5Pay(submitInfo)
-
- } else if (submitInfo.paymentMode === 4) {
-
- await h5TonglianPay(submitInfo)
-
- } else if ([2, 3].includes(submitInfo.paymentMode)) {
-
-
- await mpAliPay(submitInfo)
-
- throw new Error('支付宝相关支付暂时只支持支付宝小程序')
-
- }
-
-
-
-
-
-
-
-
-
- }
- }
|