Making WordPress.org

Changeset 2533


Ignore:
Timestamp:
02/19/2016 12:15:08 AM (11 years ago)
Author:
iandunn
Message:

WordCamp Budgets Dashboard: Sync invoice payment status with QuickBooks.

Location:
sites/trunk/wordcamp.org/public_html/wp-content/plugins
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/sponsor-invoices-dashboard.php

    r2532 r2533  
    254254         */
    255255        $sent_invoices = $wpdb->get_results( "
    256                 SELECT *
     256                SELECT blog_id, invoice_id, qbo_invoice_id
    257257                FROM $table_name
    258258                WHERE status = 'wcbsi_approved'
    259259                LIMIT 100
    260         "); // todo if QBO's API imposes a limit, then update this to match
    261 
    262         // todo fake data for testing. replace w/ API call to QBO
    263         $updated_invoices = array(
    264                 //array( 'blog_id' => 11, 'invoice_id' => 45499, 'status' => 'submitted' ),
    265                 //array( 'blog_id' => 11, 'invoice_id' => 45506, 'status' => 'paid' ),
    266         );
    267 
    268         foreach ( $updated_invoices as $invoice ) {
    269                 if ( 'paid' === $invoice['status'] ) {
    270                         update_invoice_status(           $invoice['blog_id'], $invoice['invoice_id'], 'paid' );
    271                         notify_organizer_status_changed( $invoice['blog_id'], $invoice['invoice_id'], 'paid' );
     260        " );
     261
     262        $paid_invoices = \WordCamp_QBO_Client::get_paid_invoices(
     263                wp_list_pluck( $sent_invoices, 'qbo_invoice_id' )
     264        );
     265
     266        mark_invoices_as_paid( $sent_invoices, $paid_invoices );
     267}
     268
     269/**
     270 * Mark WordCamp.org invoices as paid when they've been paid in QuickBooks
     271 *
     272 * @param array $sent_invoices
     273 * @param array $paid_invoices
     274 */
     275function mark_invoices_as_paid( $sent_invoices, $paid_invoices ) {
     276        foreach ( $sent_invoices as $invoice ) {
     277                if ( in_array( (int) $invoice->qbo_invoice_id, $paid_invoices, true ) ) {
     278                        update_invoice_status(           $invoice->blog_id, $invoice->invoice_id, 'paid' );
     279                        notify_organizer_status_changed( $invoice->blog_id, $invoice->invoice_id, 'paid' );
    272280                }
    273281        }
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo-client/wordcamp-qbo-client.php

    r2532 r2533  
    365365
    366366        /**
     367         * Get the paid invoices from the given set of sent invoices
     368         *
     369         * @param array $sent_invoices
     370         *
     371         * @return array
     372         */
     373        public static function get_paid_invoices( $sent_invoices ) {
     374                $paid_invoices = array();
     375                $request       = self::build_paid_invoices_request( $sent_invoices );
     376                $response      = wp_remote_get( $request['url'], $request['args'] );
     377
     378                if ( ! is_wp_error( $response ) ) {
     379                        $body = json_decode( wp_remote_retrieve_body( $response ) );
     380
     381                        if ( is_array( $body ) ) {
     382                                $paid_invoices = array_map( 'absint', $body );
     383                        }
     384                }
     385
     386                return $paid_invoices;
     387        }
     388
     389        /**
     390         * Build the request to check for paid invoices in QuickBooks
     391         *
     392         * @param array $invoice_ids
     393         *
     394         * @return array
     395         */
     396        protected static function build_paid_invoices_request( $invoice_ids ) {
     397                $invoice_ids = array_map( 'absint', $invoice_ids );    // validate
     398                $invoice_ids = array_map( 'strval', $invoice_ids );    // format the way WordCamp_QBO::_in_valid_request() will expect it
     399
     400                $params = array(
     401                        'invoice_ids' => $invoice_ids,
     402                );
     403
     404                $request_url = self::$api_base . '/paid_invoices';
     405
     406                $args = array(
     407                        'headers' => array(
     408                                'Authorization' => self::_get_auth_header( 'get', $request_url, '', $params ),
     409                                'Content-Type'  => 'application/json',
     410                        ),
     411                );
     412
     413                $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
     414
     415                return array(
     416                        'url'  => $request_url,
     417                        'args' => $args,
     418                );
     419        }
     420
     421        /**
    367422         * Create an HMAC signature header for a request.
    368423         *
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo/wordcamp-qbo.php

    r2532 r2533  
    8888                        'methods' => 'GET, POST',
    8989                        'callback' => array( __CLASS__, 'rest_callback_invoice' ),
     90                ) );
     91
     92                register_rest_route( 'wordcamp-qbo/v1', '/paid_invoices', array(
     93                        'methods' => 'GET',
     94                        'callback' => array( __CLASS__, 'rest_callback_paid_invoices' ),
    9095                ) );
    9196        }
    … …  
    834839
    835840        /**
     841         * Returns the subset of the given invoices that have been paid
     842         *
     843         * For example, if QBO invoice IDs 15, 18, 29, and 54 are passed, and 15 and 29 are marked as paid in QBO,
     844         * then this endpoint will return 15 and 29.
     845         *
     846         * @param WP_REST_Request $wordcamp_request
     847         *
     848         * @return array|WP_Error
     849         */
     850        public static function rest_callback_paid_invoices( $wordcamp_request ) {
     851                if ( ! self::_is_valid_request( $wordcamp_request ) ) {
     852                        return new WP_Error( 'unauthorized', 'Unauthorized', array( 'status' => 401 ) );
     853                }
     854
     855                $qbo_request = self::build_qbo_paid_invoices_request( $wordcamp_request->get_param( 'invoice_ids' ) );
     856
     857                if ( is_wp_error( $qbo_request ) ) {
     858                        return $qbo_request;
     859                }
     860
     861                $response = wp_remote_get( $qbo_request['url'], $qbo_request['args'] );
     862
     863                if ( is_wp_error( $response ) ) {
     864                        $result = $response;
     865                } elseif ( 200 != wp_remote_retrieve_response_code( $response ) ) {
     866                        $result = new WP_Error( 'invalid_http_code', 'Invalid HTTP response code', $response );
     867                } else {
     868                        $body = json_decode( wp_remote_retrieve_body( $response ), true );
     869
     870                        if ( isset( $body['QueryResponse']['Invoice'][0]['Id'] ) ) {
     871                                $result = wp_list_pluck( $body['QueryResponse']['Invoice'], 'Id' );
     872                                $result = array_map( 'absint', $result );
     873                        } elseif ( isset( $body['QueryResponse'] ) && 0 === count( $body['QueryResponse'] ) ) {
     874                                $result = array();  // no invoices have been paid
     875                        } else {
     876                                $result = new WP_Error( 'invalid_response', 'Could not extract invoice IDs from response.', $response );
     877                        }
     878                }
     879
     880                return $result;
     881        }
     882
     883        /**
     884         * Build a request to check QuickBook's API for paid invoices
     885         *
     886         * @param array $sent_invoice_ids
     887         *
     888         * @return array|WP_Error
     889         */
     890        protected static function build_qbo_paid_invoices_request( $sent_invoice_ids ) {
     891                global $wpdb;
     892
     893                self::load_options();
     894                $oauth = self::_get_oauth();
     895                $oauth->set_token( self::$options['auth']['oauth_token'], self::$options['auth']['oauth_token_secret'] );
     896
     897                $request_url = sprintf(
     898                        '%s/v3/company/%d/query',
     899                        self::$api_base_url,
     900                        rawurlencode( self::$options['auth']['realmId'] )
     901                );
     902
     903                $sent_invoice_ids = array_map( 'absint', $sent_invoice_ids );    // Invoice IDs are initially cast as integers for validation, and then converted back to strings, because that's what QBO expects.
     904
     905                if ( empty( $sent_invoice_ids ) ) {
     906                        return new WP_Error( 'no_ids', 'No Invoice IDs were given.' );
     907                }
     908
     909                $invoice_id_placeholders = implode( ', ', array_fill( 0, count( $sent_invoice_ids ), '%s' ) );
     910
     911                $request_url_query = array(
     912                        'query' => $wpdb->prepare( "
     913                                SELECT Id, Balance
     914                                FROM Invoice
     915                                WHERE
     916                                        Id IN ( $invoice_id_placeholders ) AND
     917                                        Balance = '0'", // QBO doesn't have an explicit status parameter, it just considers an invoice paid if the balance is 0.
     918                                $sent_invoice_ids
     919                        ),
     920                );
     921
     922                $args = array(
     923                        'headers' => array(
     924                                'Authorization' => $oauth->get_oauth_header( 'GET', $request_url, $request_url_query ),
     925                                'Accept'        => 'application/json',
     926                        ),
     927                );
     928
     929                $request_url_query = array_map( 'rawurlencode', $request_url_query );   // The URL query parameters have to be encoded after the OAuth header is generated so that the signature will be generated correctly.
     930                $request_url       = add_query_arg( $request_url_query, $request_url );
     931
     932                return array(
     933                        'url'  => $request_url,
     934                        'args' => $args,
     935                );
     936        }
     937
     938        /**
    836939         * Verify an HMAC signature for an API request.
    837940         *
Note: See TracChangeset for help on using the changeset viewer.