paypal.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. h2 {
  13. margin: 0;
  14. border: 0;
  15. padding: 0;
  16. display: block;
  17. height: 100%;
  18. width: 100%;
  19. background: white;
  20. color: black;
  21. overflow: hidden;
  22. }
  23. h2 {
  24. height: 50px;
  25. font-size: 20px;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <!-- Include the PayPal JavaScript SDK; replace "test" with your own sandbox Business account app client ID -->
  31. <script src="https://www.paypal.com/sdk/js?client-id=ASn7k0zqyS5AWYikVSfmamR-RFpjyU_QFJWSxOHHoWE04-RgHNO6nahn0GyHUaUAEBxj-aKgtSrq4O4G&currency=USD"></script>
  32. <!-- Set up a container element for the button -->
  33. <div id="paypal-button-container"></div>
  34. <script>
  35. // let amount = window.location.search.split('=')[1];
  36. let amount = 0;
  37. document.addEventListener("DOMContentLoaded", function () {
  38. window.parent.postMessage({ actionType: "iframeLoaded" }, "*");
  39. window.addEventListener("message", function (event) {
  40. if (event.data && event.data.actionType) {
  41. switch (event.data.actionType) {
  42. case "setAmount":
  43. amount = event.data.amount;
  44. initPaypal(amount);
  45. break;
  46. }
  47. }
  48. });
  49. });
  50. function initPaypal(amount) {
  51. let dom = document.getElementById('paypal-button-container');
  52. if(dom.children && dom.children.length) {
  53. empty(dom)
  54. }
  55. paypal
  56. .Buttons({
  57. style: {
  58. layout: 'horizontal',
  59. color: 'blue',
  60. shape: 'pill',
  61. tagline: false,
  62. height: 48
  63. },
  64. // Sets up the transaction when a payment button is clicked
  65. createOrder: function (data, actions) {
  66. return actions.order.create({
  67. purchase_units: [
  68. {
  69. amount: {
  70. value: amount, // Can reference variables or functions. Example: `value: document.getElementById('...').value`
  71. },
  72. },
  73. ],
  74. });
  75. },
  76. // Finalize the transaction after payer approval
  77. onApprove: function (data, actions) {
  78. return actions.order
  79. .capture()
  80. .then(function (orderData) {
  81. // Successful capture! For dev/demo purposes:
  82. console.log(
  83. "Capture result",
  84. orderData,
  85. JSON.stringify(orderData, null, 2)
  86. );
  87. var transaction =
  88. orderData.purchase_units[0].payments
  89. .captures[0];
  90. amount = 0;
  91. window.parent.postMessage(
  92. {
  93. actionType: "payCallBack",
  94. orderData: orderData,
  95. transaction: transaction,
  96. },
  97. "*"
  98. );
  99. // When ready to go live, remove the alert and show a success message within this page. For example:
  100. // var element = document.getElementById('paypal-button-container');
  101. // element.innerHTML = '';
  102. // element.innerHTML = '<h3>Thank you for your payment!</h3>';
  103. // Or go to another URL: actions.redirect('thank_you.html');
  104. });
  105. },
  106. })
  107. .render("#paypal-button-container");
  108. }
  109. function empty (e) {
  110. while (e.firstChild) {
  111. e.removeChild (e.firstChild);
  112. }
  113. }
  114. </script>
  115. </body>
  116. </html>