donate.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. $this->settingsOption('switch', 'homepageDonateShowUserHistory', ['label' => 'Show User Donate History']),
  79. ]
  80. ]
  81. ];
  82. return array_merge($homepageInformation, $homepageSettings);
  83. }
  84. public function donateHomepagePermissions($key = null)
  85. {
  86. $permissions = [
  87. 'main' => [
  88. 'enabled' => [
  89. 'homepageDonateEnabled'
  90. ],
  91. 'auth' => [
  92. 'homepageDonateAuth'
  93. ],
  94. 'not_empty' => [
  95. 'homepageDonateMinimum',
  96. 'homepageDonatePublicToken',
  97. 'homepageDonateSecretToken',
  98. 'homepageDonateProductID',
  99. ]
  100. ],
  101. 'history' => [
  102. 'enabled' => [
  103. 'homepageDonateEnabled',
  104. 'homepageDonateShowUserHistory'
  105. ],
  106. 'auth' => [
  107. 'homepageDonateAuth'
  108. ],
  109. 'not_empty' => [
  110. 'homepageDonateMinimum',
  111. 'homepageDonatePublicToken',
  112. 'homepageDonateSecretToken',
  113. 'homepageDonateProductID',
  114. ]
  115. ]
  116. ];
  117. return $this->homepageCheckKeyPermissions($key, $permissions);
  118. }
  119. public function homepageDonateUserHistory()
  120. {
  121. $items = [];
  122. if ($this->homepageItemPermissions($this->donateHomepagePermissions('history'))) {
  123. try {
  124. $stripe = new \Stripe\StripeClient(
  125. trim($this->config['homepageDonateSecretToken'])
  126. );
  127. $history = $stripe->charges->all(['limit' => 100]);
  128. if (count($history) > 0) {
  129. if ($this->user['email']) {
  130. foreach ($history as $charge) {
  131. if (($this->qualifyRequest(0) || (strtolower($charge['billing_details']['email']) == strtolower($this->user['email']))) && $charge['amount_captured'] > 0) {
  132. $items[] = [
  133. 'date' => date('Y-m-d\TH:i:s\Z', $charge['created']),
  134. 'email' => $charge['billing_details']['email'],
  135. 'amount' => $charge['amount_captured'] / 100
  136. ];
  137. }
  138. }
  139. }
  140. }
  141. } catch (\Stripe\Exception\ApiErrorException $e) {
  142. die($this->showHTML('Error', $e->getMessage()));
  143. }
  144. }
  145. $this->setResponse(200, null, $items);
  146. return $items;
  147. }
  148. public function homepageDonateCreateSession($amount = null)
  149. {
  150. $amount = $amount ? $amount * 100 : $this->config['homepageDonateMinimum'];
  151. if ($this->config['homepageDonatePublicToken'] == '' || $this->config['homepageDonateSecretToken'] == '' || $this->config['homepageDonateProductID'] == '') {
  152. $this->setResponse(409, 'Donation Tokens are not setup');
  153. return false;
  154. }
  155. try {
  156. $stripe = new \Stripe\StripeClient(
  157. trim($this->config['homepageDonateSecretToken'])
  158. );
  159. $sessionInfo = [
  160. 'payment_method_types' => ['card'],
  161. 'line_items' => [[
  162. 'price_data' => [
  163. 'product' => $this->config['homepageDonateProductID'],
  164. 'unit_amount' => $amount,
  165. 'currency' => 'usd',
  166. ],
  167. 'quantity' => 1,
  168. ]],
  169. 'mode' => 'payment',
  170. 'success_url' => $this->getServerPath() . 'api/v2/homepage/donate/success',
  171. 'cancel_url' => $this->getServerPath() . 'api/v2/homepage/donate/cancel',
  172. ];
  173. if ($this->user['email'] && stripos($this->user['email'], 'placeholder') == false) {
  174. $sessionInfo = array_merge($sessionInfo, ['customer_email' => $this->user['email']]);
  175. }
  176. $session = $stripe->checkout->sessions->create($sessionInfo);
  177. header('HTTP/1.1 303 See Other');
  178. header('Location: ' . $session->url);
  179. } catch (\Stripe\Exception\ApiErrorException $e) {
  180. $this->setResponse(500, $e->getMessage());
  181. return false;
  182. }
  183. }
  184. public function homepageOrderDonate()
  185. {
  186. if ($this->homepageItemPermissions($this->donateHomepagePermissions('main'))) {
  187. $minimum = $this->config['homepageDonateMinimum'] / 100;
  188. $history = $this->config['homepageDonateShowUserHistory'] ? '<div class="pull-right"><a href="javascript:void(0)" class="toggle-donation-history" data-status="hidden"><i class="fa fa-clock-o"></i></a> </div>' : '';
  189. return '
  190. <script>
  191. $(document).on("keyup", "#custom-donation-amount", function () {
  192. $("#homepage-donation-form").attr("action", "api/v2/homepage/donate?amount=" + $(this).val());
  193. });
  194. </script>
  195. <div id="' . __FUNCTION__ . '">
  196. <div class="panel panel-primary" style="position: static; zoom: 1;">
  197. <div class="panel-heading"> ' . $this->config['homepageDonateCustomizeHeading'] . $history . '</div>
  198. <div class="panel-wrapper collapse in" aria-expanded="true">
  199. <div class="panel-body">
  200. <p>' . $this->config['homepageDonateCustomizeDescription'] . '</p>
  201. <script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
  202. <script src="https://js.stripe.com/v3/"></script>
  203. <form id="homepage-donation-form" action="api/v2/homepage/donate?amount=' . $minimum . '" method="POST" target="_blank">
  204. <div class="input-group m-b-30">
  205. <span class="input-group-addon">$</span>
  206. <input type="number" class="form-control" name="amount" id="custom-donation-amount" placeholder="' . $minimum . '" min="' . $minimum . '"/>
  207. <span class="input-group-btn">
  208. <button class="btn btn-info" type="submit" id="checkout-button" lang="en">Donate</button>
  209. </span>
  210. </div>
  211. </form>
  212. <div class="donation-history hidden"></div>
  213. </div>
  214. </div>
  215. </div>
  216. </div>
  217. ';
  218. }
  219. }
  220. }