paypal.html 5.5 KB

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