init_billogram_payment

The WooCommerce Billogram Integration init billogram payment function.

Description

init_billogram_payment(); 

Usage

  1. if ( !function_exists( 'init_billogram_payment' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-billogram-integration/class-billogram2-payment.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = init_billogram_payment(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/class-billogram2-payment.php  
  1. function init_billogram_payment() { 
  2. class WC_Billogram_Invoice extends WC_Payment_Gateway { 
  3. function __construct() { 
  4. $this->id = 'billogram-invoice'; 
  5. $this->icon = ''; 
  6. $this->has_fields = false; 
  7. $this->method_title = "Faktura"; 
  8. $this->method_description = "Receive an invoice from Billogram in no time! An administrative fee of 15SEK will be charged."; 
  9.  
  10. //Features Supported 
  11. $this->supports = array( 
  12. 'refunds',  
  13. 'products',  
  14. 'subscriptions',  
  15. 'subscription_cancellation',  
  16. 'subscription_suspension',  
  17. 'subscription_reactivation',  
  18. 'subscription_date_changes',  
  19. 'subscription_payment_method_change',  
  20. 'subscription_amount_changes',  
  21. 'multiple_subscriptions' 
  22. ); 
  23.  
  24. // Load the settings. 
  25. $this->init_form_fields(); 
  26. $this->init_settings(); 
  27.  
  28. // Define user set variables 
  29. $this->title = $this->get_option( 'title' ); 
  30. $this->description = $this->get_option( 'description' ); 
  31. $this->instructions = $this->get_option( 'instructions', $this->description ); 
  32.  
  33. // Actions 
  34. add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); 
  35. add_action( 'woocommerce_thankyou_billogram-invoice', array( $this, 'thankyou_page_billogram' ) ); 
  36.  
  37. // Customer Emails 
  38. add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions_billogram'), 10, 3 ); 
  39.  
  40. /** 
  41. * Initialise Gateway Settings Form Fields 
  42. */ 
  43. public function init_form_fields() { 
  44.  
  45. $this->form_fields = array( 
  46. 'enabled' => array( 
  47. 'title' => __( 'Enable/Disable', 'woocommerce' ),  
  48. 'type' => 'checkbox',  
  49. 'label' => __( 'Enable Billogram Invoice Payment', 'woocommerce' ),  
  50. 'default' => 'yes' 
  51. ),  
  52. 'title' => array( 
  53. 'title' => __( 'Title', 'woocommerce' ),  
  54. 'type' => 'text',  
  55. 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),  
  56. 'default' => __( 'Faktura', 'woocommerce' ),  
  57. 'desc_tip' => true,  
  58. ),  
  59. 'description' => array( 
  60. 'title' => __( 'Description', 'woocommerce' ),  
  61. 'type' => 'textarea',  
  62. 'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ),  
  63. 'default' => __( 'Receive an invoice from Billogram in no time! An Administrative fee of 15SEK will be charged.', 'woocommerce' ), //to do make the admin fee a dynamic variable. 
  64. 'desc_tip' => true,  
  65. ),  
  66. 'instructions' => array( 
  67. 'title' => __( 'Instructions', 'woocommerce' ),  
  68. 'type' => 'textarea',  
  69. 'description' => __( 'Instructions that will be added to the thank you page and emails.', 'woocommerce' ),  
  70. 'default' => 'An Billogram Invoice will be send from Billogram, please follow the instructions there!',  
  71. 'desc_tip' => true,  
  72. ),  
  73. ); 
  74.  
  75. /** 
  76. * Output for the order received page. 
  77. */ 
  78. public function thankyou_page_billogram() { 
  79. if ( $this->instructions ) { 
  80. echo wpautop( wptexturize( $this->instructions ) ); 
  81.  
  82. /** 
  83. * Add content to the WC emails. 
  84. * @access public 
  85. * @param WC_Order $order 
  86. * @param bool $sent_to_admin 
  87. * @param bool $plain_text 
  88. */ 
  89. public function email_instructions_billogram( $order, $sent_to_admin, $plain_text = false ) { 
  90. if ( $this->instructions && ! $sent_to_admin && 'billogram-invoice' === $order->payment_method && $order->has_status( 'on-hold' ) ) { 
  91. echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL; 
  92.  
  93. /** 
  94. * Process the payment and return the result 
  95. * @param int $order_id 
  96. * @return array 
  97. */ 
  98. public function process_payment( $order_id ) { 
  99.  
  100. $order = wc_get_order( $order_id ); 
  101.  
  102. $options = get_option('woocommerce_billogram_general_settings'); 
  103.  
  104. if(class_exists('WC_Subscriptions_Order')) { 
  105. if(WC_Subscriptions_Order::get_total_initial_payment( $order ) == 0 && WC_Subscriptions_Order::get_sign_up_fee( $order ) == 0) { 
  106. $order->update_status( 'processing' ); 
  107. //$order->update_status('pending', __( 'Awaiting Billogram Invoice payment', 'woocommerce' ) ); 
  108. $order->add_order_note( 'Billogram invoice is not sent, because the subscription has free trial period!' ); 
  109. }else{ 
  110. // Mark as on-hold (we're awaiting the cheque) 
  111. $order->update_status( 'on-hold' ); 
  112. //$order->update_status('pending', __( 'Awaiting Billogram Invoice payment', 'woocommerce' ) ); 
  113. $order->add_order_note( 'Pending Payment: Awaiting Billogram Invoice payment' ); 
  114.  
  115. if($options['stock-reduction'] == 'checkout') { 
  116. $order->reduce_order_stock(); // Payment is complete so reduce stock levels 
  117.  
  118.  
  119. // Remove cart 
  120. WC()->cart->empty_cart(); 
  121.  
  122. // Return thankyou redirect 
  123. return array( 
  124. 'result' => 'success',  
  125. 'redirect' => $this->get_return_url( $order ) 
  126. ); 
  127.  
  128. /** 
  129. * Process the refund and return the result 
  130. * @param int $order_id 
  131. * @return array 
  132. */  
  133. public function process_refund( $order_id, $amount = null, $reason = '' ) { 
  134. global $wpdb; 
  135. include_once("class-billogram2-api.php"); 
  136. //Init API 
  137. $apiInterface = new WCB_API(); 
  138.  
  139. logthis('------refund order_id:'.$order_id.' amount:'.$amount.'-----'); 
  140. $invoice = $wpdb->get_row("SELECT * FROM wcb_orders WHERE order_id = ".$order_id, ARRAY_A); 
  141. $invoice_id = $invoice['invoice_id']; 
  142.  
  143. if($invoice_id) { 
  144. $apiInterface->create_credit_invoice_request($invoice_id, round($amount)); 
  145. return true; 
  146. }else{ 
  147. return false; 
  148.