-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
235 lines (189 loc) · 7.71 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* HelpScout EDD integration.
*
* This code is based in large part on an example provided by HelpScout and then modified for Easy Digital Downloads and WP.
*
* Based off of Yoast's original integration.
*/
// We use core, so we include it.
require '../wp-load.php';
// Require the settings file for the secret key
require './settings.php';
class EDD_Help_Scout {
private $input = false;
/**
* Returns the requested HTTP header.
*
* @param string $header
* @return bool|string
*/
private function getHeader( $header ) {
if ( isset( $_SERVER[$header] ) ) {
return $_SERVER[$header];
}
return false;
}
/**
* Retrieve the JSON input
*
* @return bool|string
*/
private function getJsonString() {
if ( $this->input === false ) {
$this->input = @file_get_contents( 'php://input' );
}
return $this->input;
}
/**
* Generate the signature based on the secret key, to compare in isSignatureValid
*
* @return bool|string
*/
private function generateSignature() {
$str = $this->getJsonString();
if ( $str ) {
return base64_encode( hash_hmac( 'sha1', $str, HELPSCOUT_SECRET_KEY, true ) );
}
return false;
}
/**
* Returns true if the current request is a valid webhook issued from Help Scout, false otherwise.
*
* @return boolean
*/
private function isSignatureValid() {
$signature = $this->generateSignature();
if ( !$signature || !$this->getHeader( 'HTTP_X_HELPSCOUT_SIGNATURE' ) )
return false;
return $signature == $this->getHeader( 'HTTP_X_HELPSCOUT_SIGNATURE' );
}
/**
* Create a response.
*
* @return array
*/
public function getResponse() {
$ret = array( 'html' => '' );
if ( !$this->isSignatureValid() ) {
return array( 'html' => 'Invalid signature' );
}
$data = json_decode( $this->input, true );
// do some stuff
$ret['html'] = $this->fetchHtml( $data );
// Used for debugging
// $ret['html'] = '<pre>'.print_r($data,1).'</pre>' . $ret['html'];
return $ret;
}
/**
* Generate output for the response.
*
* @param $data
* @return string
*/
private function fetchHtml( $data ) {
global $wpdb;
/* Ignore own email address */
if ( isset( $data['customer']['emails'] ) && is_array( $data['customer']['emails'] ) ) {
if(($key = array_search(HELPSCOUT_EMAIL, $messages)) !== false) {
unset($data['customer']['emails'][$key]);
}
} else {
if ( $data['customer']['email'] == HELPSCOUT_EMAIL ) {
return 'Cannot query customer licenses. E-mail from ' . HELPSCOUT_EMAIL;
}
}
if ( isset( $data['customer']['emails'] ) && is_array( $data['customer']['emails'] ) ) {
$email_query = "IN (";
foreach ( $data['customer']['emails'] as $email ) {
$email_query .= "'" . $email . "',";
}
$email_query = rtrim( $email_query, ',' );
$email_query .= ')';
} else {
$email_query = "= '" . $data['customer']['email'] . "'";
}
$query = "SELECT pm2.post_id, pm2.meta_value, p.post_status FROM $wpdb->postmeta pm, $wpdb->postmeta pm2, $wpdb->posts p WHERE pm.meta_key = '_edd_payment_user_email' AND pm.meta_value $email_query AND pm.post_id = pm2.post_id AND pm2.meta_key = '_edd_payment_meta' AND pm.post_id = p.ID AND p.post_status NOT IN ('failed','pending') ORDER BY pm.post_id DESC LIMIT 20";
$results = $wpdb->get_results( $query );
if ( !$results ) {
$fuzzy_results = true;
$query = "SELECT pm.post_id, pm.meta_value, p.post_status FROM $wpdb->postmeta pm, $wpdb->posts p WHERE pm.meta_key = '_edd_payment_meta' AND pm.meta_value LIKE '%%" . $data['customer']['fname'] . "%%' AND pm.meta_value LIKE '%%" . $data['customer']['lname'] . "%%' AND pm.post_id = p.ID AND p.post_status NOT IN ('failed','pending') ORDER BY pm.post_id DESC LIMIT 20";
$results = $wpdb->get_results( $query );
}
if ( !$results ) {
return 'No license data found.';
}
$orders = array();
foreach ( $results as $result ) {
$order = array();
$order['link'] = '<a target="_blank" href="' . get_admin_url( null, 'edit.php?post_type=download&page=edd-payment-history&view=view-order-details&id=' . $result->post_id ) . '">#' . $result->post_id . '</a>';
$post = get_post( $result->post_id );
$purchase = maybe_unserialize( $result->meta_value );
$user_info = maybe_unserialize( $purchase['user_info'] );
$order['date'] = date_i18n( get_option( 'date_format' ) . ', ' . get_option( 'time_format' ), strtotime( $post->post_date ) );
unset( $post );
$order['id'] = $result->post_id;
$order['status'] = $result->post_status;
$order['amount'] = edd_get_payment_amount( $result->post_id );
$order['payment_method'] = edd_get_payment_gateway( $result->post_id );
$order['email'] = $user_info['email'];
$order['name'] = $user_info['first_name'] . ' ' . $user_info['last_name'];
if ( 'paypal' == $order['payment_method'] ) {
// Grab the PayPal transaction ID and link the transaction to PayPal
$notes = edd_get_payment_notes( $result->post_id );
foreach ( $notes as $note ) {
if ( preg_match( '/^PayPal Transaction ID: ([^\s]+)/', $note->comment_content, $match ) )
$order['paypal_transaction_id'] = $match[1];
}
$order['payment_method'] = '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=' . esc_url( $order['paypal_transaction_id'] ) . '" target="_blank">PayPal</a>';
} else if ( 'stripe' == $order['payment_method'] ) {
// Grab the PayPal transaction ID and link the transaction to PayPal
$notes = edd_get_payment_notes( $result->post_id );
foreach ( $notes as $note ) {
if ( preg_match( '/^Stripe Charge ID: ([^\s]+)/', $note->comment_content, $match ) )
$order['stripe_charge_id'] = $match[1];
}
$order['payment_method'] = '<a href="https:/stripe.com/payments/' . esc_url( $order['stripe_charge_id'] ) . '" target="_blank">Stripe</a>';
}
$downloads = edd_get_payment_meta_downloads( $result->post_id );
if ( $downloads ) {
foreach ( $downloads as $download ) {
$id = isset( $purchase['cart_details'] ) ? $download['id'] : $download;
$licensing = new EDD_Software_Licensing();
if ( get_post_meta( $id, '_edd_sl_enabled', true ) ) {
$license = $licensing->get_license_by_purchase( $order['id'], $id );
$order['downloads'][] = '<strong>' . get_the_title( $id ) . "</strong><br/>"
. edd_get_price_option_name( $id, $download['options']['price_id'] ) . '<br/>'
. get_post_meta( $license->ID, '_edd_sl_key', true ) . '<br/>';
} else {
$order['downloads'][] = '<strong>' . get_the_title( $id ) . "</strong><br/>";
}
}
}
$orders[] = $order;
}
$output = '';
if ($fuzzy_results === true) {
$output .= '<p>Matches based on customer name:</p>';
}
foreach ( $orders as $order ) {
$output .= '<strong><i class="icon-cart"></i> ' . $order['link'] . '</strong>';
if ( $order['status'] != 'publish' )
$output .= ' - <span style="color:orange;font-weight:bold;">' . $order['status'] . '</span>';
$output .= '<p><span class="muted">' . $order['date'] . '</span><br/>';
if ($fuzzy_results === true) {
$output .= $order['name'] . '<br/>' . $order['email'] . '<p>';
}
$output .= '$' . $order['amount'] . ' - ' . $order['payment_method'] . '</p>';
$output .= '<p><i class="icon-pointer"></i><a target="_blank" href="' . add_query_arg( array( 'edd-action' => 'email_links', 'purchase_id' => $order['id'] ), admin_url( 'edit.php?post_type=download&page=edd-payment-history' ) ) . '">' . __( 'Resend Purchase Receipt', 'edd' ) . '</a></p>';
$output .= '<ul>';
foreach ( $order['downloads'] as $download ) {
$output .= '<li>' . $download . '</li>';
}
$output .= '</ul>';
}
return $output;
}
}
$eddhelpscout = new EDD_Help_Scout();
echo json_encode( $eddhelpscout->getResponse() );