Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if ( ! defined( 'myCRED_VERSION' ) ) exit;
- /**
- * Event Espresso
- * Allow users to pay for event tickets using myCRED Points
- * @since 1.6.5
- * @version 1.0
- */
- if ( ! class_exists( 'EE_myCRED' ) ) :
- class EE_myCRED extends EE_Onsite_Gateway {
- private static $_instance = NULL;
- public static function instance( EEM_Gateways &$model ) {
- // check if class object is instantiated
- if ( self::$_instance === NULL or ! is_object( self::$_instance ) or ! ( self::$_instance instanceof EE_myCRED ) ) {
- self::$_instance = new self( $model );
- //echo '<h3>'. __CLASS__ .'->'.__FUNCTION__.' ( line no: ' . __LINE__ . ' )</h3>';
- }
- return self::$_instance;
- }
- protected function __construct( EEM_Gateways &$model ) {
- $this->_gateway_name = mycred_label();
- $this->_button_base = '';
- $this->_path = str_replace( '\\', '/', __FILE__ );
- parent::__construct( $model );
- if ( ! isset( $this->_payment_settings['type'] ) )
- $this->_payment_settings['type'] = 'mycred_default';
- $this->mycred = mycred( $this->_payment_settings['type'] );
- }
- protected function _default_settings() {
- $label = mycred_label();
- $this->_payment_settings['display_name'] = $label;
- $this->_payment_settings['label_payment'] = sprintf( '%s Payment', $label );
- $this->_payment_settings['type'] = 'mycred_default';
- $this->_payment_settings['rate'] = 1;
- $this->_payment_settings['log'] = 'Payment for Event Registration';
- $this->_payment_settings['message_solvent'] = 'Pay using your %_plural%';
- }
- protected function _update_settings() {
- $this->_payment_settings['display_name'] = sanitize_text_field( $_POST['mycred_display_name'] );
- $this->_payment_settings['type'] = sanitize_text_field( $_POST['mycred_type'] );
- $this->_payment_settings['rate'] = sanitize_text_field( $_POST['mycred_rate'] );
- $this->_payment_settings['log'] = strip_tags( $_POST['mycred_log'], '<br /><span><a>' );
- $this->_payment_settings['message_solvent'] = sanitize_text_field( $_POST['mycred_message_solvent'] );
- }
- public function espresso_reg_page_billing_inputs() {
- }
- protected function _display_settings() {
- $mycred_types = mycred_get_types();
- $currency = EE_Config::instance()->currency; ?>
- <tr>
- <th>
- <label for="mycred_label_gateway">Gateway Display Name</label>
- </th>
- <td>
- <input class="regular-text" type="text" name="mycred_display_name" size="30" value="<?php echo stripslashes_deep( $this->_payment_settings['display_name'] ); ?>" /><br />
- <span class="description"><?php _e( 'Payment gateway title to use in the front end.', 'mycred' ); ?></span>
- </td>
- </tr>
- <?php if ( count( $mycred_types ) > 1 ) : ?>
- <tr>
- <th>
- <label for="mycred_type">Point Type</label>
- </th>
- <td>
- <?php mycred_types_select_from_dropdown( 'mycred_type', 'mycred_type', $this->_payment_settings['type'] ); ?><br />
- <span class="description">Select the point type that users can use to pay for event tickets.</span>
- </td>
- </tr>
- <?php else : ?>
- <tr>
- <th> </th>
- <td><input type="hidden" name="mycred_type" value="mycred_default" /></td>
- </tr>
- <?php endif; ?>
- <tr>
- <th>
- <label for="mycred_rate">Exchange Rate</label>
- </th>
- <td>
- <?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 />
- <span class="description"></span>
- </td>
- </tr>
- <tr>
- <th>
- <label for="mycred_log">Log Template</label>
- </th>
- <td>
- <input type="text" name="mycred_log" style="width: 100%;" value="<?php echo esc_attr( $this->_payment_settings['log'] ); ?>" />
- </td>
- </tr>
- <tr>
- <th> </th>
- <td> </td>
- </tr>
- <tr>
- <th>
- <label for="mycred_message_solvent">Gateway Message</label>
- </th>
- <td>
- <input type="text" name="mycred_message_solvent" style="width: 100%;" value="<?php echo esc_attr( $this->_payment_settings['message_solvent'] ); ?>" /><br />
- <span class="description">Message to show when a user selects this gateway for payment. Leave empty to hide.</span>
- </td>
- </tr>
- <tr>
- <th>
- <label for="mycred_message_visitors">Important</label>
- </th>
- <td><span class="description">Visitors and insolvent users will not have access to this gateway!</span></td>
- </tr>
- <?php
- }
- protected function _display_settings_help() {
- }
- public function espresso_display_payment_gateways( $selected_gateway = '' ) {
- if ( ! is_user_logged_in() ) return;
- $session_data = EE_Registry::instance()->SSN->get_session_data();
- if ( ! isset( $session_data['cart'] ) ) return;
- $cart = $session_data['cart'];
- $buyer_id = absint( $session_data['user_id'] );
- $balance = $this->mycred->get_users_balance( $buyer_id, $this->_payment_settings['type'] );
- $cart_total = $cart->get_cart_grand_total();
- $cost = $this->mycred->number( $cart_total * $this->_payment_settings['rate'] );
- if ( $balance < $cost ) return;
- $this->_css_class = $selected_gateway == $this->_gateway_name ? '' : ' hidden';
- echo $this->_generate_payment_gateway_selection_button();
- ?>
- <div id="reg-page-billing-info-<?php echo $this->_gateway_name; ?>-dv" class="reg-page-billing-info-dv <?php echo $this->_css_class; ?>">
- <h3><?php echo $this->_payment_settings['label_gateway']; ?></h3>
- <p><?php echo $this->_payment_settings['message_solvent']; ?></p>
- <table class="table">
- <thead>
- <tr>
- <th>Current Balance</th>
- <th>Cost</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td><?php echo $this->mycred->format_creds( $balance ); ?></td>
- <td><?php echo $this->mycred->format_creds( $cost ); ?></td>
- </tr>
- </tbody>
- </table>
- </div>
- <?php
- }
- /**
- * @param EE_Line_Item $line_item
- * @return boolean
- */
- public function process_payment_start( EE_Line_Item $total_line_item, $transaction = null, $total_to_charge = null ) {
- $session_data = EE_Registry::instance()->SSN->get_session_data();
- if ( is_user_logged_in() ) {
- $item_num = 1;
- if ( ! $transaction )
- $transaction = $total_line_item->transaction();
- $buyer_id = get_current_user_id();
- $primary_registrant = $transaction->primary_registration();
- $balance = $this->mycred->get_users_balance( $buyer_id );
- $cart_total = $total_line_item->total();
- $cost = $this->mycred->number( $cart_total * $this->_payment_settings['rate'] );
- // Solvent
- if ( $balance >= $cost ) {
- // Charge
- $charge = $this->mycred->add_creds(
- 'event_ticket_payment',
- $buyer_id,
- 0-$cost,
- $this->_payment_settings['log'],
- '',
- '',
- $this->_payment_settings['type']
- );
- $this->_debug_log("<hr>No Previous IPN payment received. Create a new one");
- if ( $charge ) {
- $payment_status = EEM_Payment::status_id_approved;
- //no previous payment exists, create one
- $primary_registration_code = ! empty( $primary_registrant ) ? $primary_registrant->reg_code() : '';
- $payment = EE_Payment::new_instance( array(
- 'TXN_ID' => $transaction->ID(),
- 'STS_ID' => $payment_status,
- 'PAY_timestamp' => current_time( 'mysql', false ),
- 'PAY_method' => 'CART',
- 'PAY_amount' => $cart_total,
- 'PAY_gateway' => $this->_gateway_name,
- 'PAY_gateway_response' => 'Account successfully charged',
- 'PAY_txn_id_chq_nmbr' => time() . $buyer_id,
- 'PAY_po_number' => NULL,
- 'PAY_extra_accntng' => $this->mycred->format_creds( $cost ),
- 'PAY_via_admin' => false,
- 'PAY_details' => ''
- ) );
- $success = $payment->save();
- $successful_update_of_transaction = $this->update_transaction_with_payment( $transaction, $payment );
- $return = array( 'success' => true );
- }
- }
- // Insolvent
- else {
- $payment_status = EEM_Payment::status_id_declined;
- $primary_registration_code = ! empty( $primary_registrant ) ? $primary_registrant->reg_code() : '';
- $payment = EE_Payment::new_instance( array(
- 'TXN_ID' => $transaction->ID(),
- 'STS_ID' => $payment_status,
- 'PAY_timestamp' => current_time( 'mysql', false ),
- 'PAY_method' => 'CART',
- 'PAY_amount' => $cart_total,
- 'PAY_gateway' => $this->_gateway_name,
- 'PAY_gateway_response' => 'Insufficient Funds',
- 'PAY_txn_id_chq_nmbr' => NULL,
- 'PAY_po_number' => NULL,
- 'PAY_extra_accntng' => $primary_registration_code,
- 'PAY_via_admin' => false,
- 'PAY_details' => ''
- ) );
- $payment->save();
- $return = array( 'success' => true );
- }
- } else {
- $return = array( 'error' => 'Only registered members can use this payment gateway. Please login and try again.' );
- // no payment required
- }
- return $return;
- }
- }
- endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement