Making WordPress.org


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

WordCamp Budgets Dashboard: Send Sponsor Invoices to QuickBooks.

This includes several interconnected pieces:

  • Creating new Customers
  • Fetching existing Customers
  • Creating Invoices
  • Sending Invoices
File:
1 edited

Legend:

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

    r2524 r2532  
    270270
    271271        /**
     272         * Send an invoice to the sponsor through QuickBooks Online's API
     273         *
     274         * @param int $invoice_id
     275         *
     276         * @return string
     277         */
     278        public static function send_invoice_to_quickbooks( $invoice_id ) {
     279                $request  = self::build_send_invoice_request( $invoice_id );
     280                $response = wp_remote_post( $request['url'], $request['args'] );
     281
     282                if ( is_wp_error( $response ) ) {
     283                        $sent = $response->get_error_message();
     284                } else {
     285                        $body = json_decode( wp_remote_retrieve_body( $response ) );
     286
     287                        if ( is_numeric( $body ) ) {
     288                                $sent = absint( $body );
     289                        } elseif ( isset( $body->message ) ) {
     290                                $sent = $body->message;
     291                        } else {
     292                                $sent = 'Unknown error.';
     293                        }
     294                }
     295
     296                return $sent;
     297        }
     298
     299        /**
     300         * Build a request for sending an invoice to QuickBooks
     301         *
     302         * @param int $invoice_id
     303         *
     304         * @return array
     305         */
     306        protected static function build_send_invoice_request( $invoice_id ) {
     307                $invoice      = get_post( $invoice_id );
     308                $invoice_meta = get_post_custom( $invoice_id );
     309                $sponsor_meta = get_post_custom( $invoice_meta['_wcbsi_sponsor_id'][0] );
     310
     311                $due_date = new \DateTime(
     312                        date( 'Y-m-d', $invoice_meta['_wcbsi_due_date'][0] ),
     313                        new \DateTimeZone( get_option('timezone_string') )
     314                );
     315
     316                $payload = array(
     317                        'invoice_title'   => sanitize_text_field( $invoice->post_title                       ),
     318                        'currency_code'   => sanitize_text_field( $invoice_meta['_wcbsi_currency'       ][0] ),
     319                        'qbo_class_id'    => sanitize_text_field( $invoice_meta['_wcbsi_qbo_class_id'   ][0] ),
     320                        'amount'          => floatval(            $invoice_meta['_wcbsi_amount'         ][0] ),
     321                        'description'     => sanitize_text_field( $invoice_meta['_wcbsi_description'    ][0] ),
     322                        'due_date'        => $due_date->format( 'Y-m-dP' ),
     323
     324                        'statement_memo' => sprintf(
     325                                'WordCamp.org Invoice: %s',
     326                                esc_url_raw( admin_url( sprintf( 'post.php?post=%s&action=edit', $invoice_id ) ) )
     327                        ),
     328
     329                        'sponsor' => array(
     330                                'company-name'  => sanitize_text_field( $sponsor_meta['_wcpt_sponsor_company_name' ][0] ),
     331                                'first-name'    => sanitize_text_field( $sponsor_meta['_wcpt_sponsor_first_name'   ][0] ),
     332                                'last-name'     => sanitize_text_field( $sponsor_meta['_wcpt_sponsor_last_name'    ][0] ),
     333                                'email-address' => is_email(            $sponsor_meta['_wcpt_sponsor_email_address'][0] ),
     334                                'phone-number'  => sanitize_text_field( $sponsor_meta['_wcpt_sponsor_phone_number' ][0] ),
     335
     336                                'address1' => sanitize_text_field( $sponsor_meta['_wcpt_sponsor_street_address1'][0] ),
     337                                'city'     => sanitize_text_field( $sponsor_meta['_wcpt_sponsor_city'           ][0] ),
     338                                'state'    => sanitize_text_field( $sponsor_meta['_wcpt_sponsor_state'          ][0] ),
     339                                'zip-code' => sanitize_text_field( $sponsor_meta['_wcpt_sponsor_zip_code'       ][0] ),
     340                                'country'  => sanitize_text_field( $sponsor_meta['_wcpt_sponsor_country'        ][0] ),
     341                        )
     342                );
     343
     344                if ( isset( $sponsor_meta['_wcpt_sponsor_street_address2'][0] ) ) {
     345                        $payload['sponsor']['address2'] = sanitize_text_field( $sponsor_meta['_wcpt_sponsor_street_address2'][0] );
     346                }
     347
     348                $request_url  = self::$api_base . '/invoice';
     349                $body         = wp_json_encode( $payload );
     350                $oauth_header = self::_get_auth_header( 'post', $request_url, $body );
     351
     352                $args = array(
     353                        'headers' => array(
     354                                'Authorization' => $oauth_header,
     355                                'Content-Type'  => 'application/json',
     356                        ),
     357                        'body' => $body,
     358                );
     359
     360                return array(
     361                        'url'  => $request_url,
     362                        'args' => $args,
     363                );
     364        }
     365
     366        /**
    272367         * Create an HMAC signature header for a request.
    273368         *
Note: See TracChangeset for help on using the changeset viewer.