Skip to content

Commit

Permalink
Quickbooks: Update customer info when creating invoices
Browse files Browse the repository at this point in the history
  • Loading branch information
iandunn committed Jul 27, 2023
1 parent 5ef9b7b commit ba7fa61
Showing 1 changed file with 117 additions and 4 deletions.
121 changes: 117 additions & 4 deletions public_html/wp-content/plugins/wordcamp-qbo/wordcamp-qbo.php
Original file line number Diff line number Diff line change
Expand Up @@ -750,10 +750,13 @@ protected static function notify_invoice_failed_to_send( $invoice_id, $error ) {
* @return int|WP_Error The customer ID if success; a WP_Error if failure
*/
protected static function probably_get_customer_id( $sponsor, $currency_code ) {
$customer_id = self::get_customer( $sponsor['company-name'], $currency_code );
$customer = self::get_customer( $sponsor['company-name'], $currency_code );
$customer_id = $customer['Id'];

if ( is_wp_error( $customer_id ) || ! $customer_id ) {
if ( is_wp_error( $customer ) || ! $customer ) {
$customer_id = self::create_customer( $sponsor, $currency_code );
} else {
self::update_customer( $customer, $sponsor );
}

return $customer_id;
Expand All @@ -765,7 +768,7 @@ protected static function probably_get_customer_id( $sponsor, $currency_code ) {
* @param string $customer_name
* @param string $currency_code
*
* @return int|false|WP_Error A customer ID as integer, if one was found; false if no match was found; a WP_Error if an error occurred.
* @return array|false|WP_Error A customer array if one was found; false if no match was found; a WP_Error if an error occurred.
*/
protected static function get_customer( $customer_name, $currency_code ) {
$qbo_request = self::build_qbo_get_customer_request( $customer_name, $currency_code );
Expand All @@ -785,7 +788,8 @@ protected static function get_customer( $customer_name, $currency_code ) {
$body = json_decode( wp_remote_retrieve_body( $response ), true );

if ( isset( $body['QueryResponse']['Customer'][0]['Id'] ) ) {
$result = absint( $body['QueryResponse']['Customer'][0]['Id'] );
$result = $body['QueryResponse']['Customer'][0];
$result['Id'] = absint( $result['Id'] );
} elseif ( isset( $body['QueryResponse'] ) && 0 === count( $body['QueryResponse'] ) ) {
$result = false;
} else {
Expand Down Expand Up @@ -986,6 +990,115 @@ protected static function normalize_sponsor( array $sponsor ) : array {
return $sponsor;
}

/**
* Update a customer in QuickBooks with the latest info for a corresponding Sponsor in WordCamp.org
*
* Over time the info in QBO gets out of date, so it should be refreshed based the info that active camps
* provide.
*
* @param array $customer
* @param array $sponsor
*
* @return bool|WP_Error The customer ID if success; a WP_Error if failure
*/
protected static function update_customer( array $customer, array $sponsor ) {
$qbo_request = self::build_qbo_update_customer_request( $customer, $sponsor );

if ( is_wp_error( $qbo_request ) ) {
return $qbo_request;
}

$response = wp_remote_post( $qbo_request['url'], $qbo_request['args'] );
Logger\log( 'remote_request', compact( 'qbo_request', 'response' ) );

if ( is_wp_error( $response ) ) {
$result = $response;
} elseif ( 200 != wp_remote_retrieve_response_code( $response ) ) {
$result = new WP_Error( 'invalid_http_code', 'Invalid HTTP response code', $response );
} else {
$body = json_decode( wp_remote_retrieve_body( $response ), true );

if ( isset( $body['Customer']['Id'] ) ) {
$result = true;
} else {
$result = new WP_Error( 'invalid_response_body', 'Could not extract customer ID from response.', $response );
}
}

return $result;
}

/**
* Build a request to update a Customer via QuickBook's API
*
* @param array $customer
* @param array $sponsor
*
* @return array|WP_Error
*/
protected static function build_qbo_update_customer_request( array $customer, array $sponsor ) {
self::load_options();
$oauth_header = self::qbo_client()->get_oauth_header();
$realm_id = self::qbo_client()->get_realm_id();
$sponsor = self::normalize_sponsor( $sponsor );

if ( empty( $sponsor['email-address'] ) ) {
return new WP_Error( 'required_fields_missing', 'Required fields are missing.', $sponsor );
}

$payload = array(
'Id' => $customer['Id'],
'sparse' => true,
'SyncToken' => $customer['SyncToken'],

'BillAddr' => array(
'Line1' => $sponsor['address1'],
'City' => $sponsor['city'],
'Country' => $sponsor['country'],
'CountrySubDivisionCode' => $sponsor['state'],
'PostalCode' => $sponsor['zip-code'],
),

'GivenName' => $sponsor['first-name'],
'FamilyName' => $sponsor['last-name'],

'PrimaryPhone' => array(
'FreeFormNumber' => $sponsor['phone-number'],
),

'PrimaryEmailAddr' => array(
'Address' => $sponsor['email-address'],
),
);

if ( isset( $sponsor['address2'] ) ) {
$payload['BillAddr']['Line2'] = $sponsor['address2'];
}

$request_url = sprintf(
'%s/v3/company/%d/customer',
self::$api_base_url,
rawurlencode( $realm_id )
);

$payload = wp_json_encode( $payload );

$args = array(
'timeout' => self::REMOTE_REQUEST_TIMEOUT,
'headers' => array(
'Authorization' => $oauth_header,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
),
'body' => $payload,
);

return array(
'url' => $request_url,
'args' => $args,
);
}

/**
* Returns the subset of the given invoices that have been paid
*
Expand Down

0 comments on commit ba7fa61

Please sign in to comment.