Advertisement
Guest User

Untitled

a guest
Apr 10th, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.98 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @package ClubTrips_Payments
  4.  * @version 1.0
  5.  */
  6. /*
  7. Plugin Name: Club-Trips - Payments
  8. Description: This allows you to take payments from your website for trips
  9. Author: Victor Macko
  10. Version: 1.0
  11. Author URI: http://www.victor.com.au
  12. */
  13.  
  14. function clubtrips_makepayment($args = array()) {
  15.    
  16.     $account_bank = get_option('clubtrips_account_bank');
  17.     $account_name = get_option('clubtrips_account_name');
  18.     $account_bsb = get_option('clubtrips_account_bsb');
  19.     $account_number = get_option('clubtrips_account_number');
  20.    
  21.     $paypal_email = get_option('clubtrips_paypal_email');
  22.     $paypal_additional_percent = get_option('clubtrips_paypal_additional_percent');
  23.    
  24.     $siteurl = get_option('siteurl');
  25.     $dir = basename(dirname(__FILE__));
  26.    
  27.     if(!get_option('clubtrips_url') || !get_option('clubtrips_username') || !get_option('clubtrips_password')) {
  28.         return "<p>Oops! The ClubTrips Pluggin isn't setup just yet - your site administrator needs to fill in the settings page to get it going!</p>";
  29.     }
  30.    
  31.    
  32.     ob_start();
  33.    
  34.     // Use nonce for verification
  35.     wp_nonce_field( plugin_basename( __FILE__ ), 'member_noncename' );
  36. ?>
  37.  
  38. <script type="text/javascript">
  39.    
  40. jQuery(function($) {
  41.     var $form = $('form.clubtrips');
  42.    
  43.     $form.validate({
  44.         'submitHandler': function(form) {
  45.             $thisForm = $(form);
  46.             $thisForm.find('#submitButton').prop('disabled', true).after('<img src="<?php echo $siteurl ?>/wp-content/plugins/<?php echo $dir ?>/loading.gif" class="loading" style="padding-left: 5px;" />');
  47.             $.post($form.prop('action'), $(form).serialize(), function(data) {
  48.                 if(data.result == 'ok') {
  49.                     if(data.data) {
  50.                         var text = '<div class="clubtrips-tx-complete"><p><strong>Thankyou</strong>, your transaction has been recorded. Your reference for this transaction is <strong>' + data.data.transactionReference + '</strong>. ';
  51.                         if($form.find('input[name=paymentMethod]:checked').val() == 'direct-debit') {
  52.                             text += 'Now all that\'s left to do is visit your internet banking site, and use the following reference number in the transaction to us.</p><p>Our bank account details are:<br />Bank Account <strong><?php echo $account_name ?> (<?php echo $account_bank ?>)</strong>, BSB <strong><?php echo $account_bsb ?></strong>, Account No. <strong><?php echo $account_number ?></strong><br />Transaction Reference: <strong>' + data.data.transactionReference + '</strong>';
  53.                         } else {
  54.                             text += 'Now all that\'s left to do is click the following link to PayPal, to complete the transaction.';
  55.                         }
  56.                         text += '</p></div>';
  57.  
  58.                         $thisForm.hide().after(text);
  59.                     } else {
  60.                         alert('Oops - it looks like something\'s gone wrong ... please try again and let us know if it continues to happen.');
  61.                         $thisForm.find('#submitButton').prop('disabled', false);
  62.                         $thisForm.find('img.loading').remove();
  63.                     }
  64.                 } else if(data.result == 'fields-not-completed') {
  65.                     alert('Oops - you need to complete all the fields to process your transaction! Please complete as much as you can, and try again.');
  66.                     $thisForm.find('#submitButton').prop('disabled', false);
  67.                     $thisForm.find('img.loading').remove();
  68.                 } else {
  69.                     alert('Oops - it looks like something\'s gone wrong ... please try again and let us know if it continues to happen.');
  70.                     $thisForm.find('#submitButton').prop('disabled', false);
  71.                     $thisForm.find('img.loading').remove();
  72.                 }
  73.             }, 'json');
  74.             return false;
  75.         }
  76.     });
  77.    
  78.     $form.find('input[name=paymentMethod]').change(function() {
  79.         $form.find('div.direct-debit-message').toggle(this.checked && this.value == 'direct-debit');
  80.         $form.find('div.credit-card-message').toggle(this.checked && this.value == 'paypal');
  81.        
  82.         if(this.value == 'paypal' && this.checked) {
  83.             var amount = parseFloat($('input.amount').val());
  84.             var surchargeAmount = 0;
  85.             var paypalSurchargePercent = <?php echo $paypal_additional_percent ? $paypal_additional_percent : 0 ?>;
  86.             if(this.value == 'paypal' && this.checked) {
  87.                 surchargeAmount = amount * (paypalSurchargePercent/100);
  88.             }
  89.             $('span.amountSubTotal').html(' (+ $' + surchargeAmount.toFixed(2) + ' surcharge = $' + (amount + surchargeAmount).toFixed(2) + ')');
  90.  
  91.             $('.paypalSurchargeAmount').html((surchargeAmount > 0 ? '($' + surchargeAmount.toFixed(2) + ')' : '-'));
  92.         }
  93.     });
  94.     $form.find('input[name=paymentMethod]:checked').change();
  95. });
  96.  
  97. </script>
  98. <!--
  99. <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
  100.     Click <input type="image" src="./images/site/paypal_button.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!"> to pay
  101.    
  102.     <input type="hidden" name="cmd" value="_xclick">
  103.     <input type="hidden" name="business" value="<?php echo $paypal_email ?>">
  104.     <input type="hidden" name="item_name" value="SUSS Payment - # 4513-NM-5806">
  105.     <input type="hidden" name="item_number" value="4513-NM-5806">
  106.    
  107.     <input type="hidden" name="currency_code" value="AUD">
  108.     <input type="hidden" name="amount" value="10.30">
  109.    
  110.     <input type="hidden" name="cancel_return" value="http://www.suss.org.au/payments.php?paid_cancel&amp;t=4513-NM-5806">
  111.     <input type="hidden" name="return" value="http://www.suss.org.au/payments.php?complete">
  112.    
  113.     <input type="hidden" name="no_shipping" value="1">
  114.     <input type="hidden" name="no_note" value="1">
  115.     <input type="hidden" name="invoice" value="4513-NM-5806">
  116.     <input type="hidden" name="email" value="asd">
  117. </form>
  118. -->
  119.  
  120.  
  121. <h3>Make a payment</h3>
  122. <p>To make a payment, <strong>please complete the following form and then click submit</strong>. You will then be directed to pay via your preferred payment method with a <strong>transaction reference</strong> so we know who's paying for what!<br />If you are paying on behalf of someone else, please complete each person's payment in a separate transaction, with their details completed (feel free to make a note than you're paying for them in the details box if you like).</p>
  123. <form action="<?php echo admin_url('admin-ajax.php') ?>?action=ct_make_payment" method="post" class="clubtrips">
  124. <?php wp_nonce_field( plugin_basename( __FILE__ ), 'ct_nonce' ); ?>
  125. <?php if(!empty($args['accountid'])) { ?><input type="hidden" name="accountId" value="<?php echo $args['accountid'] ?>" /><? } ?>
  126. <table class="form-table">
  127. <tbody>
  128. <tr>
  129.     <th><label for="name">Your Name</label></th>
  130.     <td><input type="text" name="name" id="name" value="" class="regular-text required"></td>
  131. </tr>
  132. <tr>
  133.     <th><label for="memberNumber">Member Number</label></th>
  134.     <td><input type="text" name="memberNumber" id="memberNumber" value="" class="regular-text"></td>
  135. </tr>
  136. <tr>
  137.     <th><label for="first_name">Contact Number</label></th>
  138.     <td><input type="text" name="contactNumber" id="contactNumber" value="" class="regular-text required"></td>
  139. </tr>
  140. <tr>
  141.     <th><label for="last_name">Email</label></th>
  142.     <td><input type="text" name="email" id="email" value="" class="regular-text required"></td>
  143. </tr>
  144.  
  145. <tr>
  146.     <th><label for="details">Payment Method</label></th>
  147.     <td>
  148.         <label><input type="radio" name="paymentMethod" value="direct-debit" checked="checked" /> Direct Debt</label><br />
  149.         <div class="direct-debit-message">You will be given a reference number to include in your bank transfer so we can tell who's given us money, and for what.</div>
  150. <!--        <label><input type="radio" name="paymentMethod" value="paypal" /> PayPal or Credit Card</label>
  151.         <div class="credit-card-message">You will be directed to PayPal to complete the transaction. Note: a <strong><?php echo $paypal_additional_percent ?>% charge <span class="paypalSurchargeAmount"></span></strong> applies to all credit-card &amp; PayPal transactions.</div>  -->
  152.     </td>
  153. </tr>
  154. <tr>
  155.     <th><label for="what">What are you paying for?</label></th>
  156.     <td><?php if(!empty($args['payingfor'])) { echo $args['payingfor']; ?><input type="hidden" name="payingFor" value="<?php echo $args['payingfor'] ?>" /><?php } else { ?><select name="payingFor" id="payingFor"><option>Trips</option><option>Membership</option><option>Social</option><option>Merchandise</option></select><?php } ?></td>
  157. </tr>
  158. <tr>
  159.     <th><label for="nickname">Amount</label></th>
  160.     <td>$ <?php if(!empty($args['amount'])) { echo number_format($args['amount'], 2); ?><input type="hidden" name="amount" value="<?php echo $args['amount'] ?>" class="amount" /><?php } else { ?><input type="text" name="amount" id="amount" value="" class="regular-text"><?php } ?><span class="amountSubTotal"></span></td>
  161. </tr>
  162. <tr>
  163.     <th><label for="details">Additional Details / Comments</label></th>
  164.     <td><textarea name="comment" id="details" class="regular-text"></textarea></td>
  165. </tr>
  166. <tr>
  167.     <th></th>
  168.     <td><input type="submit" id="submitButton" value="Submit details" /></td>
  169. </tr>
  170. </tbody></table>
  171. </form>
  172.  
  173. <?php  
  174.     $html = ob_get_clean();
  175.  
  176.     return $html;
  177. }
  178.  
  179. add_shortcode( 'ct-make-payment', 'clubtrips_makepayment' );
  180.  
  181. function clubtrips_process_payment() {
  182.     if ( !isset($_POST['ct_nonce']) || !wp_verify_nonce( $_POST['ct_nonce'], plugin_basename( __FILE__ ) ) ) {
  183.         echo json_encode(array('status' => 200, 'result' => 'form-error'));
  184.         exit;
  185.     }
  186.    
  187.     // get vars
  188.     $details = array(
  189.         'name' => isset($_POST['name']) ? stripslashes($_POST['name']) : null,
  190.         'memberNumber' => isset($_POST['memberNumber']) ? stripslashes($_POST['memberNumber']) : null,
  191.         'contactNumber' => isset($_POST['contactNumber']) ? stripslashes($_POST['contactNumber']) : null,
  192.         'email' => isset($_POST['email']) ? stripslashes($_POST['email']) : null,
  193.         'amount' => isset($_POST['amount']) ? stripslashes($_POST['amount']) : null,
  194.         'comment' => isset($_POST['comment']) ? stripslashes($_POST['description']) : null,
  195.         'payingFor' => isset($_POST['payingFor']) ? stripslashes($_POST['payingFor']) : null,
  196.         'paymentMethod' => isset($_POST['paymentMethod']) ? stripslashes($_POST['payment_method']) : null,
  197.        
  198.         'distributions' => array()
  199.     );
  200.    
  201.     if(isset($_POST['accountId']) && !empty($_POST['accountId'])) {
  202.         $details['distributions'][] = array('account' => stripslashes($_POST['accountId']), 'amount' => $details['amount']);
  203.     }
  204.    
  205.     // if paying via paypal, add the paypal account ID
  206.     if($details['paymentMethod'] == 'paypal') {
  207.         $paypal_additional_percent = get_option('clubtrips_paypal_additional_percent');
  208.         $paypal_account_id = get_option('clubtrips_paypal_account_id');
  209.        
  210.         if($paypal_account_id && $paypal_additional_percent > 0) {
  211.             $details['distributions'][] = array('account' => $paypal_account_id, 'amount' => $details['amount'] * $paypal_additional_percent);
  212.         }
  213.     }
  214.    
  215.     if(empty($details['name']) || empty($details['contactNumber']) ||
  216.             empty($details['email']) || empty($details['amount']) || empty($details['payingFor'])) {
  217.         echo json_encode(array('status' => 200, 'result' => 'fields-not-completed'));
  218.         exit;
  219.     }
  220.    
  221.     $ch = curl_init();
  222.     $timeout = 5;
  223.     curl_setopt($ch, CURLOPT_URL, get_option('clubtrips_url') . '/api/transactions/add.json');
  224.     curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  225.     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,$timeout);
  226.     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  227.     curl_setopt($ch, CURLOPT_USERPWD, get_option('clubtrips_username') . ':' . get_option('clubtrips_password'));
  228.     //curl_setopt($ch, CURLOPT_C, true);
  229.     //curl_setopt($ch, CURLOPT_POST, true);
  230.     curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('tx' => $details)));
  231.     $data = curl_exec($ch);
  232.     curl_close($ch);
  233.    
  234.     // get receipt / transaction #
  235.     $result = json_decode($data);
  236.    
  237.     // send details back to user
  238.     echo json_encode(array('status' => 200, 'result' => 'ok', 'data' => $result));
  239.    
  240.     exit;
  241. }
  242.  
  243. add_action('wp_ajax_ct_make_payment', 'clubtrips_process_payment');
  244. add_action('wp_ajax_nopriv_ct_make_payment', 'clubtrips_process_payment'); // need this to serve non logged in users
  245.  
  246. function clubtrips_addJs() {
  247.    
  248.     //$siteurl = get_option('siteurl');
  249.     //$dir = basename(dirname(__FILE__));
  250.    
  251.     wp_register_script('jquery.validate', 'https://ajax.aspnetcdn.com/ajax/jquery.validate/1.8.1/jquery.validate.min.js', array('jquery'));
  252.    
  253.     wp_register_script('akismet.js', plugin_dir_url(__FILE__) . 'clubtrips.js', array('jquery', 'jquery.validate'));
  254.     wp_enqueue_script('akismet.js');
  255. }
  256.  
  257. add_action('init', 'clubtrips_addJs');
  258.  
  259. add_action('admin_menu', 'clubtrips_settings_menu');
  260.  
  261. function clubtrips_settings_menu() {
  262.     // Add a new submenu under Settings:
  263.     add_options_page(__('ClubTrips Settings','menu-ct'), __('ClubTrips Settings','menu-ct'), 'manage_options', 'ct_settings', 'clubtrips_settings_page');
  264. }
  265.  
  266. function clubtrips_settings_page() {
  267.     //must check that the user has the required capability
  268.     if (!current_user_can('manage_options'))
  269.     {
  270.         wp_die( __('You do not have sufficient permissions to access this page.') );
  271.     }
  272.  
  273.     $url = get_option('clubtrips_url');
  274.     $username = get_option('clubtrips_username');
  275.     $password = get_option('clubtrips_password');
  276.    
  277.    
  278.     $account_bank = get_option('clubtrips_account_bank');
  279.     $account_name = get_option('clubtrips_account_name');
  280.     $account_bsb = get_option('clubtrips_account_bsb');
  281.     $account_number = get_option('clubtrips_account_number');
  282.    
  283.     $paypal_email = get_option('clubtrips_paypal_email');
  284.     $paypal_account_id = get_option('clubtrips_paypal_account_id');
  285.     $paypal_additional_percent = get_option('clubtrips_paypal_additional_percent');
  286.    
  287.    
  288.     if(count($_POST) > 0) {
  289.         if(isset($_POST['url'])) {
  290.             $url = stripslashes($_POST['url']);
  291.         }
  292.         if(isset($_POST['username'])) {
  293.             $username = stripslashes($_POST['username']);
  294.         }
  295.         if(isset($_POST['password']) && !empty($_POST['password'])) {
  296.             $password = stripslashes($_POST['password']);
  297.         }
  298.        
  299.         if(isset($_POST['account_bank'])) {
  300.             $account_bank = stripslashes($_POST['account_bank']);
  301.         }
  302.         if(isset($_POST['account_name'])) {
  303.             $account_name = stripslashes($_POST['account_name']);
  304.         }
  305.         if(isset($_POST['account_bsb'])) {
  306.             $account_bsb = stripslashes($_POST['account_bsb']);
  307.         }
  308.         if(isset($_POST['account_number'])) {
  309.             $account_number = stripslashes($_POST['account_number']);
  310.         }
  311.        
  312.         if(isset($_POST['paypal_email'])) {
  313.             $paypal_email = stripslashes($_POST['paypal_email']);
  314.         }
  315.         if(isset($_POST['paypal_account_id'])) {
  316.             $paypal_account_id = stripslashes($_POST['paypal_account_id']);
  317.         }
  318.         if(isset($_POST['paypal_additional_percent'])) {
  319.             $paypal_additional_percent = stripslashes($_POST['paypal_additional_percent']);
  320.         }
  321.        
  322.         // save settings
  323.         update_option('clubtrips_url', $url);
  324.         update_option('clubtrips_username', $username);
  325.         update_option('clubtrips_password', $password);
  326.        
  327.         update_option('clubtrips_account_bank', $account_bank);
  328.         update_option('clubtrips_account_name', $account_name);
  329.         update_option('clubtrips_account_bsb', $account_bsb);
  330.         update_option('clubtrips_account_number', $account_number);
  331.        
  332.         update_option('clubtrips_paypal_email', $paypal_email);
  333.         update_option('clubtrips_paypal_account_id', $paypal_account_id);
  334.         update_option('clubtrips_paypal_additional_percent', $paypal_additional_percent);
  335.     }
  336.  
  337.     // get list of accounts to select from when selecting paypal account
  338. ?>
  339.  
  340. <div class="wrap">
  341.  
  342. <h2>ClubTrips Settings</h2>
  343.  
  344. <form name="form" method="post" action="">
  345. <table class="form-table">
  346. <tr valign="top">
  347.     <th scope="row"><label for="url"><?php _e("ClubTrips URL:", 'url' ); ?></label></th>
  348.     <td><input name="url" type="text" id="url" value="<?php echo $url ?>" class="regular-text code"></td>
  349. </tr>
  350. <tr valign="top">
  351.     <th scope="row"><label for="username"><?php _e("Username:", 'username' ); ?></label></th>
  352.     <td><input name="username" type="text" id="username" value="<?php echo $username ?>" class="regular-text"></td>
  353. </tr>
  354. <tr valign="top">
  355.     <th scope="row"><label for="password"><?php _e("Password:", 'password' ); ?></label></th>
  356.     <td><input name="password" type="password" id="password" value="" class="regular-text"></td>
  357. </tr>
  358. </table>
  359.  
  360. <h3>Your bank account details</h3>
  361. <p>These are shown to your members when making a deposit via Direct Debit.</p>
  362. <table class="form-table">
  363. <tr valign="top">
  364.     <th scope="row"><label for="account_bank"><?php _e("Bank:", 'account_name' ); ?></label></th>
  365.     <td><input name="account_bank" type="text" id="account_bank" value="<?php echo $account_bank ?>" class="regular-text"></td>
  366. </tr>
  367. <tr valign="top">
  368.     <th scope="row"><label for="account_name"><?php _e("Account Name:", 'account_name' ); ?></label></th>
  369.     <td><input name="account_name" type="text" id="account_name" value="<?php echo $account_name ?>" class="regular-text"></td>
  370. </tr>
  371. <tr valign="top">
  372.     <th scope="row"><label for="account_bsb"><?php _e("Account BSB:", 'account_bsb' ); ?></label></th>
  373.     <td><input name="account_bsb" type="text" id="account_bsb" value="<?php echo $account_bsb ?>" class="regular-text code"></td>
  374. </tr>
  375. <tr valign="top">
  376.     <th scope="row"><label for="account_number"><?php _e("Account Number:", 'account_number' ); ?></label></th>
  377.     <td><input name="account_number" type="text" id="account_number" value="<?php echo $account_number ?>" class="regular-text code"></td>
  378. </tr>
  379. </table>
  380.  
  381.  
  382. <h3>PayPal details</h3>
  383. <p>These are used when your members pay for transactions via PayPal.</p>
  384. <table class="form-table">
  385. <tr valign="top">
  386.     <th scope="row"><label for="paypal_email"><?php _e("PayPal Email Address:", 'paypal_email' ); ?></label></th>
  387.     <td><input name="paypal_email" type="text" id="paypal_email" value="<?php echo $paypal_email ?>" class="regular-text"></td>
  388. </tr>
  389. <tr valign="top">
  390.     <th scope="row"><label for="paypal_account_id"><?php _e("PayPal Account:", 'paypal_account_id' ); ?></label></th>
  391.     <td><input name="paypal_account_id" type="text" id="paypal_account_id" value="<?php echo $paypal_account_id ?>" class="regular-text"></td>
  392. </tr>
  393. <tr valign="top">
  394.     <th scope="row"><label for="paypal_additional_percent"><?php _e("PayPal Additional Fee (%):", 'paypal_additional_percent' ); ?></label></th>
  395.     <td><input name="paypal_additional_percent" type="text" id="paypal_additional_percent" value="<?php echo $paypal_additional_percent ?>" class="regular-text"></td>
  396. </tr>
  397. </table>
  398.  
  399.    
  400. <p class="submit"><input type="submit" name="submit" id="submit" class="button-primary" value="Save Changes"></p>
  401.  
  402. </form>
  403.    
  404. </div>
  405.  
  406. <?php
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement