Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.54 KB | None | 0 0
  1. <?php require_once 'engine/init.php';
  2. protect_page();
  3. // Import from config:
  4. $pagseguro = $config['pagseguro'];
  5. $paypal = $config['paypal'];
  6. $prices = $config['paypal_prices'];
  7. // Begin processing paypal transaction request
  8. if (empty($_POST) === false) {
  9. $price = intval($_POST['amount']);
  10. if(is_int($price) && $price > 0 && in_array($price, array_keys($prices))) {
  11. $points = $prices[$price];
  12. if ($paypal['debug']) data_dump($_REQUEST, false, "Request");
  13. // Init curl
  14. $ch = curl_init();
  15. // Get token
  16. curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
  17. curl_setopt($ch, CURLOPT_HEADER, false);
  18. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  19. curl_setopt($ch, CURLOPT_POST, true);
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  21. curl_setopt($ch, CURLOPT_USERPWD, $paypal['client_id'].":".$paypal['secret_id']);
  22. curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
  23.  
  24. $result = curl_exec($ch);
  25. $token_json = json_decode($result);
  26. $token = $token_json->access_token;
  27. if ($paypal['debug']) data_dump($token_json, false, "Payment token");
  28.  
  29. $profile_id = mysql_select_single("SELECT `value` FROM `znote_global_storage` WHERE `key`='paypal_profile_id' LIMIT 1;");
  30. if ($profile_id === false) {
  31. // Prepare profile data
  32. $buyer_profile = array(
  33. "name" => "paypalProfile",
  34. "temporary" => false,
  35. "input_fields" => array(
  36. "no_shipping" => 1,
  37. "address_override" => 1
  38. )
  39. );
  40. // Send profile create request
  41. curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payment-experience/web-profiles");
  42. curl_setopt($ch, CURLOPT_HEADER, false);
  43. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  44. curl_setopt($ch, CURLOPT_POST, true);
  45. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  46. curl_setopt($ch, CURLOPT_USERPWD, $paypal['client_id'].":".$paypal['secret_id']);
  47. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($buyer_profile));
  48. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  49. "Authorization: Bearer ".$token."",
  50. "Content-Type: application/json"
  51. ));
  52. $profile_return = json_decode(curl_exec($ch));
  53. //{"id":"XP-4KHA-UYPT-QNCF-7JYC","name":"webProfile","temporary":false,"input_fields":{"no_shipping":1,"address_override":1}}
  54. if ($paypal['debug']) data_dump($profile_return, false, "Profile return data");
  55. $profile_id = (isset($profile_return->id)) ? $profile_return->id : false;
  56. if ($profile_id !== false) {
  57. mysql_insert("INSERT INTO `znote_global_storage` (`key`, `value`) VALUES ('paypal_profile_id', '$profile_id');");
  58. }
  59. } else {
  60. $profile_id = $profile_id['value'];
  61. }
  62. // Prepare payment data
  63. $currency = $paypal['currency'];
  64. $payment = array(
  65. "intent" => "sale",
  66. "payer" => array(
  67. "payment_method" => "paypal"
  68. ),
  69. "transactions" => array(
  70. array(
  71. "amount" => array(
  72. "currency" => $currency,
  73. "total" => $price
  74. ),
  75. "description" => "Shop points on ". $config['site_title'],
  76. "item_list" => array(
  77. "items" => array(
  78. array(
  79. "quantity" => "1",
  80. "name" => $prices[$price]." shop points.",
  81. "price" => $price,
  82. "currency" => $currency
  83. )
  84. )
  85. )
  86. )
  87. ),
  88. "experience_profile_id" => $profile_id,
  89. "redirect_urls" => array(
  90. "return_url" => $paypal['process'],
  91. "cancel_url" => $paypal['failed'],
  92. )
  93. );
  94. if ($paypal['debug']) data_dump($payment, false, "Payment data");
  95. // Send payment request
  96. curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
  97. curl_setopt($ch, CURLOPT_HEADER, false);
  98. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  99. curl_setopt($ch, CURLOPT_POST, true);
  100. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  101. curl_setopt($ch, CURLOPT_USERPWD, $paypal['client_id'].":".$paypal['secret_id']);
  102. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payment));
  103. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  104. "Authorization: Bearer ".$token."",
  105. "Content-Type: application/json"
  106. ));
  107. $result = curl_exec($ch);
  108. $payment_link = json_decode($result);
  109. // Close curl
  110. curl_close($ch);
  111. // Send user to paypal to process payment
  112. if (isset($payment_link->links[1]->href)) {
  113.  
  114. // Log the start of the payment process
  115. // Account ID, payment ID, price, redirect, execute, status
  116. $account_id = (int)$session_user_id;
  117. $payment_id = $payment_link->id;
  118. $payment_state = $payment_link->state;
  119. $time_created = $payment_link->create_time;
  120. mysql_insert("INSERT INTO `znote_paypal` (`account_id`,`payment_id`,`payment_state`,`price`,`currency`,`points`, `time_created`) VALUES ('{$account_id}','{$payment_id}','{$payment_state}','{$price}','{$currency}','{$points}','{$time_created}');");
  121. if ($paypal['debug']) {
  122. data_dump($payment_link, false, "Payment link success");
  123. echo "<p><a target='_BLANK' href='".$payment_link->links[1]->href."'>Click here to proceed to paypal</a></p>";
  124. } else {
  125. header("Location: ".$payment_link->links[1]->href);
  126. exit();
  127. }
  128. } else {
  129. data_dump($payment_link, false, "Payment link ERROR");
  130. }
  131. } else {
  132. data_dump($_REQUEST, false, "Invalid post data.");
  133. }
  134. }
  135. // Render html
  136. include 'layout/overall/header.php';
  137. // PayPal
  138. if ($paypal['enabled']):
  139. ?>
  140. <h1>Buy Points</h1>
  141. <h2>Buy points using Paypal:</h2>
  142. <table id="buypointsTable" class="table table-striped table-hover">
  143. <tr class="yellow">
  144. <th>Price:</th>
  145. <th>Points:</th>
  146. <?php if ($paypal['showBonus']) echo "<th>Bonus:</th>"; ?>
  147. <th>Action:</th>
  148. </tr>
  149. <?php
  150. foreach ($prices as $price => $points):
  151. $discount = calculate_discount(($paypal['points_per_currency'] * $price), $points);
  152. ?>
  153. <tr class="special">
  154. <td><?php echo $price; ?>(<?php echo $paypal['currency']; ?>)</td>
  155. <td><?php echo $points; ?></td>
  156. <?php if ($paypal['showBonus']) echo '<td>'. $discount .' bonus</td>'; ?>
  157. <td>
  158. <form action="" method="POST">
  159. <input type="hidden" name="amount" value="<?php echo $price; ?>">
  160. <input type="submit" value=" PURCHASE ">
  161. </form>
  162. </td>
  163. </tr>
  164. <?php
  165. endforeach;
  166. ?>
  167. </table>
  168. <?php
  169. endif;
  170. // PagseGuro
  171. if ($config['pagseguro']['enabled'] == true):
  172. ?>
  173. <h2>Buy points using Pagseguro:</h2>
  174. <form target="pagseguro" action="https://<?=$pagseguro['urls']['www']?>/checkout/checkout.jhtml" method="post">
  175. <input type="hidden" name="email_cobranca" value="<?=$pagseguro['email']?>">
  176. <input type="hidden" name="tipo" value="CP">
  177. <input type="hidden" name="moeda" value="<?=$pagseguro['currency']?>">
  178. <input type="hidden" name="ref_transacao" value="<?php echo (int)$session_user_id; ?>">
  179. <input type="hidden" name="item_id_1" value="1">
  180. <input type="hidden" name="item_descr_1" value="<?=$pagseguro['product_name']?>">
  181. <input type="number" name="item_quant_1" min="1" step="4" value="1">
  182. <input type="hidden" name="item_peso_1" value="0">
  183. <input type="hidden" name="item_valor_1" value="<?=$pagseguro['price']?>">
  184. <input type="submit" value=" PURCHASE ">
  185. </form>
  186. <br>
  187. <?php
  188. endif;
  189. // PayGol
  190. if ($config['paygol']['enabled'] == true):
  191. $paygol = $config['paygol'];
  192. ?>
  193. <!-- PayGol Form using Post method -->
  194. <h2>Buy points using Paygol:</h2>
  195. <p><?php echo $paygol['price'] ." ". $paygol['currency'] ."~ for ". $paygol['points'] ." points:"; ?></p>
  196. <form name="pg_frm" method="post" action="http://www.paygol.com/micropayment/paynow" >
  197. <input type="hidden" name="pg_serviceid" value="<?php echo $paygol['serviceID']; ?>">
  198. <input type="hidden" name="pg_currency" value="<?php echo $paygol['currency']; ?>">
  199. <input type="hidden" name="pg_name" value="<?php echo $paygol['name']; ?>">
  200. <input type="hidden" name="pg_custom" value="<?php echo $session_user_id; ?>">
  201. <input type="hidden" name="pg_price" value="<?php echo $paygol['price']; ?>">
  202. <input type="hidden" name="pg_return_url" value="<?php echo $paygol['returnURL']; ?>">
  203. <input type="hidden" name="pg_cancel_url" value="<?php echo $paygol['cancelURL']; ?>">
  204. <input type="image" name="pg_button" src="http://www.paygol.com/micropayment/img/buttons/150/black_en_pbm.png" border="0" alt="Make payments with PayGol: the easiest way!" title="Make payments with PayGol: the easiest way!">
  205. </form>
  206. <?php
  207. endif;
  208. if (!$config['paypal']['enabled'] && !$config['paygol']['enabled'] && !$config['pagseguro']['enabled']):
  209. ?>
  210. <h1>Buy Points system disabled.</h1>
  211. <p>Sorry, this functionality is disabled.</p>
  212. <?php
  213. endif;
  214. include 'layout/overall/footer.php';
  215. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement