paypal-button.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <!-- paypal 支付按钮 -->
  3. <div class="pay-wrapper">
  4. <div class="pay-msg">
  5. <div class="row">
  6. <template v-if="currentCurrencyInfo.currencyCode == 'USD'">
  7. Pay ${{finalAmountData.finalAmountValue || 0}}
  8. <span>
  9. (Available ${{finalAmountData.requestAmountValue}})
  10. </span>
  11. </template>
  12. <template v-else>
  13. <span class="desc">
  14. Pay
  15. </span>
  16. {{payConfig.amount || 0}}
  17. <img :src="currentCurrencyInfo.iconPath" class="icon">
  18. <span>
  19. {{currentCurrencyInfo.tokenSymbol}}
  20. </span>
  21. </template>
  22. </div>
  23. <div class="msg">
  24. <template v-if="currentCurrencyInfo.currencyCode == 'USD'">
  25. {{payConfig.feeDesc}}
  26. </template>
  27. <template v-else>
  28. (Balance: {{currentCurrencyInfo.balance}})
  29. </template>
  30. </div>
  31. </div>
  32. <div class="pay-btn">
  33. <div class="iframe-pay"
  34. v-show="currentCurrencyInfo.currencyCode == 'USD'"
  35. @click="paypalPay">
  36. <iframe
  37. class="iframe-pay"
  38. ref="iframe"
  39. :src="`${payConfig.paypalHtml}?paypalClientId=${payConfig.paypalClientId}&amount=${props.finalAmountData.finalAmountValue}`"></iframe>
  40. </div>
  41. <div class="token-pay"
  42. v-if="currentCurrencyInfo.currencyCode != 'USD'"
  43. @click="tokenPay">
  44. Pay Now
  45. </div>
  46. </div>
  47. </div>
  48. </template>
  49. <script setup>
  50. import { onMounted, ref, defineProps, defineEmits, watch } from "vue";
  51. import {payTaskLuckdropWithBalance} from "@/http/publishApi"
  52. import Report from "@/logcenter/log"
  53. const props = defineProps({
  54. finalAmountData: {
  55. type: Object,
  56. default: () => {
  57. return {
  58. currencyCode: "",
  59. feeAmountValue: 0,
  60. finalAmountValue: 0,
  61. requestAmountValue: 0
  62. }
  63. },
  64. },
  65. payConfig: {
  66. type: Object,
  67. default: () => {
  68. return {
  69. paypalClientId: '',
  70. paypalHtml: 'https://d1mcov78iir8kk.cloudfront.net/website/paypal_1.html'
  71. }
  72. }
  73. },
  74. currentCurrencyInfo: {
  75. type: Object,
  76. default: () => {
  77. }
  78. },
  79. });
  80. let iframe = ref(null);
  81. let payIng = false;
  82. watch(
  83. () => props.finalAmountData.finalAmountValue,
  84. (newVal) => {
  85. iframe.value.contentWindow.postMessage({
  86. actionType: "setAmount", amount: newVal
  87. },
  88. "*"
  89. );
  90. },
  91. {
  92. deep: true
  93. }
  94. );
  95. const emits = defineEmits(["payPalFinsh"]);
  96. const paypalPay = () => {
  97. Report.reportLog({
  98. pageSource: Report.pageSource.previewPage,
  99. businessType: Report.businessType.buttonClick,
  100. objectType: Report.objectType.confirmButton
  101. }, {
  102. type: 'paypal'
  103. });
  104. };
  105. const tokenPay = () => {
  106. Report.reportLog({
  107. pageSource: Report.pageSource.previewPage,
  108. businessType: Report.businessType.buttonClick,
  109. objectType: Report.objectType.confirmButton
  110. }, {
  111. type: 'confirm'
  112. });
  113. if(payIng) {
  114. return;
  115. }
  116. payIng = true;
  117. payTaskLuckdropWithBalance({
  118. params: {
  119. currencyCode: props.currentCurrencyInfo.currencyCode,
  120. postId: props.payConfig.postId
  121. }
  122. }).then(res => {
  123. if(res.code == 0) {
  124. emits("payPalFinsh", {...res.data, payNetwork: 'bsc'});
  125. }
  126. payIng = false;
  127. }).catch(() => {
  128. payIng = false;
  129. })
  130. }
  131. onMounted(() => {
  132. window.addEventListener("message", function (event) {
  133. if (event.data && event.data.actionType) {
  134. switch (event.data.actionType) {
  135. case "payCallBack":
  136. console.log(
  137. "payCallBack",
  138. event.data.orderData,
  139. event.data.transaction
  140. );
  141. emits("payPalFinsh", {...event.data, payNetwork: 'paypal'});
  142. break;
  143. }
  144. }
  145. });
  146. });
  147. </script>
  148. <style lang="scss" scoped>
  149. .pay-wrapper {
  150. width: 100%;
  151. height: 80px;
  152. background-color: #fff;
  153. position: absolute;
  154. left: 0;
  155. bottom: 0;
  156. box-shadow: 0 -4px 8px -3px #00000026;
  157. border-bottom-right-radius: 16px;
  158. padding: 12px 30px;
  159. box-sizing: border-box;
  160. display: flex;
  161. align-items: center;
  162. justify-content: flex-end;
  163. border-bottom-left-radius: 16px;
  164. z-index: 999;
  165. .pay-msg {
  166. text-align: right;
  167. margin-right: 25px;
  168. .row {
  169. font-weight: 600;
  170. font-size: 16px;
  171. color: #1D9BF0;
  172. display: flex;
  173. align-items: center;
  174. margin-bottom: 6px;
  175. span {
  176. display: inline-block;
  177. color: #000000;
  178. margin-left: 6px;
  179. }
  180. .icon {
  181. width: 14px;
  182. margin-left:6px;
  183. }
  184. .desc {
  185. margin-right:6px
  186. }
  187. }
  188. .msg {
  189. font-size: 13px;
  190. color: #898989;
  191. }
  192. }
  193. .pay-btn {
  194. width: 206px;
  195. height: 48px;
  196. .token-pay {
  197. width: 100%;
  198. height: 100%;
  199. background: #1D9BF0;
  200. border-radius: 10000px;
  201. display: flex;
  202. align-items: center;
  203. justify-content: center;
  204. font-weight: 600;
  205. font-size: 18px;
  206. color: #fff;
  207. cursor: pointer;
  208. }
  209. .iframe-pay {
  210. width: 100%;
  211. height: 100%;
  212. }
  213. iframe {
  214. border: medium none;
  215. width: 100%;
  216. height: 100%;
  217. }
  218. }
  219. }
  220. </style>