Advertisement
designbymerovingi

mycred EE 4.6+ gateway

Oct 12th, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.42 KB | None | 0 0
  1. <?php
  2. if ( ! defined( 'myCRED_VERSION' ) ) exit;
  3.  
  4. /**
  5. * Event Espresso
  6. * Allow users to pay for event tickets using myCRED Points
  7. * @since 1.6.5
  8. * @version 1.0
  9. */
  10. if ( ! class_exists( 'EE_myCRED' ) ) :
  11. class EE_myCRED extends EE_Onsite_Gateway {
  12.  
  13. private static $_instance = NULL;
  14.  
  15. public static function instance( EEM_Gateways &$model ) {
  16. // check if class object is instantiated
  17. if ( self::$_instance === NULL or ! is_object( self::$_instance ) or ! ( self::$_instance instanceof EE_myCRED ) ) {
  18. self::$_instance = new self( $model );
  19. //echo '<h3>'. __CLASS__ .'->'.__FUNCTION__.' ( line no: ' . __LINE__ . ' )</h3>';
  20. }
  21. return self::$_instance;
  22. }
  23.  
  24. protected function __construct( EEM_Gateways &$model ) {
  25. $this->_gateway_name = mycred_label();
  26. $this->_button_base = '';
  27. $this->_path = str_replace( '\\', '/', __FILE__ );
  28.  
  29. parent::__construct( $model );
  30.  
  31. if ( ! isset( $this->_payment_settings['type'] ) )
  32. $this->_payment_settings['type'] = 'mycred_default';
  33.  
  34. $this->mycred = mycred( $this->_payment_settings['type'] );
  35. }
  36.  
  37. protected function _default_settings() {
  38. $label = mycred_label();
  39. $this->_payment_settings['display_name'] = $label;
  40. $this->_payment_settings['label_payment'] = sprintf( '%s Payment', $label );
  41. $this->_payment_settings['type'] = 'mycred_default';
  42. $this->_payment_settings['rate'] = 1;
  43. $this->_payment_settings['log'] = 'Payment for Event Registration';
  44. $this->_payment_settings['message_solvent'] = 'Pay using your %_plural%';
  45. }
  46.  
  47. protected function _update_settings() {
  48. $this->_payment_settings['display_name'] = sanitize_text_field( $_POST['mycred_display_name'] );
  49. $this->_payment_settings['type'] = sanitize_text_field( $_POST['mycred_type'] );
  50. $this->_payment_settings['rate'] = sanitize_text_field( $_POST['mycred_rate'] );
  51. $this->_payment_settings['log'] = strip_tags( $_POST['mycred_log'], '<br /><span><a>' );
  52. $this->_payment_settings['message_solvent'] = sanitize_text_field( $_POST['mycred_message_solvent'] );
  53. }
  54.  
  55. public function espresso_reg_page_billing_inputs() {
  56.  
  57. }
  58.  
  59. protected function _display_settings() {
  60. $mycred_types = mycred_get_types();
  61. $currency = EE_Config::instance()->currency; ?>
  62.  
  63. <tr>
  64. <th>
  65. <label for="mycred_label_gateway">Gateway Display Name</label>
  66. </th>
  67. <td>
  68. <input class="regular-text" type="text" name="mycred_display_name" size="30" value="<?php echo stripslashes_deep( $this->_payment_settings['display_name'] ); ?>" /><br />
  69. <span class="description"><?php _e( 'Payment gateway title to use in the front end.', 'mycred' ); ?></span>
  70. </td>
  71. </tr>
  72.  
  73. <?php if ( count( $mycred_types ) > 1 ) : ?>
  74.  
  75. <tr>
  76. <th>
  77. <label for="mycred_type">Point Type</label>
  78. </th>
  79. <td>
  80. <?php mycred_types_select_from_dropdown( 'mycred_type', 'mycred_type', $this->_payment_settings['type'] ); ?><br />
  81. <span class="description">Select the point type that users can use to pay for event tickets.</span>
  82. </td>
  83. </tr>
  84.  
  85. <?php else : ?>
  86.  
  87. <tr>
  88. <th>&nbsp;</th>
  89. <td><input type="hidden" name="mycred_type" value="mycred_default" /></td>
  90. </tr>
  91.  
  92. <?php endif; ?>
  93.  
  94. <tr>
  95. <th>
  96. <label for="mycred_rate">Exchange Rate</label>
  97. </th>
  98. <td>
  99. <?php echo number_format( 1, $currency->dec_plc, $currency->dec_mrk, $currency->thsnds ); ?> <?php echo $currency->code; ?> = <input type="text" name="mycred_rate" size="5" value="<?php echo $this->_payment_settings['rate']; ?>" style="width: 120px;" /> <?php if ( $this->_payment_settings['rate'] == 1 ) echo $this->mycred->singular(); else echo $this->mycred->plural(); ?><br />
  100. <span class="description"></span>
  101. </td>
  102. </tr>
  103.  
  104. <tr>
  105. <th>
  106. <label for="mycred_log">Log Template</label>
  107. </th>
  108. <td>
  109. <input type="text" name="mycred_log" style="width: 100%;" value="<?php echo esc_attr( $this->_payment_settings['log'] ); ?>" />
  110. </td>
  111. </tr>
  112.  
  113. <tr>
  114. <th>&nbsp;</th>
  115. <td>&nbsp;</td>
  116. </tr>
  117.  
  118. <tr>
  119. <th>
  120. <label for="mycred_message_solvent">Gateway Message</label>
  121. </th>
  122. <td>
  123. <input type="text" name="mycred_message_solvent" style="width: 100%;" value="<?php echo esc_attr( $this->_payment_settings['message_solvent'] ); ?>" /><br />
  124. <span class="description">Message to show when a user selects this gateway for payment. Leave empty to hide.</span>
  125. </td>
  126. </tr>
  127. <tr>
  128. <th>
  129. <label for="mycred_message_visitors">Important</label>
  130. </th>
  131. <td><span class="description">Visitors and insolvent users will not have access to this gateway!</span></td>
  132. </tr>
  133. <?php
  134.  
  135. }
  136.  
  137. protected function _display_settings_help() {
  138.  
  139. }
  140.  
  141. public function espresso_display_payment_gateways( $selected_gateway = '' ) {
  142. if ( ! is_user_logged_in() ) return;
  143.  
  144. $session_data = EE_Registry::instance()->SSN->get_session_data();
  145. if ( ! isset( $session_data['cart'] ) ) return;
  146.  
  147. $cart = $session_data['cart'];
  148.  
  149. $buyer_id = absint( $session_data['user_id'] );
  150. $balance = $this->mycred->get_users_balance( $buyer_id, $this->_payment_settings['type'] );
  151.  
  152. $cart_total = $cart->get_cart_grand_total();
  153. $cost = $this->mycred->number( $cart_total * $this->_payment_settings['rate'] );
  154.  
  155. if ( $balance < $cost ) return;
  156.  
  157. $this->_css_class = $selected_gateway == $this->_gateway_name ? '' : ' hidden';
  158. echo $this->_generate_payment_gateway_selection_button();
  159.  
  160. ?>
  161.  
  162. <div id="reg-page-billing-info-<?php echo $this->_gateway_name; ?>-dv" class="reg-page-billing-info-dv <?php echo $this->_css_class; ?>">
  163. <h3><?php echo $this->_payment_settings['label_gateway']; ?></h3>
  164. <p><?php echo $this->_payment_settings['message_solvent']; ?></p>
  165. <table class="table">
  166. <thead>
  167. <tr>
  168. <th>Current Balance</th>
  169. <th>Cost</th>
  170. </tr>
  171. </thead>
  172. <tbody>
  173. <tr>
  174. <td><?php echo $this->mycred->format_creds( $balance ); ?></td>
  175. <td><?php echo $this->mycred->format_creds( $cost ); ?></td>
  176. </tr>
  177. </tbody>
  178. </table>
  179. </div>
  180. <?php
  181. }
  182.  
  183. /**
  184. * @param EE_Line_Item $line_item
  185. * @return boolean
  186. */
  187. public function process_payment_start( EE_Line_Item $total_line_item, $transaction = null, $total_to_charge = null ) {
  188. $session_data = EE_Registry::instance()->SSN->get_session_data();
  189.  
  190. if ( is_user_logged_in() ) {
  191.  
  192. $item_num = 1;
  193. if ( ! $transaction )
  194. $transaction = $total_line_item->transaction();
  195.  
  196. $buyer_id = get_current_user_id();
  197. $primary_registrant = $transaction->primary_registration();
  198.  
  199. $balance = $this->mycred->get_users_balance( $buyer_id );
  200.  
  201. $cart_total = $total_line_item->total();
  202. $cost = $this->mycred->number( $cart_total * $this->_payment_settings['rate'] );
  203.  
  204. // Solvent
  205. if ( $balance >= $cost ) {
  206.  
  207. // Charge
  208. $charge = $this->mycred->add_creds(
  209. 'event_ticket_payment',
  210. $buyer_id,
  211. 0-$cost,
  212. $this->_payment_settings['log'],
  213. '',
  214. '',
  215. $this->_payment_settings['type']
  216. );
  217.  
  218. $this->_debug_log("<hr>No Previous IPN payment received. Create a new one");
  219.  
  220. if ( $charge ) {
  221.  
  222. $payment_status = EEM_Payment::status_id_approved;
  223.  
  224. //no previous payment exists, create one
  225. $primary_registration_code = ! empty( $primary_registrant ) ? $primary_registrant->reg_code() : '';
  226.  
  227. $payment = EE_Payment::new_instance( array(
  228. 'TXN_ID' => $transaction->ID(),
  229. 'STS_ID' => $payment_status,
  230. 'PAY_timestamp' => current_time( 'mysql', false ),
  231. 'PAY_method' => 'CART',
  232. 'PAY_amount' => $cart_total,
  233. 'PAY_gateway' => $this->_gateway_name,
  234. 'PAY_gateway_response' => 'Account successfully charged',
  235. 'PAY_txn_id_chq_nmbr' => time() . $buyer_id,
  236. 'PAY_po_number' => NULL,
  237. 'PAY_extra_accntng' => $this->mycred->format_creds( $cost ),
  238. 'PAY_via_admin' => false,
  239. 'PAY_details' => ''
  240. ) );
  241.  
  242. $success = $payment->save();
  243. $successful_update_of_transaction = $this->update_transaction_with_payment( $transaction, $payment );
  244.  
  245. $return = array( 'success' => true );
  246. }
  247.  
  248. }
  249.  
  250. // Insolvent
  251. else {
  252.  
  253. $payment_status = EEM_Payment::status_id_declined;
  254.  
  255. $primary_registration_code = ! empty( $primary_registrant ) ? $primary_registrant->reg_code() : '';
  256.  
  257. $payment = EE_Payment::new_instance( array(
  258. 'TXN_ID' => $transaction->ID(),
  259. 'STS_ID' => $payment_status,
  260. 'PAY_timestamp' => current_time( 'mysql', false ),
  261. 'PAY_method' => 'CART',
  262. 'PAY_amount' => $cart_total,
  263. 'PAY_gateway' => $this->_gateway_name,
  264. 'PAY_gateway_response' => 'Insufficient Funds',
  265. 'PAY_txn_id_chq_nmbr' => NULL,
  266. 'PAY_po_number' => NULL,
  267. 'PAY_extra_accntng' => $primary_registration_code,
  268. 'PAY_via_admin' => false,
  269. 'PAY_details' => ''
  270. ) );
  271.  
  272. $payment->save();
  273. $return = array( 'success' => true );
  274.  
  275. }
  276.  
  277. } else {
  278. $return = array( 'error' => 'Only registered members can use this payment gateway. Please login and try again.' );
  279. // no payment required
  280. }
  281.  
  282. return $return;
  283. }
  284.  
  285. }
  286. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement