WC_Moip

WooCommerce Moip main class.

Defined (1)

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

/woocommerce-moip.php  
  1. class WC_Moip { 
  2.  
  3. /** 
  4. * Plugin version. 
  5. * @var string 
  6. */ 
  7. const VERSION = '2.2.9'; 
  8.  
  9. /** 
  10. * Integration id. 
  11. * @var string 
  12. */ 
  13. protected static $gateway_id = 'moip'; 
  14.  
  15. /** 
  16. * Instance of this class. 
  17. * @var object 
  18. */ 
  19. protected static $instance = null; 
  20.  
  21. /** 
  22. * Initialize the plugin actions. 
  23. */ 
  24. public function __construct() { 
  25. // Load plugin text domain 
  26. add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); 
  27.  
  28. // Checks with WooCommerce is installed. 
  29. if ( class_exists( 'WC_Payment_Gateway' ) ) { 
  30. // Include the WC_Moip_Gateway class. 
  31. include_once 'includes/class-wc-moip-messages.php'; 
  32. include_once 'includes/class-wc-moip-gateway.php'; 
  33.  
  34. add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateway' ) ); 
  35. add_action( 'wp_ajax_woocommerce_moip_transparent_checkout', array( $this, 'transparent_checkout_ajax' ) ); 
  36. add_action( 'wp_ajax_nopriv_woocommerce_moip_transparent_checkout', array( $this, 'transparent_checkout_ajax' ) ); 
  37. } else { 
  38. add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) ); 
  39.  
  40. /** 
  41. * Return an instance of this class. 
  42. * @return object A single instance of this class. 
  43. */ 
  44. public static function get_instance() { 
  45. // If the single instance hasn't been set, set it now. 
  46. if ( null == self::$instance ) { 
  47. self::$instance = new self; 
  48.  
  49. return self::$instance; 
  50.  
  51. /** 
  52. * Return the gateway id/slug. 
  53. * @return string Gateway id/slug variable. 
  54. */ 
  55. public static function get_gateway_id() { 
  56. return self::$gateway_id; 
  57.  
  58. /** 
  59. * Load the plugin text domain for translation. 
  60. * @return void 
  61. */ 
  62. public function load_plugin_textdomain() { 
  63. $locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce-moip' ); 
  64.  
  65. load_textdomain( 'woocommerce-moip', trailingslashit( WP_LANG_DIR ) . 'woocommerce-moip/woocommerce-moip-' . $locale . '.mo' ); 
  66. load_plugin_textdomain( 'woocommerce-moip', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); 
  67.  
  68. /** 
  69. * Add the gateway to WooCommerce. 
  70. * @param array $methods WooCommerce payment methods. 
  71. * @return array Payment methods with Moip. 
  72. */ 
  73. public function add_gateway( $methods ) { 
  74. $methods[] = 'WC_Moip_Gateway'; 
  75.  
  76. return $methods; 
  77.  
  78. /** 
  79. * Saved by ajax the order information. 
  80. * @return void 
  81. */ 
  82. public function transparent_checkout_ajax() { 
  83. $settings = get_option( 'woocommerce_moip_settings' ); 
  84.  
  85. if ( 'tc' != $settings['api'] ) { 
  86. die(); 
  87.  
  88. check_ajax_referer( 'woocommerce_moip_transparent_checkout', 'security' ); 
  89.  
  90. $method = $_POST['method']; 
  91. $order_id = (int) $_POST['order_id']; 
  92. $order = new WC_Order( $order_id ); 
  93. if ( function_exists( 'WC' ) ) { 
  94. $mailer = WC()->mailer(); 
  95. } else { 
  96. global $woocommerce; 
  97. $mailer = $woocommerce->mailer(); 
  98.  
  99. if ( 'CartaoCredito' == $method ) { 
  100. // Add payment information. 
  101. $status = esc_attr( WC_Moip_Messages::translate_status( $_POST['status'] ) ); 
  102. update_post_meta( $order_id, 'woocommerce_moip_method', esc_attr( $_POST['method'] ) ); 
  103. update_post_meta( $order_id, 'woocommerce_moip_code', esc_attr( $_POST['code'] ) ); 
  104. update_post_meta( $order_id, 'woocommerce_moip_status', $status ); 
  105.  
  106. // Send email with payment information. 
  107. $message_body = '<p>'; 
  108. $message_body .= WC_Moip_Messages::credit_cart_message( $status, $_POST['code'] ); 
  109. $message_body .= '</p>'; 
  110.  
  111. $message = $mailer->wrap_message( 
  112. sprintf( __( 'Order %s received', 'woocommerce-moip' ), $order->get_order_number() ),  
  113. apply_filters( 'woocommerce_moip_thankyou_creditcard_email_message', $message_body, $order_id ) 
  114. ); 
  115.  
  116. $mailer->send( $order->billing_email, sprintf( __( 'Order %s received', 'woocommerce-moip' ), $order->get_order_number() ), $message ); 
  117. } else if ( 'DebitoBancario' == $method ) { 
  118. // Add payment information. 
  119. update_post_meta( $order_id, 'woocommerce_moip_method', esc_attr( $_POST['method'] ) ); 
  120. update_post_meta( $order_id, 'woocommerce_moip_url', esc_url( $_POST['url'] ) ); 
  121.  
  122. // Send email with payment information. 
  123. $url = sprintf( '<p><a class="button" href="%1$s" target="_blank">%1$s</a></p>', esc_url( $_POST['url'] ) ); 
  124. $message_body = '<p>'; 
  125. $message_body .= WC_Moip_Messages::debit_email_message(); 
  126. $message_body .= '</p>'; 
  127.  
  128. $message = $mailer->wrap_message( 
  129. sprintf( __( 'Order %s received', 'woocommerce-moip' ), $order->get_order_number() ),  
  130. apply_filters( 'woocommerce_moip_thankyou_debit_email_message', $message_body, $order_id ) . $url 
  131. ); 
  132.  
  133. $mailer->send( $order->billing_email, sprintf( __( 'Order %s received', 'woocommerce-moip' ), $order->get_order_number() ), $message ); 
  134. } else { 
  135. // Add payment information. 
  136. update_post_meta( $order_id, 'woocommerce_moip_method', esc_attr( $_POST['method'] ) ); 
  137. update_post_meta( $order_id, 'woocommerce_moip_url', esc_url( $_POST['url'] ) ); 
  138.  
  139. // Send email with payment information. 
  140. $url = sprintf( '<p><a class="button" href="%1$s" target="_blank">%1$s</a></p>', esc_url( $_POST['url'] ) ); 
  141. $message_body = '<p>'; 
  142. $message_body .= WC_Moip_Messages::billet_email_message(); 
  143. $message_body .= '</p>'; 
  144.  
  145. $message = $mailer->wrap_message( 
  146. sprintf( __( 'Order %s received', 'woocommerce-moip' ), $order->get_order_number() ),  
  147. apply_filters( 'woocommerce_moip_thankyou_billet_email_message', $message_body, $order_id ) . $url 
  148. ); 
  149.  
  150. $mailer->send( $order->billing_email, sprintf( __( 'Order %s received', 'woocommerce-moip' ), $order->get_order_number() ), $message ); 
  151.  
  152. die(); 
  153.  
  154. /** 
  155. * WooCommerce fallback notice. 
  156. * @return string 
  157. */ 
  158. public function woocommerce_missing_notice() { 
  159. echo '<div class="error"><p>' . sprintf( __( 'WooCommerce Moip Gateway depends on the last version of %s to work!', 'woocommerce-moip' ), '<a href="http://wordpress.org/extend/plugins/woocommerce/">WooCommerce</a>' ) . '</p></div>';