domain = 'wc-gateway-bloyal-loyalty-dollar'; $this->id = 'bloyal_loyalty_dollar'; $this->icon = apply_filters( 'woocommerce_offline_icon', '' ); $this->has_fields = true; $this->method_title = __( 'bLoyal Loyalty Dollar', 'wc-gateway-bloyal-loyalty-dollar' ); $this->method_description = __( 'Allows payments thorugh bloyal loyalty dollar.', 'wc-gateway-bloyal-loyalty-dollar' ); $this->init_form_fields(); $this->init_settings(); $this->title = $this->get_option( 'title' ); $this->description = $this->get_option( 'description' ); $this->instructions = $this->get_option( 'instructions', $this->description ); $this->supports = array( 'products', 'refunds', ); add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) ); add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 ); } /** * Initialize Gateway Settings Form Fields */ public function init_form_fields() { $this->form_fields = apply_filters( 'wc_offline_form_fields', array( 'enabled' => array( 'title' => __( 'Enable/Disable', 'wc-gateway-bloyal-loyalty-dollar' ), 'type' => 'checkbox', 'label' => __( 'Enable bLoyal Loyalty Dollar', 'wc-gateway-bloyal-loyalty-dollar' ), 'default' => 'No', ), 'title' => array( 'title' => __( 'Title', 'wc-gateway-bloyal-loyalty-dollar' ), 'type' => 'text', 'description' => __( 'This controls the title for the payment method the customer sees during checkout.', 'wc-gateway-bloyal-loyalty-dollar' ), 'default' => __( 'bLoyal Loyalty Dollar', 'wc-gateway-bloyal-loyalty-dollar' ), 'desc_tip' => true, ), 'description' => array( 'title' => __( 'Description', 'wc-gateway-bloyal-loyalty-dollar' ), 'type' => 'textarea', 'description' => __( 'Payment method description that the customer will see on your checkout.', 'wc-gateway-bloyal-loyalty-dollar' ), 'default' => __( '', 'wc-gateway-bloyal-loyalty-dollar' ), 'desc_tip' => true, ), 'validate' => array( 'title' => __( 'Validate', 'wc-gateway-bloyal-loyalty-dollar' ), 'type' => 'button', 'description' => __( 'Payment method description that the customer will see on your checkout.', 'wc-gateway-bloyal-loyalty-dollar' ), 'default' => __( 'Amount will be deducted from your bloyal loyalty dollar balance.', 'wc-gateway-bloyal-loyalty-dollar' ), 'desc_tip' => true, ), 'instructions' => array( 'title' => __( 'Instructions', 'wc-gateway-bloyal-loyalty-dollar' ), 'type' => 'textarea', 'description' => __( 'Instructions that will be added to the thank you page and emails.', 'wc-gateway-bloyal-loyalty-dollar' ), 'default' => __( 'Instructions that will be added to the thank you page and emails.', 'wc-gateway-bloyal-loyalty-dollar' ), 'desc_tip' => true, ), ) ); } /** * Output for the order received page. */ public function thankyou_page() { if ( $this->instructions ) { echo wpautop( wptexturize( esc_attr( $this->instructions ) ) ); } } /** * Add content to the WC emails. * * @access public * @param WC_Order $order * @param bool $sent_to_admin * @param bool $plain_text */ public function email_instructions( $order, $sent_to_admin, $plain_text = false ) { if ( $this->instructions && ! $sent_to_admin && $this->id === $order->get_payment_method() && $order->has_status( 'on-hold' ) ) { echo wpautop( wptexturize( esc_attr( $this->instructions ) ) ) . PHP_EOL; } } public function payment_fields() { if ( $description = $this->get_description() ) { echo wpautop( wptexturize( esc_attr( $description ) ) ); } $applyFullBalance = get_option( 'bloyal_apply_full_balance_loyalty' ); $chosen_payment_method = WC()->session->get( 'chosen_payment_method' ); ?>
cart->total; $cart_total = WC()->cart->total; $redeemResult = $this->redeemLoyaltyDollar( $cart_total, $order_id ); if ( $redeemResult['result'] == true ) { $order->add_order_note( 'Payment done using loyalty Dollar. Transaction ID : ' . $redeemResult['transaction_id'] ); $order->payment_complete(); return array( 'result' => 'success', 'redirect' => $this->get_return_url( $order ), ); } else { wc_add_notice( $redeemResult['result'], 'error' ); return array( 'result' => 'failure', 'message' => $redeemResult['message'], ); } } catch ( Exception $exception ) { bLoyalLoggerService::write_custom_log( $exception->getMessage(), 3 ); echo 'Got Exception : ' . esc_attr( $exception ); return array( 'result' => 'failure', 'message' => 'Unable to redeem giftcard.', ); } } private function redeemLoyaltyDollar( $amount, $order_id ) { try { $balance = ( new LoyaltyDollarPaymentController() )->getLoyaltyDollarBalance(); if ( $balance == -1 || $balance->GetCardBalanceResult->CurrentBalance < $amount ) { $msg = $balance == -1 ? 'Unable to check your balance' : 'Select another payment method to complete transaction.'; wc_add_notice( $msg, 'error' ); return array( 'result' => false, 'message' => $msg, ); } else { $result = ( new LoyaltyDollarPaymentController() )->redeemLoyaltyDollarBalance( $amount, $order_id ); if ( $result['result'] == false ) { $msg = 'Unable to redeem gift card.'; wc_add_notice( $msg, 'error' ); return array( 'result' => false, 'message' => $msg, ); } else { return array( 'result' => true, 'transaction_id' => $result['transaction_id'], ); } } } catch ( Exception $e ) { bLoyalLoggerService::write_custom_log( $e->getMessage(), 3 ); } } public function process_refund( $order_id, $amount = null, $reason = '' ) { try { $order = new WC_Order( $order_id ); $bloyal_obj = new BloyalController(); $refund_transaction_id = $bloyal_obj->bloyal_refund_loyalty_dollars( $order_id, $amount ); if ( $refund_transaction_id ) { return true; } return false; } catch ( Exception $e ) { bLoyalLoggerService::write_custom_log( $e->getMessage(), 3 ); } } } } }