id = 'payments'; $this->db = new RCP_Payments; } /** * Retrieve response data * * @since 1.0 */ public function get_data( WP_REST_Request $request ) { if( $request->get_param( 'id' ) ) { $payment = new RCP_REST_API_Payment( $request->get_param( 'id' ) ); if( ! empty( $payment->id ) ) { $payment->setup(); } else { $payment = new WP_Error( 'no_payment', __( 'Invalid payment', 'rcp-rest' ), array( 'status' => 404 ) ); } return $payment; } return new WP_REST_Response( $this->get_payments() ); } /** * Retrieve response data for create requests * * @since 1.0 */ public function new_post_data( WP_REST_Request $request ) { if( ! $request->has_param( 'amount' ) ) { $response = new WP_Error( 'missing_amount', __( 'No payment amount supplied', 'rcp-rest' ), array( 'status' => 500 ) ); } if( ! $request->get_param( 'subscription' ) ) { $response = new WP_Error( 'missing_subscription', __( 'No subscription name supplied', 'rcp-rest' ), array( 'status' => 500 ) ); } if( ! $request->get_param( 'user_id' ) ) { $response = new WP_Error( 'missing_user_id', __( 'No user ID supplied', 'rcp-rest' ), array( 'status' => 500 ) ); } if( empty( $response ) ) { $payment = new RCP_REST_API_Payment; $args = $payment->sanitize_payment_args( $request->get_params() ); $payment_id = $this->db->insert( $args ); if( $payment_id ) { $response = 1; } else { $response = new WP_Error( 'create_failed', __( 'Payment creation failed', 'rcp-rest' ), array( 'status' => 500 ) ); } } return new WP_REST_Response( $response ); } /** * Retrieve response data for update requests * * @since 1.0 */ public function update_post_data( WP_REST_Request $request ) { if ( $request->has_param( 'ID' ) ) { $request->set_param( 'id', $request->get_param( 'ID' ) ); } if( ! $request->get_param( 'id' ) ) { $response = new WP_Error( 'missing_id', __( 'No payment ID supplied', 'rcp-rest' ), array( 'status' => 500 ) ); return new WP_REST_Response( $response ); } $payment = new RCP_REST_API_Payment( $request->get_param( 'id' ) ); $fields = (array) $payment; $args = wp_parse_args( $request->get_params(), $fields ); $args = $payment->sanitize_payment_args( $args ); if( $this->db->update( $request->get_param( 'id' ), $args ) ) { $response = 1; } else { $response = new WP_Error( 'update_failed', __( 'Update Failed', 'rcp-rest' ), array( 'status' => 500 ) ); } return new WP_REST_Response( $response ); } /** * Retrieve response data for delete requests * * @since 1.0 */ public function delete_data( WP_REST_Request $request ) { if ( $request->has_param( 'ID' ) ) { $request->set_param( 'id', $request->get_param( 'ID' ) ); } $this->db->delete( $request->get_param( 'id' ) ); return new WP_REST_Response( 1 ); } /** * Retrieve payment data * * @since 1.0 */ private function get_payments() { $args = wp_parse_args( $this->query_args, array( 'number' => 20, 'orderby' => 'id', 'order' => 'DESC', 'offset' => 0, 's' => '', 'status' => '', 'date' => '', 'fields' => '*', 'subscription' => 0, ) ); if( ! empty( $this->query_args['member'] ) ) { $args['user_id'] = absint( $this->query_args['member'] ); } $payments = $this->db->get_payments( $args ); if( ! empty( $payments ) ) { foreach( $payments as $key => $payment ) { $payments[ $key ] = new RCP_REST_API_Payment( $payment->id ); } } return $payments; } /** * Determine if authenticated user has permission to access response data * * @since 1.0 */ public function can_view() { return current_user_can( 'rcp_view_payments' ); } /** * Determine if authenticated user has permission to edit data * * @since 1.0 */ public function can_edit() { return current_user_can( 'rcp_manage_payments' ); } }