WC_GZD_REST_Orders_Controller

Class WC_GZD_REST_Orders_Controller.

Defined (1)

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

/includes/api/class-wc-gzd-rest-orders-controller.php  
  1. class WC_GZD_REST_Orders_Controller { 
  2.  
  3. /** 
  4. * @var WC_GZD_Gateway_Direct_Debit 
  5. */ 
  6. private $direct_debit_gateway = null; 
  7.  
  8. /** 
  9. * ExtendOrdersController constructor. 
  10. */ 
  11. public function __construct() { 
  12. $this->direct_debit_gateway = new WC_GZD_Gateway_Direct_Debit(); 
  13.  
  14. // v3 
  15. if ( wc_gzd_get_dependencies()->woocommerce_version_supports_crud() ) { 
  16. add_filter( 'woocommerce_rest_prepare_shop_order_object', array( $this, 'prepare' ), 10, 3 ); 
  17. add_filter( 'woocommerce_rest_pre_insert_shop_order_object', array( $this, 'insert_v3' ), 10, 3 ); 
  18. } else { 
  19. add_filter( 'woocommerce_rest_prepare_shop_order', array( $this, 'prepare' ), 10, 3 ); 
  20. add_action( 'woocommerce_rest_insert_shop_order', array( $this, 'insert' ), 10, 3 ); 
  21.  
  22. add_filter( 'woocommerce_rest_shop_order_schema', array( $this, 'schema' ) ); 
  23.  
  24. /** 
  25. * Filter order data returned from the REST API. 
  26. * @since 1.0.0 
  27. * @wp-hook woocommerce_rest_prepare_order 
  28. * @param \WP_REST_Response $response The response object. 
  29. * @param \WP_Post $post object used to create response. 
  30. * @param \WP_REST_Request $request Request object. 
  31. * @return \WP_REST_Response 
  32. */ 
  33. public function prepare( $response, $post, $request ) { 
  34.  
  35. $order = wc_get_order( $post ); 
  36. $response_order_data = $response->get_data(); 
  37.  
  38. $response_order_data['billing']['title'] = wc_gzd_get_crud_data( $order, 'billing_title' ); 
  39. $response_order_data['shipping']['title'] = wc_gzd_get_crud_data( $order, 'shipping_title' ); 
  40. $response_order_data['shipping']['parcelshop'] = (bool) wc_gzd_get_crud_data( $order, 'shipping_parcelshop' ); 
  41. $response_order_data['shipping']['parcelshop_post_number'] = wc_gzd_get_crud_data( $order, 'shipping_parcelshop_post_number' ); 
  42. $response_order_data['parcel_delivery_opted_in'] = wc_gzd_get_crud_data( $order, 'parcel_delivery_opted_in' ); 
  43.  
  44. $holder = wc_gzd_get_crud_data( $order, 'direct_debit_holder' ); 
  45. $iban = wc_gzd_get_crud_data( $order, 'direct_debit_iban' ); 
  46. $bic = wc_gzd_get_crud_data( $order, 'direct_debit_bic' ); 
  47. $mandate_id = wc_gzd_get_crud_data( $order, 'direct_debit_mandate_id' ); 
  48.  
  49. if ( $this->direct_debit_gateway ) { 
  50. $iban = $this->direct_debit_gateway->maybe_decrypt( $iban ); 
  51. $bic = $this->direct_debit_gateway->maybe_decrypt( $bic ); 
  52.  
  53. $response_order_data['direct_debit'] = array( 
  54. 'holder' => $holder,  
  55. 'iban' => $iban,  
  56. 'bic' => $bic,  
  57. 'mandate_id' => $mandate_id 
  58. ); 
  59.  
  60. $response->set_data( $response_order_data ); 
  61.  
  62. return $response; 
  63.  
  64. /** 
  65. * Prepare a single order for create or update. 
  66. * @since 1.0.0 
  67. * @wp-hook woocommerce_rest_insert_customer 
  68. * @param \WP_Post $post Data used to create the customer. 
  69. * @param \WP_REST_Request $request Request object. 
  70. * @param bool $creating True when creating item, false when updating. 
  71. */ 
  72. public function insert( $post, $request, $creating ) { 
  73. $order = wc_get_order( $post->ID ); 
  74. $order = $this->save_update_order_data( $order, $request ); 
  75.  
  76. public function insert_v3( $order, $request, $creating ) { 
  77. $order = $this->save_update_order_data( $order, $request ); 
  78. return $order; 
  79.  
  80. public function save_update_order_data( $order, $request ) { 
  81. if ( isset( $request['billing']['title'] ) ) { 
  82. $order = wc_gzd_set_crud_meta_data( $order, '_billing_title', absint( $request['billing']['title'] ) ); 
  83.  
  84. if ( isset( $request['shipping']['title'] ) ) { 
  85. $order = wc_gzd_set_crud_meta_data( $order, '_shipping_title', absint( $request['shipping']['title'] ) ); 
  86.  
  87. if ( isset( $request['shipping']['parcelshop'] ) ) { 
  88. if ( (bool) $request['shipping']['parcelshop'] ) { 
  89. $order = wc_gzd_set_crud_meta_data( $order, '_shipping_parcelshop', 1 ); 
  90. } else { 
  91. $order = wc_gzd_unset_crud_meta_data( $order, '_shipping_parcelshop' ); 
  92.  
  93. if ( isset( $request['shipping']['parcelshop_post_number'] ) ) { 
  94. $order = wc_gzd_set_crud_meta_data( $order, '_shipping_parcelshop_post_number', wc_clean( $request['shipping']['parcelshop_post_number'] ) ); 
  95.  
  96. if ( isset( $request['direct_debit'] ) ) { 
  97. if ( isset( $request['direct_debit']['holder'] ) ) { 
  98. $order = wc_gzd_set_crud_meta_data( $order, '_direct_debit_holder', sanitize_text_field( $request['direct_debit']['holder'] ) ); 
  99.  
  100. if ( isset( $request['direct_debit']['iban'] ) ) { 
  101. $iban = sanitize_text_field( $request['direct_debit']['iban'] ); 
  102. if ( $this->direct_debit_gateway ) { 
  103. $iban = $this->direct_debit_gateway->maybe_encrypt( $iban ); 
  104. $order = wc_gzd_set_crud_meta_data( $order, '_direct_debit_iban', $iban ); 
  105.  
  106. if ( isset( $request['direct_debit']['bic'] ) ) { 
  107. $bic = sanitize_text_field( $request['direct_debit']['bic'] ); 
  108. if ( $this->direct_debit_gateway ) { 
  109. $bic = $this->direct_debit_gateway->maybe_encrypt( $bic ); 
  110. $order = wc_gzd_set_crud_meta_data( $order, '_direct_debit_bic', $bic ); 
  111.  
  112. if ( isset( $request['direct_debit']['mandate_id'] ) ) { 
  113. $order = wc_gzd_set_crud_meta_data( $order, '_direct_debit_mandate_id', sanitize_text_field( $request['direct_debit']['mandate_id'] ) ); 
  114.  
  115. return $order; 
  116.  
  117. /** 
  118. * Extend schema. 
  119. * @since 1.0.0 
  120. * @wp-hook woocommerce_rest_order_schema 
  121. * @param array $schema_properties Data used to create the order. 
  122. * @return array 
  123. */ 
  124. public function schema( $schema_properties ) { 
  125.  
  126. $schema_properties['billing']['properties']['title'] = array( 
  127. 'description' => __( 'Title', 'woocommerce-germanized' ),  
  128. 'type' => 'integer',  
  129. 'context' => array( 'view', 'edit' ),  
  130. 'enum' => array( 1, 2 ) 
  131. ); 
  132.  
  133. $schema_properties['shipping']['properties']['title'] = array( 
  134. 'description' => __( 'Title', 'woocommerce-germanized' ),  
  135. 'type' => 'integer',  
  136. 'context' => array( 'view', 'edit' ),  
  137. 'enum' => array( 1, 2 ) 
  138. ); 
  139.  
  140. $schema_properties['shipping']['properties']['parcelshop'] = array( 
  141. 'description' => __( 'Parcel Shop', 'woocommerce-germanized' ),  
  142. 'type' => 'boolean',  
  143. 'context' => array( 'view', 'edit' ),  
  144. ); 
  145.  
  146. $schema_properties['shipping']['properties']['parcelshop_post_number'] = array( 
  147. 'description' => __( 'Postnumber', 'woocommerce-germanized' ),  
  148. 'type' => 'string',  
  149. 'context' => array( 'view', 'edit' ),  
  150. ); 
  151.  
  152. $schema_properties['parcel_delivery_opted_in'] = array( 
  153. 'description' => __( 'Parcel Delivery Data Transfer', 'woocommerce-germanized' ),  
  154. 'type' => 'string',  
  155. 'context' => array( 'view', 'edit' ),  
  156. 'readonly' => true,  
  157. ); 
  158.  
  159. $schema_properties['direct_debit'] = array( 
  160. 'description' => __( 'Direct Debit', 'woocommerce-germanized' ),  
  161. 'type' => 'object',  
  162. 'context' => array( 'view', 'edit' ),  
  163. 'properties' => array( 
  164. 'holder' => array( 
  165. 'description' => __( 'Account Holder', 'woocommerce-germanized' ),  
  166. 'type' => 'string',  
  167. 'context' => array( 'view', 'edit' ) 
  168. ),  
  169. 'iban' => array( 
  170. 'description' => __( 'IBAN', 'woocommerce-germanized' ),  
  171. 'type' => 'string',  
  172. 'context' => array( 'view', 'edit' ) 
  173. ),  
  174. 'bic' => array( 
  175. 'description' => __( 'BIC/SWIFT', 'woocommerce-germanized' ),  
  176. 'type' => 'string',  
  177. 'context' => array( 'view', 'edit' ) 
  178. ),  
  179. 'mandate_id' => array( 
  180. 'description' => __( 'Mandate Reference ID', 'woocommerce-germanized' ),  
  181. 'type' => 'string',  
  182. 'context' => array( 'view', 'edit' ) 
  183. ); 
  184.  
  185. return $schema_properties;