paypal.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. <style>
  9. html,
  10. body,
  11. iframe{
  12. margin: 0;
  13. border: 0;
  14. padding: 0;
  15. display: block;
  16. height: 100%;
  17. width: 100%;
  18. background: white;
  19. color: black;
  20. overflow: hidden;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <!-- Include the PayPal JavaScript SDK; replace "test" with your own sandbox Business account app client ID -->
  26. <!-- Set up a container element for the button -->
  27. <div id="paypal-button-container"></div>
  28. <script>
  29. let isDOMContentLoaded = false;
  30. let isOnMessage = false;
  31. document.addEventListener("DOMContentLoaded", function () {
  32. isDOMContentLoaded = true;
  33. })
  34. function loadAsyncScript(src, callback = function () {}) { // 同步加载js
  35. const head = document.getElementsByTagName('head')[0];
  36. const script = document.createElement('script');
  37. script.setAttribute('type', 'text/javascript');
  38. script.setAttribute('src', src);
  39. script.setAttribute('async', true);
  40. script.setAttribute('defer', true);
  41. head.appendChild(script);
  42. if (script.readyState) { // ie
  43. script.onreadystatechange = function () {
  44. var state = this.readyState;
  45. if (state === 'loaded' || state === 'complete') {
  46. callback();
  47. }
  48. }
  49. } else {
  50. script.onload = function () {
  51. callback();
  52. }
  53. }
  54. }
  55. function getQueryString(name) {
  56. let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  57. let r = window.location.search.substr(1).match(reg);
  58. if (r != null) {
  59. return decodeURIComponent(r[2]);
  60. };
  61. return null;
  62. }
  63. let paypalClientId = getQueryString('paypalClientId');
  64. let src = `https://www.paypal.com/sdk/js?client-id=${paypalClientId}&currency=USD`;
  65. loadAsyncScript(src, () => {
  66. if(isDOMContentLoaded) {
  67. loadPaypal();
  68. } else {
  69. document.addEventListener("DOMContentLoaded", function () {
  70. isDOMContentLoaded = true;
  71. if(!isOnMessage) {
  72. loadPaypal()
  73. }
  74. })
  75. setTimeout(() => {
  76. if(!isOnMessage) {
  77. loadPaypal()
  78. }
  79. }, 600)
  80. }
  81. })
  82. function loadPaypal() {
  83. let amount = getQueryString('amount');
  84. isOnMessage = true;
  85. window.parent.postMessage({ actionType: "iframeLoaded" }, "*");
  86. // window.addEventListener("message", function (event) {
  87. // if (event.data && event.data.actionType) {
  88. // switch (event.data.actionType) {
  89. // case "setAmount":
  90. // amount = event.data.amount;
  91. initPaypal(amount);
  92. // break;
  93. // }
  94. // }
  95. // });
  96. }
  97. function initPaypal(amount) {
  98. let dom = document.getElementById('paypal-button-container');
  99. if(dom.children && dom.children.length) {
  100. empty(dom)
  101. }
  102. paypal
  103. .Buttons({
  104. style: {
  105. layout: 'horizontal',
  106. color: 'blue',
  107. shape: 'pill',
  108. tagline: false,
  109. height: 48
  110. },
  111. // Sets up the transaction when a payment button is clicked
  112. createOrder: function (data, actions) {
  113. return actions.order.create({
  114. purchase_units: [
  115. {
  116. amount: {
  117. value: amount, // Can reference variables or functions. Example: `value: document.getElementById('...').value`
  118. },
  119. },
  120. ],
  121. });
  122. },
  123. // Finalize the transaction after payer approval
  124. onApprove: function (data, actions) {
  125. return actions.order
  126. .capture()
  127. .then(function (orderData) {
  128. // Successful capture! For dev/demo purposes:
  129. console.log(
  130. "Capture result",
  131. orderData,
  132. JSON.stringify(orderData, null, 2)
  133. );
  134. var transaction =
  135. orderData.purchase_units[0].payments
  136. .captures[0];
  137. window.parent.postMessage(
  138. {
  139. actionType: "payCallBack",
  140. orderData: orderData,
  141. transaction: transaction,
  142. },
  143. "*"
  144. );
  145. // When ready to go live, remove the alert and show a success message within this page. For example:
  146. // var element = document.getElementById('paypal-button-container');
  147. // element.innerHTML = '';
  148. // element.innerHTML = '<h3>Thank you for your payment!</h3>';
  149. // Or go to another URL: actions.redirect('thank_you.html');
  150. });
  151. },
  152. })
  153. .render("#paypal-button-container");
  154. }
  155. function empty (e) {
  156. while (e.firstChild) {
  157. e.removeChild (e.firstChild);
  158. }
  159. }
  160. </script>
  161. </body>
  162. </html>