donate.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. trait DonateHomepageItem
  3. {
  4. public function donateSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'Donate',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/donate.png',
  10. 'category' => 'Requests',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'About' => [
  20. $this->settingsOption('about', 'Donations', ['about' => 'This item allows you to use Stripe to accept donations on the homepage']),
  21. ],
  22. 'Setup' => [
  23. $this->settingsOption('html', null, ['label' => 'Instructions', 'override' => 12, 'html' => '
  24. <div class="panel panel-default">
  25. <div class="panel-heading">
  26. <a href="https://dashboard.stripe.com//" target="_blank"><span class="label label-info m-l-5">Visit Stripe Site</span></a>
  27. </div>
  28. <div class="panel-wrapper collapse in">
  29. <div class="panel-body">
  30. <ul class="list-icons">
  31. <li lang="en"><i class="fa fa-caret-right text-info"></i> Create or Login if you already have an account</li>
  32. <li lang="en"><i class="fa fa-caret-right text-info"></i> Goto products and click [Add Product]</li>
  33. <li lang="en"><i class="fa fa-caret-right text-info"></i> Name the product anything you like</li>
  34. <li lang="en"><i class="fa fa-caret-right text-info"></i> Make sure the product has standard pricing</li>
  35. <li lang="en"><i class="fa fa-caret-right text-info"></i> Also make sure that the product is set to <code>One Time</code></li>
  36. <li lang="en"><i class="fa fa-caret-right text-info"></i> Set the pricing to the minimum price i.e. 1 USD</li>
  37. <li lang="en"><i class="fa fa-caret-right text-info"></i> Click <code>Save Product</code></li>
  38. <li lang="en"><i class="fa fa-caret-right text-info"></i> Click The Product you just created</li>
  39. <li lang="en"><i class="fa fa-caret-right text-info"></i> Copy <code>ID</code> value</li>
  40. <li lang="en"><i class="fa fa-caret-right text-info"></i> Click <code>Developers</code></li>
  41. <li lang="en"><i class="fa fa-caret-right text-info"></i> Click <code>API Keys</code></li>
  42. <li lang="en"><i class="fa fa-caret-right text-info"></i> Copy both <code>Publishable key</code> and <code>Secret key</code></li>
  43. </ul>
  44. </div>
  45. </div>
  46. </div>
  47. ']
  48. ),
  49. ],
  50. 'Enable' => [
  51. $this->settingsOption('enable', 'homepageDonateEnabled'),
  52. $this->settingsOption('auth', 'homepageDonateAuth'),
  53. ],
  54. 'Connection' => [
  55. $this->settingsOption('input', 'homepageDonatePublicToken', ['label' => 'Public Token']),
  56. $this->settingsOption('token', 'homepageDonateSecretToken', ['label' => 'Secret Token']),
  57. $this->settingsOption('input', 'homepageDonateProductID', ['label' => 'Product ID']),
  58. ],
  59. 'Customize' => [
  60. $this->settingsOption('input', 'homepageDonateCustomizeHeading', ['label' => 'Heading']),
  61. $this->settingsOption('code-editor', 'homepageDonateCustomizeDescription', ['label' => 'Description', 'mode' => 'html']),
  62. $this->settingsOption('select', 'homepageDonateMinimum',
  63. ['label' => 'Minimum',
  64. 'options' => [
  65. ['name' => '1 USD', 'value' => '100'],
  66. ['name' => '2 USD', 'value' => '200'],
  67. ['name' => '3 USD', 'value' => '300'],
  68. ['name' => '4 USD', 'value' => '400'],
  69. ['name' => '5 USD', 'value' => '500'],
  70. ['name' => '10 USD', 'value' => '1000'],
  71. ['name' => '20 USD', 'value' => '2000'],
  72. ['name' => '25 USD', 'value' => '2500'],
  73. ['name' => '50 USD', 'value' => '5000'],
  74. ['name' => '75 USD', 'value' => '7500'],
  75. ['name' => '100 USD', 'value' => '10000'],
  76. ]
  77. ]),
  78. ]
  79. ]
  80. ];
  81. return array_merge($homepageInformation, $homepageSettings);
  82. }
  83. public function donateHomepagePermissions($key = null)
  84. {
  85. $permissions = [
  86. 'main' => [
  87. 'enabled' => [
  88. 'homepageDonateEnabled'
  89. ],
  90. 'auth' => [
  91. 'homepageDonateAuth'
  92. ],
  93. 'not_empty' => [
  94. 'homepageDonateMinimum',
  95. 'homepageDonatePublicToken',
  96. 'homepageDonateSecretToken',
  97. 'homepageDonateProductID',
  98. ]
  99. ]
  100. ];
  101. return $this->homepageCheckKeyPermissions($key, $permissions);
  102. }
  103. public function homepageDonateCreateSession($amount = null)
  104. {
  105. $amount = $amount ? $amount * 100 : $this->config['homepageDonateMinimum'];
  106. if ($this->config['homepageDonatePublicToken'] == '' || $this->config['homepageDonateSecretToken'] == '' || $this->config['homepageDonateProductID'] == '') {
  107. $this->setResponse(409, 'Donation Tokens are not setup');
  108. return false;
  109. }
  110. try {
  111. $stripe = new \Stripe\StripeClient(
  112. trim($this->config['homepageDonateSecretToken'])
  113. );
  114. $sessionInfo = [
  115. 'payment_method_types' => ['card'],
  116. 'line_items' => [[
  117. 'price_data' => [
  118. 'product' => $this->config['homepageDonateProductID'],
  119. 'unit_amount' => $amount,
  120. 'currency' => 'usd',
  121. ],
  122. 'quantity' => 1,
  123. ]],
  124. 'mode' => 'payment',
  125. 'success_url' => $this->getServerPath() . 'api/v2/homepage/donate/success',
  126. 'cancel_url' => $this->getServerPath() . 'api/v2/homepage/donate/error',
  127. ];
  128. if ($this->user['email'] && stripos($this->user['email'], 'placeholder') == false) {
  129. $sessionInfo = array_merge($sessionInfo, ['customer_email' => $this->user['email']]);
  130. }
  131. $session = $stripe->checkout->sessions->create($sessionInfo);
  132. header('HTTP/1.1 303 See Other');
  133. header('Location: ' . $session->url);
  134. } catch (\Stripe\Exception\ApiErrorException $e) {
  135. die($this->showHTML('Error', $e->getMessage()));
  136. }
  137. }
  138. public function homepageOrderDonate()
  139. {
  140. if ($this->homepageItemPermissions($this->donateHomepagePermissions('main'))) {
  141. $minimum = $this->config['homepageDonateMinimum'] / 100;
  142. return '
  143. <script>
  144. $(document).on("keyup", "#custom-donation-amount", function () {
  145. $("#homepage-donation-form").attr("action", "api/v2/homepage/donate?amount=" + $(this).val());
  146. });
  147. </script>
  148. <div id="' . __FUNCTION__ . '">
  149. <div class="panel panel-primary" style="position: static; zoom: 1;">
  150. <div class="panel-heading"> ' . $this->config['homepageDonateCustomizeHeading'] . '</div>
  151. <div class="panel-wrapper collapse in" aria-expanded="true">
  152. <div class="panel-body">
  153. <p>' . $this->config['homepageDonateCustomizeDescription'] . '</p>
  154. <script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
  155. <script src="https://js.stripe.com/v3/"></script>
  156. <form id="homepage-donation-form" action="api/v2/homepage/donate?amount=' . $minimum . '" method="POST" target="_blank">
  157. <div class="input-group m-b-30">
  158. <span class="input-group-addon">$</span>
  159. <input type="number" class="form-control" name="amount" id="custom-donation-amount" placeholder="' . $minimum . '" min="' . $minimum . '"/>
  160. <span class="input-group-btn">
  161. <button class="btn btn-info" type="submit" id="checkout-button" lang="en">Donate</button>
  162. </span>
  163. </div>
  164. </form>
  165. </div>
  166. </div>
  167. </div>
  168. </div>
  169. ';
  170. }
  171. }
  172. }