Making WordPress.org


Ignore:
Timestamp:
05/11/2016 11:21:02 PM (10 years ago)
Author:
iandunn
Message:

WordCamp Budgets: Attach a PDF copy of the invoice to the notification email.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo/wordcamp-qbo.php

    r3114 r3119  
    9090            'methods' => 'GET, POST',
    9191            'callback' => array( __CLASS__, 'rest_callback_invoice' ),
     92        ) );
     93
     94        register_rest_route( 'wordcamp-qbo/v1', '/invoice_pdf', array(
     95            'methods' => 'GET',
     96            'callback' => array( __CLASS__, 'rest_callback_invoice_pdf' ),
    9297        ) );
    9398
     
    595600
    596601    /**
     602     * REST: /invoice_pdf
     603     *
     604     * Saves a PDF copy of the invoice and returns the filename
     605     *
     606     * Note: The function that eventually ends up using the file should delete it once it's done with it.
     607     *
     608     * @param WP_REST_Request $request
     609     *
     610     * @return string|WP_Error The filename on success, or a WP_Error on failure
     611     */
     612    public static function rest_callback_invoice_pdf( $request ) {
     613        if ( ! self::_is_valid_request( $request ) ) {
     614            return new WP_Error( 'unauthorized', 'Unauthorized', array( 'status' => 401 ) );
     615        }
     616
     617        $qbo_request = self::build_qbo_get_invoice_pdf_request( $request->get_param( 'invoice_id' ) );
     618        $response    = wp_remote_get( $qbo_request['url'], $qbo_request['args'] );
     619
     620        if ( is_wp_error( $response ) ) {
     621            $result = $response;
     622        } elseif ( 200 != wp_remote_retrieve_response_code( $response ) ) {
     623            $result = new WP_Error( 'invalid_http_code', 'Invalid HTTP response code', $response );
     624        } else {
     625            $body             = wp_remote_retrieve_body( $response );
     626            $valid_pdf_header = '%PDF-' === substr( $body, 0, 5 );
     627            $valid_pdf_footer = '%%EOF' === substr( $body, strlen( $body ) - 7, 5 );
     628
     629            if ( $valid_pdf_header && $valid_pdf_footer ) {
     630                $filename = sprintf(
     631                    '%sWPCS-invoice-%d.pdf',
     632                    get_temp_dir(),
     633                    $request->get_param( 'invoice_id' )
     634                );
     635
     636                if ( file_put_contents( $filename, $body ) ) {
     637                    $result = array( 'filename' => $filename );
     638                } else {
     639                    $result = new WP_Error( 'write_error', 'Failed writing PDF to disk.', compact( 'filename', 'body' ) );
     640                }
     641            } else {
     642                $result = new WP_Error( 'invalid_body', 'Response body was not a PDF.', $response );
     643            }
     644        }
     645
     646        return $result;
     647    }
     648
     649    /**
     650     * Build a request to send an Invoice via QuickBook's API
     651     *
     652     * @param int $invoice_id
     653     *
     654     * @return array
     655     */
     656    protected static function build_qbo_get_invoice_pdf_request( $invoice_id ) {
     657        self::load_options();
     658        $oauth = self::_get_oauth();
     659        $oauth->set_token( self::$options['auth']['oauth_token'], self::$options['auth']['oauth_token_secret'] );
     660
     661        $request_url = sprintf(
     662            '%s/v3/company/%d/invoice/%d/pdf',
     663            self::$api_base_url,
     664            rawurlencode( self::$options['auth']['realmId'] ),
     665            rawurlencode( $invoice_id )
     666        );
     667
     668        $args = array(
     669            'timeout' => self::REMOTE_REQUEST_TIMEOUT,
     670            'headers' => array(
     671                'Authorization' => $oauth->get_oauth_header( 'GET', $request_url ),
     672                'Accept'        => 'application/pdf',
     673                'Content-Type'  => 'application/pdf',
     674            ),
     675            'body' => '',
     676        );
     677
     678        return array(
     679            'url'  => $request_url,
     680            'args' => $args,
     681        );
     682    }
     683
     684    /**
    597685     * Notify Central that an invoice was created but couldn't be sent to the sponsor
    598686     *
Note: See TracChangeset for help on using the changeset viewer.