<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[3119] sites/trunk/wordcamp.org/public_html/wp-content/plugins: WordCamp Budgets: Attach a PDF copy of the invoice to the notification email.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/3119">3119</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/3119","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>iandunn</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-05-11 23:21:02 +0000 (Wed, 11 May 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>WordCamp Budgets: Attach a PDF copy of the invoice to the notification email.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordcamporgpublic_htmlwpcontentpluginswordcamppaymentsnetworkincludessponsorinvoicesdashboardphp">sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/sponsor-invoices-dashboard.php</a></li>
<li><a href="#sitestrunkwordcamporgpublic_htmlwpcontentpluginswordcampqbowordcampqbophp">sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo/wordcamp-qbo.php</a></li>
<li><a href="#sitestrunkwordcamporgpublic_htmlwpcontentpluginswordcampqboclientwordcampqboclientphp">sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo-client/wordcamp-qbo-client.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordcamporgpublic_htmlwpcontentpluginswordcamppaymentsnetworkincludessponsorinvoicesdashboardphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/sponsor-invoices-dashboard.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/sponsor-invoices-dashboard.php 2016-05-11 19:06:13 UTC (rev 3118)
+++ sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/sponsor-invoices-dashboard.php 2016-05-11 23:21:02 UTC (rev 3119)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -322,11 +322,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $sponsor_name = get_sponsor_name( $invoice_id );
</span><span class="cx" style="display: block; padding: 0 10px"> $invoice_url = admin_url( sprintf( 'post.php?post=%s&action=edit', $invoice_id ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $headers = array( 'Reply-To: support@wordcamp.org' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $attachments = array();
+ $attachment_message = '';
+ $invoice_filename = false;
+ // todo realign
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'approved' === $new_status ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $sponsor_id = get_post_meta( $invoice_id, '_wcbsi_sponsor_id', true );
</span><span class="cx" style="display: block; padding: 0 10px"> $sponsor_email = get_post_meta( $sponsor_id, '_wcpt_sponsor_email_address', true );
</span><span class="cx" style="display: block; padding: 0 10px"> $status_message = "has been sent to $sponsor_name via $sponsor_email. You will receive another notification when they have paid the invoice.";
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $qbo_invoice_id = get_post_meta( $invoice_id, '_wcbsi_qbo_invoice_id', true );
+ $invoice_filename = \WordCamp_QBO_Client::get_invoice_filename( $qbo_invoice_id );
+ // todo realign
+
+ if ( ! is_wp_error( $invoice_filename ) ) {
+ $attachments[] = $invoice_filename;
+ $attachment_message = "\nA copy of the invoice has been attached to this message, in case you need to follow up with the sponsor.";
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> } elseif ( 'paid' === $new_status ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $status_message = "has been paid by $sponsor_name. Go ahead and publish them to your website!";
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -339,12 +351,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> You can view the invoice and its status any time at:
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $invoice_url
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $attachment_message
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> If you have any questions, please reply to let us know."
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_mail( $to, $subject, $message, $headers );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_mail( $to, $subject, $message, $headers, $attachments );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $invoice_filename ) {
+ unlink( $invoice_filename );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> restore_current_blog();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="sitestrunkwordcamporgpublic_htmlwpcontentpluginswordcampqbowordcampqbophp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo/wordcamp-qbo.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo/wordcamp-qbo.php 2016-05-11 19:06:13 UTC (rev 3118)
+++ sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo/wordcamp-qbo.php 2016-05-11 23:21:02 UTC (rev 3119)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -91,6 +91,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'callback' => array( __CLASS__, 'rest_callback_invoice' ),
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ register_rest_route( 'wordcamp-qbo/v1', '/invoice_pdf', array(
+ 'methods' => 'GET',
+ 'callback' => array( __CLASS__, 'rest_callback_invoice_pdf' ),
+ ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> register_rest_route( 'wordcamp-qbo/v1', '/paid_invoices', array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'methods' => 'GET',
</span><span class="cx" style="display: block; padding: 0 10px"> 'callback' => array( __CLASS__, 'rest_callback_paid_invoices' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -594,6 +599,89 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * REST: /invoice_pdf
+ *
+ * Saves a PDF copy of the invoice and returns the filename
+ *
+ * Note: The function that eventually ends up using the file should delete it once it's done with it.
+ *
+ * @param WP_REST_Request $request
+ *
+ * @return string|WP_Error The filename on success, or a WP_Error on failure
+ */
+ public static function rest_callback_invoice_pdf( $request ) {
+ if ( ! self::_is_valid_request( $request ) ) {
+ return new WP_Error( 'unauthorized', 'Unauthorized', array( 'status' => 401 ) );
+ }
+
+ $qbo_request = self::build_qbo_get_invoice_pdf_request( $request->get_param( 'invoice_id' ) );
+ $response = wp_remote_get( $qbo_request['url'], $qbo_request['args'] );
+
+ 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 = wp_remote_retrieve_body( $response );
+ $valid_pdf_header = '%PDF-' === substr( $body, 0, 5 );
+ $valid_pdf_footer = '%%EOF' === substr( $body, strlen( $body ) - 7, 5 );
+
+ if ( $valid_pdf_header && $valid_pdf_footer ) {
+ $filename = sprintf(
+ '%sWPCS-invoice-%d.pdf',
+ get_temp_dir(),
+ $request->get_param( 'invoice_id' )
+ );
+
+ if ( file_put_contents( $filename, $body ) ) {
+ $result = array( 'filename' => $filename );
+ } else {
+ $result = new WP_Error( 'write_error', 'Failed writing PDF to disk.', compact( 'filename', 'body' ) );
+ }
+ } else {
+ $result = new WP_Error( 'invalid_body', 'Response body was not a PDF.', $response );
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * Build a request to send an Invoice via QuickBook's API
+ *
+ * @param int $invoice_id
+ *
+ * @return array
+ */
+ protected static function build_qbo_get_invoice_pdf_request( $invoice_id ) {
+ self::load_options();
+ $oauth = self::_get_oauth();
+ $oauth->set_token( self::$options['auth']['oauth_token'], self::$options['auth']['oauth_token_secret'] );
+
+ $request_url = sprintf(
+ '%s/v3/company/%d/invoice/%d/pdf',
+ self::$api_base_url,
+ rawurlencode( self::$options['auth']['realmId'] ),
+ rawurlencode( $invoice_id )
+ );
+
+ $args = array(
+ 'timeout' => self::REMOTE_REQUEST_TIMEOUT,
+ 'headers' => array(
+ 'Authorization' => $oauth->get_oauth_header( 'GET', $request_url ),
+ 'Accept' => 'application/pdf',
+ 'Content-Type' => 'application/pdf',
+ ),
+ 'body' => '',
+ );
+
+ return array(
+ 'url' => $request_url,
+ 'args' => $args,
+ );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Notify Central that an invoice was created but couldn't be sent to the sponsor
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int $invoice_id
</span></span></pre></div>
<a id="sitestrunkwordcamporgpublic_htmlwpcontentpluginswordcampqboclientwordcampqboclientphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo-client/wordcamp-qbo-client.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo-client/wordcamp-qbo-client.php 2016-05-11 19:06:13 UTC (rev 3118)
+++ sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-qbo-client/wordcamp-qbo-client.php 2016-05-11 23:21:02 UTC (rev 3119)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -433,6 +433,61 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Get the filename for a PDF copy of an invoice
+ *
+ * @param int $invoice_id
+ *
+ * @return WP_Error|string
+ */
+ public static function get_invoice_filename( $invoice_id ) {
+ $request = self::build_invoice_filename_request( $invoice_id );
+ $response = wp_remote_get( $request['url'], $request['args'] );
+
+ if ( is_wp_error( $response ) ) {
+ $result = $response;
+ } else {
+ $result = json_decode( wp_remote_retrieve_body( $response ) );
+
+ if ( $result && is_file( $result->filename ) ) {
+ $result = $result->filename;
+ } else {
+ $result = new WP_Error( 'invalid_filename', 'The filename was not valid.', $result );
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * Build the request to get the filename for a PDF copy of an invoice
+ *
+ * @param array $invoice_ids
+ *
+ * @return array
+ */
+ protected static function build_invoice_filename_request( $invoice_id ) {
+ $params = array(
+ 'invoice_id' => strval( absint( $invoice_id ) ), // absint() to validate, strval() to convert to type expected by API
+ );
+
+ $request_url = self::$api_base . '/invoice_pdf';
+
+ $args = array(
+ 'headers' => array(
+ 'Authorization' => self::_get_auth_header( 'get', $request_url, '', $params ),
+ 'Content-Type' => 'application/json',
+ ),
+ );
+
+ $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
+
+ return array(
+ 'url' => $request_url,
+ 'args' => $args,
+ );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Create an HMAC signature header for a request.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Use with Authorization HTTP header.
</span></span></pre>
</div>
</div>
</body>
</html>