payUtil.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { NativeModules, NativeEventEmitter } from 'react-native'
  2. import * as authService from '../services/auth'
  3. import { ScaleUtil, accMul, accDiv } from '../utils/utils'
  4. import Toast from 'react-native-root-toast'
  5. let toast
  6. // 支付宝支付
  7. export const _AliPay = async (payment, payCallBack, payFailed, payGoback) => {
  8. /**
  9. * 1、生产支付单(需要模拟支付单数据) 返回支付单号
  10. * 2、以支付单号去支付
  11. * 3、订单信息去后台获取要支付的信息
  12. * 4、调起支付软件并支付
  13. * 5、将返回数据传后台进行验签
  14. * 6、验签后返回是否验签成功信息
  15. */
  16. if (toast) {
  17. Toast.hide(toast)
  18. }
  19. let totlal_result = ScaleUtil(payment.paymentAmount, 3)
  20. totlal_result = accDiv(Math.ceil(accMul(totlal_result, 100)), 100)
  21. const search = {
  22. subject: '渠道云助手在线支付',
  23. seller_id: '',
  24. out_trade_no: payment.paybillCode,
  25. total_amount: totlal_result,
  26. product_code: 'QUICK_MSECURITY_PAY',
  27. financialOrgId: payment.payeeId,
  28. // financialOrgId: "15cbaf47-e7dc-487b-97e1-96166314de55"
  29. }
  30. const pay = NativeModules.Alipay.pay
  31. const result = await authService.aliPay(search).catch(error => {
  32. toast = Toast.show('签名服务异常', { position: toastHeight })
  33. payFailed()
  34. console.log(error, 'error-Result!')
  35. })
  36. result.data = result.data.replace(/\+/g, ' ')
  37. let ret = await pay(result.data).catch(error => {
  38. toast = Toast.show('支付服务异常', { position: toastHeight })
  39. payFailed()
  40. })
  41. if (ret.resultStatus == '9000') {
  42. // 1-将返回的字符串传给后台验签
  43. // 在这写代码
  44. const returnSign = await authService
  45. .returnSign(JSON.parse(ret.result))
  46. .catch(() => {
  47. toast = Toast.show('验签服务异常', { position: toastHeight })
  48. payFailed()
  49. })
  50. if (returnSign && returnSign.data && returnSign.data == 'success') {
  51. payCallBack(ret)
  52. } else {
  53. toast = Toast.show('验签不通过,订单信息与发出不吻合', {
  54. position: toastHeight,
  55. })
  56. payFailed()
  57. }
  58. } else if (ret.resultStatus == '6001') {
  59. payGoback()
  60. } else {
  61. // 支付失败或取消
  62. payFailed()
  63. }
  64. }
  65. // 微信支付
  66. export const _WechatPay = async payment => {
  67. const Wxpay = NativeModules.Wxpay,
  68. //search = `body=${'渠道云销-支付'}&detail=${'测试数据'}&attach =${'附加数据--自定义'}&out_trade_no =${'商户订单号--统一支付单号'}&total_fee =${90}&spbill_create_ip =${'123.103.9.7'}&trade_type =${'APP'}`
  69. search = `body=${'易下单-支付'}&detail=${'测试数据'}&attach =${'附加数据--自定义'}&out_trade_no =${'商户订单号--统一支付单号'}&total_fee =${90}&spbill_create_ip =${'123.103.9.7'}&trade_type =${'APP'}`
  70. Wxpay.registerApp('wxdd9ee9be12443920') //注册微信
  71. const isSupported = await Wxpay.isSupported
  72. if (!isSupported) {
  73. toast = Toast.show('找不到微信应用,请安装最新版微信', {
  74. position: toastHeight,
  75. })
  76. return
  77. }
  78. const weChatAuth = await authService.weChatAuth(search)
  79. if (weChatAuth) {
  80. const unifiedOrder = await authService.unifiedOrder(weChatAuth)
  81. }
  82. // this.props.dispatch(
  83. // NavigationActions.navigate({ routeName: "ShoppingCartHomeTab" })
  84. // );
  85. // alert("微信支付");
  86. }