request-payment.uvue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <template>
  2. <page-head title="发起支付"></page-head>
  3. <view class="uni-common-mt" style="padding: 0 10px;">
  4. <text>如对当前页面的支付示例功能有任何疑问,通过电子邮件:service@dcloud.io 联系我们</text>
  5. </view>
  6. <template v-if="providerList.length > 0">
  7. <button style="margin-top: 20px;" type="primary" v-for="(item,index) in providerList" :key="index"
  8. @click="requestPayment(item)">{{item.name}}</button>
  9. </template>
  10. </template>
  11. <script>
  12. export type PayItem = { id : string, name : string, provider ?: UniProvider }
  13. export default {
  14. data() {
  15. return {
  16. btnText: "支付宝支付",
  17. btnType: "primary",
  18. orderInfo: "",
  19. errorCode: 0,
  20. errorMsg: "",
  21. complete: false,
  22. fail: false,
  23. providerList: [] as PayItem[],
  24. outTradeNo: "",
  25. openid: ""
  26. }
  27. },
  28. onLoad: function () {
  29. // #ifdef APP
  30. let provider = uni.getProviderSync({
  31. service: "payment",
  32. } as GetProviderSyncOptions)
  33. console.log(provider)
  34. provider.providerObjects.forEach((value : UniProvider) => {
  35. switch (value.id) {
  36. case 'alipay':
  37. var aliPayProvider = value as UniPaymentAlipayProvider
  38. console.log('alipay', aliPayProvider)
  39. this.providerList.push({
  40. name: aliPayProvider.description,
  41. id: aliPayProvider.id,
  42. provider: aliPayProvider
  43. } as PayItem);
  44. break;
  45. case 'wxpay':
  46. var wxPayProvider = value as UniPaymentWxpayProvider
  47. console.log('wxpay', wxPayProvider)
  48. this.providerList.push({
  49. name: wxPayProvider.description,
  50. id: wxPayProvider.id,
  51. provider: wxPayProvider
  52. } as PayItem);
  53. break;
  54. default:
  55. break;
  56. }
  57. })
  58. // #endif
  59. // #ifdef MP-WEIXIN
  60. this.getOpenId()
  61. this.providerList.push({
  62. name: '微信支付',
  63. id: 'wxpay'
  64. } as PayItem);
  65. // #endif
  66. },
  67. methods: {
  68. requestPayment(e : PayItem) {
  69. const provider = e.id
  70. // #ifdef APP
  71. if (provider == "alipay") {
  72. this.payAli(provider)
  73. } else if (provider == "wxpay") {
  74. // #ifdef APP-ANDROID
  75. if (e.provider != null && e.provider instanceof UniPaymentWxpayProvider && !((e.provider as UniPaymentWxpayProvider).isWeChatInstalled)) {
  76. uni.showToast({
  77. title: "微信没有安装",
  78. icon: 'error'
  79. })
  80. } else {
  81. this.payWX(provider)
  82. }
  83. // #endif
  84. // #ifdef APP-IOS || APP-HARMONY
  85. if (e.provider != null && ((e.provider as UniPaymentWxpayProvider).isWeChatInstalled == undefined || ((e.provider as UniPaymentWxpayProvider).isWeChatInstalled != null && (e.provider as UniPaymentWxpayProvider).isWeChatInstalled == false))) {
  86. uni.showToast({
  87. title: "微信没有安装",
  88. icon: 'error'
  89. })
  90. } else {
  91. this.payWX(provider)
  92. }
  93. // #endif
  94. }
  95. // #endif
  96. // #ifdef MP-WEIXIN
  97. this.payMpWexin()
  98. // #endif
  99. },
  100. payAli(id : string) {
  101. uni.showLoading({
  102. title: "请求中..."
  103. })
  104. uni.request({
  105. url: 'https://demo.dcloud.net.cn/payment/alipay/?total=0.01',
  106. method: 'GET',
  107. timeout: 6000,
  108. success: (res) => {
  109. this.orderInfo = JSON.stringify(res.data);
  110. console.log("====" + this.orderInfo)
  111. uni.hideLoading()
  112. uni.requestPayment({
  113. provider: id,
  114. orderInfo: res.data as string,
  115. fail: (res) => {
  116. console.log(JSON.stringify(res))
  117. this.errorCode = res.errCode
  118. uni.showToast({
  119. icon: 'error',
  120. title: 'errorCode:' + this.errorCode
  121. });
  122. },
  123. success: (res) => {
  124. console.log(JSON.stringify(res))
  125. uni.showToast({
  126. icon: 'success',
  127. title: '支付成功'
  128. });
  129. }
  130. })
  131. },
  132. fail: (e) => {
  133. console.log(e)
  134. uni.hideLoading()
  135. },
  136. });
  137. },
  138. payWX(id : string) {
  139. uni.showLoading({
  140. title: "请求中..."
  141. })
  142. let url = 'https://demo.dcloud.net.cn/payment/wxpayv3.__UNI__uniappx/?total=0.01'
  143. const res = uni.getAppBaseInfo();
  144. let packageName : string | null
  145. // #ifdef APP-ANDROID
  146. packageName = res.packageName
  147. // #endif
  148. // #ifdef APP-IOS
  149. packageName = res.bundleId
  150. // #endif
  151. if (packageName == 'io.dcloud.hellouniappx') {//hello uniappx
  152. url = 'https://demo.dcloud.net.cn/payment/wxpayv3.__UNI__HelloUniAppX/?total=0.01'
  153. }
  154. uni.request({
  155. url: url,
  156. method: 'GET',
  157. timeout: 6000,
  158. header: {
  159. "Content-Type": "application/json"
  160. } as UTSJSONObject,
  161. success: (res) => {
  162. console.log(res.data)
  163. uni.hideLoading()
  164. uni.requestPayment({
  165. provider: id,
  166. orderInfo: JSON.stringify(res.data),
  167. fail: (res) => {
  168. console.log(JSON.stringify(res))
  169. this.errorCode = res.errCode
  170. uni.showToast({
  171. duration: 5000,
  172. icon: 'error',
  173. title: 'errorCode:' + this.errorCode,
  174. });
  175. },
  176. success: (res) => {
  177. console.log(JSON.stringify(res))
  178. uni.showToast({
  179. duration: 5000,
  180. icon: 'success',
  181. title: '支付成功'
  182. });
  183. }
  184. })
  185. },
  186. fail: (res) => {
  187. uni.hideLoading()
  188. console.log(res)
  189. },
  190. });
  191. },
  192. // #ifdef MP-WEIXIN
  193. getOpenId() {
  194. uni.showLoading({
  195. title: "请稍等...",
  196. mask: true
  197. });
  198. let bundleId = uni.getAccountInfoSync().miniProgram.appId;
  199. uni.login({
  200. provider: 'weixin',
  201. success: (res) => {
  202. uni.request({
  203. url: "https://env-00jxt67zj8kj.dev-hz.cloudbasefunction.cn/uni-pay-api/getOpenId",
  204. method: "GET",
  205. data: {
  206. code: res.code,
  207. bundleId
  208. },
  209. success: (res) => {
  210. uni.hideLoading();
  211. this.openid = res.data.openid;
  212. console.log('openid: ', this.openid);
  213. },
  214. fail: (err) => {
  215. uni.hideLoading();
  216. console.error("request-err", err);
  217. }
  218. });
  219. }
  220. })
  221. },
  222. payMpWexin() {
  223. let bundleId = uni.getAccountInfoSync().miniProgram.appId;
  224. let random = Math.floor(Math.random() * 9000) + 1000;
  225. this.outTradeNo = `test${Date.now()}${random}`;
  226. console.log('outTradeNo: ', this.outTradeNo)
  227. uni.showLoading({
  228. title: "请求中..."
  229. });
  230. uni.request({
  231. url: "https://env-00jxt67zj8kj.dev-hz.cloudbasefunction.cn/uni-pay-api/getOrderInfo",
  232. method: "GET",
  233. data: {
  234. outTradeNo: this.outTradeNo,
  235. bundleId,
  236. openid: this.openid,
  237. totalFee: 1
  238. },
  239. success: (res) => {
  240. uni.hideLoading();
  241. let data = res.data as UTSJSONObject;
  242. let errCode = data['errCode'] as number;
  243. if (errCode != 0) {
  244. uni.showModal({
  245. title: "提示",
  246. content: data['errMsg'] as string,
  247. showCancel: false
  248. });
  249. return;
  250. }
  251. let orderInfo = data["orderInfo"] as string;
  252. console.log('orderInfo: ', orderInfo)
  253. uni.requestPayment({
  254. provider: "wxpay",
  255. orderInfo: orderInfo,
  256. ...JSON.parse(orderInfo),
  257. success: (res) => {
  258. console.log('res: ', res)
  259. uni.showToast({
  260. title: "支付成功",
  261. icon: 'success'
  262. });
  263. },
  264. fail: (err) => {
  265. console.error("err", err);
  266. uni.hideLoading();
  267. uni.showToast({
  268. title: "支付失败",
  269. icon: 'error'
  270. });
  271. }
  272. });
  273. },
  274. fail: (err) => {
  275. uni.hideLoading();
  276. console.error("request-err", err);
  277. }
  278. });
  279. },
  280. // #endif
  281. //自动化测试使用
  282. jest_pay() {
  283. uni.requestPayment({
  284. provider: "alipay",
  285. orderInfo: this.orderInfo,
  286. fail: (res : RequestPaymentFail) => {
  287. this.errorCode = res.errCode
  288. this.complete = true
  289. this.fail = true
  290. },
  291. success: (res : RequestPaymentSuccess) => {
  292. console.log(JSON.stringify(res))
  293. this.complete = true
  294. this.fail = false
  295. }
  296. } as RequestPaymentOptions)
  297. }
  298. }
  299. }
  300. </script>
  301. <style>
  302. </style>