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-client/wordcamp-qbo-client.php

    r3112 r3119  
    434434
    435435    /**
     436     * Get the filename for a PDF copy of an invoice
     437     *
     438     * @param int $invoice_id
     439     *
     440     * @return WP_Error|string
     441     */
     442    public static function get_invoice_filename( $invoice_id ) {
     443        $request  = self::build_invoice_filename_request( $invoice_id );
     444        $response = wp_remote_get( $request['url'], $request['args'] );
     445
     446        if ( is_wp_error( $response ) ) {
     447            $result = $response;
     448        } else {
     449            $result = json_decode( wp_remote_retrieve_body( $response ) );
     450
     451            if ( $result && is_file( $result->filename ) ) {
     452                $result = $result->filename;
     453            } else {
     454                $result = new WP_Error( 'invalid_filename', 'The filename was not valid.', $result );
     455            }
     456        }
     457
     458        return $result;
     459    }
     460
     461    /**
     462     * Build the request to get the filename for a PDF copy of an invoice
     463     *
     464     * @param array $invoice_ids
     465     *
     466     * @return array
     467     */
     468    protected static function build_invoice_filename_request( $invoice_id ) {
     469        $params = array(
     470            'invoice_id' => strval( absint( $invoice_id ) ),    // absint() to validate, strval() to convert to type expected by API
     471        );
     472
     473        $request_url = self::$api_base . '/invoice_pdf';
     474
     475        $args = array(
     476            'headers' => array(
     477                'Authorization' => self::_get_auth_header( 'get', $request_url, '', $params ),
     478                'Content-Type'  => 'application/json',
     479            ),
     480        );
     481
     482        $request_url = add_query_arg( $params, $request_url );  // has to be done after get_auth_header() is called so that the base url and params can be passed separately
     483
     484        return array(
     485            'url'  => $request_url,
     486            'args' => $args,
     487        );
     488    }
     489
     490    /**
    436491     * Create an HMAC signature header for a request.
    437492     *
Note: See TracChangeset for help on using the changeset viewer.