"use strict";const e=require("../../common/vendor.js"),a=require("../index.js");require("../helper/index.js");const t=require("../api/pay/order.js");exports.SheepPay=class{constructor(e,a,t){this.payment=e,this.id=t,this.orderType=a,this.payAction()}payAction(){return{WechatOfficialAccount:{wechat:()=>{this.wechatOfficialAccountPay()},alipay:()=>{this.redirectPay()},wallet:()=>{this.walletPay()},mock:()=>{this.mockPay()}},WechatMiniProgram:{wechat:()=>{this.wechatMiniProgramPay()},alipay:()=>{this.copyPayLink()},wallet:()=>{this.walletPay()},mock:()=>{this.mockPay()}},App:{wechat:()=>{this.wechatAppPay()},alipay:()=>{this.alipay()},wallet:()=>{this.walletPay()},mock:()=>{this.mockPay()}},H5:{wechat:()=>{this.wechatWapPay()},alipay:()=>{this.redirectPay()},wallet:()=>{this.walletPay()},mock:()=>{this.mockPay()}}}[a.sheep.$platform.name][this.payment]()}prepay(e){return new Promise((async(i,s)=>{let c={id:this.id,channelCode:e,channelExtras:{}};if(["wx_pub","wx_lite"].includes(e)){console.log(a.sheep.$platform.useProvider("wechat").getOpenid);const e=await a.sheep.$platform.useProvider("wechat").getOpenid(!0);if(!e)return void this.bindWeixin();c.channelExtras.openid=e}t.PayOrderApi.submitOrder(c).then((e=>{0===e.code&&i(e),0!==e.code&&e.msg.indexOf("无效的openid")>=0&&(e.msg.indexOf("无效的openid")>=0||e.msg.indexOf("下单账号与支付账号不一致")>=0)&&this.bindWeixin()}))}))}async wechatMiniProgramPay(){let{code:t,data:i}=await this.prepay("wx_lite");if(0!==t)return;const s=JSON.parse(i.displayContent);e.index.requestPayment({provider:"wxpay",timeStamp:s.timeStamp,nonceStr:s.nonceStr,package:s.packageValue,signType:s.signType,paySign:s.paySign,success:e=>{this.payResult("success")},fail:e=>{"requestPayment:fail cancel"===e.errMsg?a.sheep.$helper.toast("支付已手动取消"):this.payResult("fail")}})}async walletPay(){const{code:e}=await this.prepay("wallet");0===e&&this.payResult("success")}async mockPay(){const{code:e}=await this.prepay("mock");0===e&&this.payResult("success")}async copyPayLink(){let{error:t,data:i}=await this.prepay();0===t&&e.index.showModal({title:"支付宝支付",content:"复制链接到外部浏览器",confirmText:"复制链接",success:e=>{e.confirm&&a.sheep.$helper.copyText(i.pay_data)}})}async alipay(){let t=this;const{error:i,data:s}=await this.prepay();0===i&&e.index.requestPayment({provider:"alipay",orderInfo:s.pay_data,success:e=>{t.payResult("success")},fail:e=>{"requestPayment:fail [paymentAlipay:62001]user cancel"===e.errMsg?a.sheep.$helper.toast("支付已手动取消"):t.payResult("fail")}})}async wechatAppPay(){let a=this,{error:t,data:i}=await this.prepay();0===t&&e.index.requestPayment({provider:"wxpay",orderInfo:i.pay_data,success:e=>{a.payResult("success")},fail:e=>{"requestPayment:fail cancel"!==e.errMsg&&a.payResult("fail")}})}payResult(e){a.sheep.$router.redirect("/pages/pay/result",{id:this.id,orderType:this.orderType,payState:e})}bindWeixin(){e.index.showModal({title:"微信支付",content:"请先绑定微信再使用微信支付",confirmText:"绑定",success:function(e){e.confirm&&a.sheep.$platform.useProvider("wechat").bind()}})}},exports.getPayMethods=function(e){const t=[{icon:"/static/img/shop/pay/wechat.png",title:"微信支付",value:"wechat",disabled:!0},{icon:"/static/img/shop/pay/alipay.png",title:"支付宝支付",value:"alipay",disabled:!0}],i=a.sheep.$platform.name,s=t[0];("WechatOfficialAccount"===i&&e.includes("wx_pub")||"WechatMiniProgram"===i&&e.includes("wx_lite")||"App"===i&&e.includes("wx_app"))&&(s.disabled=!1),s.disabled=!1;const c=t[1];return("WechatOfficialAccount"===i&&e.includes("alipay_wap")||"WechatMiniProgram"===i&&e.includes("alipay_wap")||"App"===i&&e.includes("alipay_app"))&&(c.disabled=!1),t};
