paypal.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. function loadAsyncScript(src, callback = function () {}) { // 同步加载js
  30. const head = document.getElementsByTagName('head')[0];
  31. const script = document.createElement('script');
  32. script.setAttribute('type', 'text/javascript');
  33. script.setAttribute('src', src);
  34. script.setAttribute('async', true);
  35. script.setAttribute('defer', true);
  36. head.appendChild(script);
  37. if (script.readyState) { // ie
  38. script.onreadystatechange = function () {
  39. var state = this.readyState;
  40. if (state === 'loaded' || state === 'complete') {
  41. callback();
  42. }
  43. }
  44. } else {
  45. script.onload = function () {
  46. callback();
  47. }
  48. }
  49. }
  50. function getQueryString(name) {
  51. let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  52. let r = window.location.search.substr(1).match(reg);
  53. if (r != null) {
  54. return decodeURIComponent(r[2]);
  55. };
  56. return null;
  57. }
  58. let paypalClientId = getQueryString('paypalClientId');
  59. let src = `https://www.paypal.com/sdk/js?client-id=${paypalClientId}&currency=USD`;
  60. loadAsyncScript(src, () => {
  61. let amount = 0;
  62. window.parent.postMessage({ actionType: "iframeLoaded" }, "*");
  63. window.addEventListener("message", function (event) {
  64. if (event.data && event.data.actionType) {
  65. switch (event.data.actionType) {
  66. case "setAmount":
  67. amount = event.data.amount;
  68. initPaypal(amount);
  69. break;
  70. }
  71. }
  72. });
  73. })
  74. function initPaypal(amount) {
  75. let dom = document.getElementById('paypal-button-container');
  76. if(dom.children && dom.children.length) {
  77. empty(dom)
  78. }
  79. paypal
  80. .Buttons({
  81. style: {
  82. layout: 'horizontal',
  83. color: 'blue',
  84. shape: 'pill',
  85. tagline: false,
  86. height: 48
  87. },
  88. // Sets up the transaction when a payment button is clicked
  89. createOrder: function (data, actions) {
  90. return actions.order.create({
  91. purchase_units: [
  92. {
  93. amount: {
  94. value: amount, // Can reference variables or functions. Example: `value: document.getElementById('...').value`
  95. },
  96. },
  97. ],
  98. });
  99. },
  100. // Finalize the transaction after payer approval
  101. onApprove: function (data, actions) {
  102. return actions.order
  103. .capture()
  104. .then(function (orderData) {
  105. // Successful capture! For dev/demo purposes:
  106. console.log(
  107. "Capture result",
  108. orderData,
  109. JSON.stringify(orderData, null, 2)
  110. );
  111. var transaction =
  112. orderData.purchase_units[0].payments
  113. .captures[0];
  114. amount = 0;
  115. window.parent.postMessage(
  116. {
  117. actionType: "payCallBack",
  118. orderData: orderData,
  119. transaction: transaction,
  120. },
  121. "*"
  122. );
  123. // When ready to go live, remove the alert and show a success message within this page. For example:
  124. // var element = document.getElementById('paypal-button-container');
  125. // element.innerHTML = '';
  126. // element.innerHTML = '<h3>Thank you for your payment!</h3>';
  127. // Or go to another URL: actions.redirect('thank_you.html');
  128. });
  129. },
  130. })
  131. .render("#paypal-button-container");
  132. }
  133. function empty (e) {
  134. while (e.firstChild) {
  135. e.removeChild (e.firstChild);
  136. }
  137. }
  138. </script>
  139. </body>
  140. </html>