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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
| <?php
/*
Plugin Name: WP Gift Certificate Reloaded - PayPal Payment Module
Plugin URI: http://www.wpgiftcertificatereloaded.com/
Description: PayPal payment module for WP Gift Certificate Reloaded.
Author: GC Development Team
Author URI: http://www.wpgiftcertificatereloaded.com/
Version: 1.42
*/
add_action('plugins_loaded', 'wpgcpaypal_init');
function wpgcpaypal_init() {
if (class_exists('wpgc_class') && class_exists('wpgc_module')) {
class wpgc_paypal extends wpgc_module {
var $currency_list = array("USD", "AUD", "BRL", "CAD", "CHF", "CZK", "DKK", "EUR", "GBP", "HKD", "HUF", "ILS", "JPY", "MXN", "MYR", "NOK", "NZD", "PHP", "PLN", "SEK", "SGD", "THB", "TRY", "TWD");
function __construct() {
if (function_exists('load_plugin_textdomain')) {
load_plugin_textdomain('wpgc', false, dirname(plugin_basename(__FILE__)).'/languages/');
}
$this->module = array("id" => "paypal", "title" => __('PayPal Payment Module', 'wpgc'), "settings" => true);
$this->options = array (
"enable_paypal" => "off",
"paypal_id" => "",
"paypal_sandbox" => "off"
);
$this->get_options();
add_filter('wpgc_check_options', array(&$this, 'check_options'));
add_filter('wpgc_currency_payment_methods', array(&$this, 'currency_payment_methods'), 10, 2);
add_filter('wpgc_payment_logo', array(&$this, 'payment_logo'), 10, 2);
add_filter('wpgc_currencies', array(&$this, 'currencies'));
add_filter('wpgc_active_payment_methods', array(&$this, 'active_payment_methods'));
add_filter('wpgc_available_payment_methods', array(&$this, 'available_payment_methods'));
add_action('wpgc_echo_payment_form', array(&$this, 'payment_form'));
if (is_admin()) {
add_action('wpgc_echo_options_box', array(&$this, 'admin_settings'));
add_action('wpgc_save_options', array(&$this, 'save_options'));
} else {
add_action('init', array(&$this, 'front_init'));
}
parent::__construct();
}
function check_options($_errors) {
$errors = array();
if ($this->options['enable_paypal'] == "on") {
if ((!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i", $this->options['paypal_id']) && !preg_match("/^([A-Z0-9]+)$/i", $this->options['paypal_id'])) || strlen($this->options['paypal_id']) == 0) $errors[] = __('PayPal ID must be valid e-mail address or Merchant ID', 'wpgc');
}
return array_merge($_errors, $errors);
}
function admin_settings() {
global $wpdb;
echo '
<div class="postbox" id="wpgc-settings-'.$this->module['id'].'">
<!--<div class="handlediv" title="Click to toggle"><br></div>-->
<h3 class="hndle" style="cursor: default;"><span>'.__('PayPal Settings', 'wpgc').'</span></h3>
<div class="inside">
<table class="wpgc_useroptions">
<tr>
<th>'.__('Enable', 'wpgc').':</th>
<td><input type="checkbox" id="'.$this->field_name('enable_paypal').'" name="'.$this->field_name('enable_paypal').'" '.($this->options['enable_paypal'] == "on" ? 'checked="checked"' : '').'"> '.__('Accept payments via PayPal', 'wpgc').'<br /><em>'.__('Please tick checkbox if you would like to accept payments via PayPal.', 'wpgc').'</em></td>
</tr>
<tr>
<th>'.__('PayPal ID', 'wpgc').':</th>
<td><input type="text" id="'.$this->field_name('paypal_id').'" name="'.$this->field_name('paypal_id').'" value="'.htmlspecialchars($this->options['paypal_id'], ENT_QUOTES).'" class="widefat"><br /><em>'.__('Please enter valid PayPal e-mail or <a href="https://www.paypal.com/webapps/customerprofile/summary.view" traget="_blank">Merchant ID</a>, all payments are sent to this account.', 'wpgc').'</em></td>
</tr>
<tr>
<th>'.__('Sandbox mode', 'wpgc').':</th>
<td><input type="checkbox" id="'.$this->field_name('paypal_sandbox').'" name="'.$this->field_name('paypal_sandbox').'" '.($this->options['paypal_sandbox'] == "on" ? 'checked="checked"' : '').'> '.__('Enable PayPal sandbox mode', 'wpgc').'<br /><em>'.__('Please tick checkbox if you would like to test PayPal service.', 'wpgc').'</em></td>
</tr>
</table>
<div class="alignright">
<input type="submit" class="wpgc_button button-primary" name="submit" value="'.__('Update Settings', 'wpgc').'">
</div>
<br class="clear">
</div>
</div>';
}
function save_options() {
global $wpdb, $wpgc;
$this->populate_options();
if (isset($_POST[$this->field_name('enable_paypal')])) $this->options['enable_paypal'] = "on";
else $this->options['enable_paypal'] = "off";
if (isset($_POST[$this->field_name('paypal_sandbox')])) $this->options['paypal_sandbox'] = "on";
else $this->options['paypal_sandbox'] = "off";
$this->update_options();
}
function active_payment_methods($_methods = array()) {
if ($this->options['enable_paypal'] == "on") {
$_methods[$this->module['id']] = array(
'title' => 'PayPal',
'logo' => plugins_url('images/logo_paypal.png', __FILE__),
'logo_width' => 85,
'logo_height' => 24,
'logo_vertical_shift' => 1
);
}
return $_methods;
}
function currency_payment_methods($_methods = array(), $_currency = '') {
if (empty($_currency)) unset($_methods[$this->module['id']]);
else if (!in_array($_currency, $this->currency_list)) unset($_methods[$this->module['id']]);
return $_methods;
}
function payment_logo($_logo = '', $_module = '') {
if ($_module == $this->module['id']) {
$_logo .= '<img class="wpgc_payment_logo" src="'.plugins_url('images/logo_paypal.png', __FILE__).'" alt="PayPal">';
}
return $_logo;
}
function payment_form($_data = array()) {
if (isset($_data['payment_method']) && $_data['payment_method'] == $this->module['id']) {
$qty = sizeof($_data['owners'])*$_data['qty'];
echo '
<form id="'.$_data['prefix'].'payment_form" action="'.(($this->options['paypal_sandbox'] == "on") ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr').'" method="post" style="display: none;">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="'.$this->options['paypal_id'].'">
<input type="hidden" name="charset" value="utf-8">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="shipping" value="0">
<input type="hidden" name="tax" value="0.00">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="item_name" value="'.$_data['campaign_title'].($qty > 1 ? ' ('.$qty.' '.__('persons', 'wpgc').')' : '').'">
<input type="hidden" name="item_number" value="'.$_data['campaign_id'].'">
<input type="hidden" name="amount" value="'.$_data['total_price'].'">
<input type="hidden" name="currency_code" value="'.$_data['currency'].'">
<input type="hidden" name="custom" value="'.$_data['tx_str'].'">
<input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynow_LG.gif:NonHostedGuest">
<input type="hidden" name="return" value="'.$_data['return_url'].'">
<input type="hidden" name="cancel_return" value="http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"].'">
<input type="hidden" name="notify_url" value="'.get_bloginfo("wpurl").'/?wpgc-ipn='.$this->module['id'].'">
<input type="submit" id="'.$_data['prefix'].'buynow" value="Submit">
</form>';
}
}
function available_payment_methods($_methods = array()) {
$_methods[] = $this->module['id'];
return $_methods;
}
function currencies($_currencies = array()) {
$_currencies = array_unique(array_merge($_currencies, $this->currency_list));
sort($_currencies);
$_currencies = array_unique(array_merge(array("USD"), $_currencies));
return $_currencies;
}
function front_init() {
global $wpgc, $wpdb;
if (isset($_GET['wpgc-ipn']) && $_GET['wpgc-ipn'] == $this->module['id']) {
$request = "cmd=_notify-validate";
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$request .= "&".$key."=".$value;
}
$paypalurl = $this->options['paypal_sandbox'] == "on" ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $paypalurl);
//BOF IPN - HTTP 1.1 LINE ADDED
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
//EOF IPN - HTTP 1.1 LINE ADDED
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//BOF IPN - HTTP 1.1 LINE ADDED
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
//EOF IPN - HTTP 1.1 LINE ADDED
$result = curl_exec($ch);
curl_close($ch);
if (substr(trim($result), 0, 8) != "VERIFIED") die();
$item_number = stripslashes($_POST['item_number']);
$item_name = stripslashes($_POST['item_name']);
$payment_status = stripslashes($_POST['payment_status']);
$transaction_type = stripslashes($_POST['txn_type']);
$txn_id = stripslashes($_POST['txn_id']);
$seller_paypal = stripslashes($_POST['business']);
$seller_id = stripslashes($_POST['receiver_id']);
$payer_paypal = stripslashes($_POST['payer_email']);
$gross_total = stripslashes($_POST['mc_gross']);
$mc_currency = stripslashes($_POST['mc_currency']);
$payer_name = stripslashes($_POST['first_name']).' '.stripslashes($_POST['last_name']);
$tx_str = stripslashes($_POST['custom']);
$tx_str = preg_replace('/[^a-zA-Z0-9\-]/', '', $tx_str);
//print_r($_POST);exit;
$payer_status = stripslashes($_POST['payer_status']);
$certificates = $wpdb->get_results("SELECT t1.*, t2.title AS campaign_title FROM ".$wpdb->prefix."wpgc_certificates t1 LEFT JOIN ".$wpdb->prefix."wpgc_campaigns t2 ON t2.id = t1.campaign_id WHERE t1.tx_str = '".$tx_str."'", ARRAY_A);
if ($transaction_type == "web_accept" && $payment_status == "Completed") {
//print_r($certificates);exit;
if (empty($certificates)) $payment_status = "Unrecognized";
else {
if (empty($seller_paypal)) {
$tx_details = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."wpgc_payment_transactions WHERE details LIKE '%txn_id=".$txn_id."%' AND payment_status != 'Unrecognized'", ARRAY_A);
if (intval($tx_details["id"]) != 0) $seller_paypal = $this->options['paypal_id'];
}
if ((strtolower($seller_paypal) != strtolower($this->options['paypal_id'])) && (strtolower($seller_id) != strtolower($this->options['paypal_id']))) $payment_status = "Unrecognized";
else {
$total = 0;
foreach ($certificates as $certificate) {
$total += floatval($certificate["price"]);
$currency = $certificate["currency"];
$campaign_title = $certificate["campaign_title"];
$email = $certificate["email"];
}
if (floatval($gross_total) < floatval($total) || $mc_currency != $currency) $payment_status = "Unrecognized";
}
}
}
$sql = "INSERT INTO ".$wpdb->prefix."wpgc_payment_transactions (
tx_str, payer_name, payer_email, gross, currency, payment_status, transaction_type, details, created, deleted) VALUES (
'".@$wpdb->prepare($tx_str)."',
'".@$wpdb->prepare($payer_name)."',
'".@$wpdb->prepare($payer_paypal)."',
'".floatval($gross_total)."',
'".$mc_currency."',
'".$payment_status."',
'PayPal: ".$transaction_type."',
'".addslashes($request)."',
'".time()."', '0'
)";
//BOF OWNER EMAIL
$owner_name = $certificates[0]['owner'];
$owner_email = $certificates[0]['email'];
//EOF OWNER EMAIL
$wpdb->query($sql);
if ($transaction_type == "web_accept") {
if ($payment_status == "Completed") {
$sql = "UPDATE ".$wpdb->prefix."wpgc_certificates SET status = '".WPGC_STATUS_ACTIVE."', blocked = '0' WHERE tx_str = '".$tx_str."'";
$wpdb->query($sql);
$tags = array('{payer_name}', '{payer_email}', '{certificate_title}', '{certificate_url}');
$vals = array($payer_name, $payer_paypal, $campaign_title, get_bloginfo('wpurl').'/?wpgc-show='.$tx_str);
$body = str_replace($tags, $vals, $wpgc->options['success_email_body']);
$mail_headers = "Content-Type: text/plain; charset=utf-8\r\n";
$mail_headers .= "From: ".$wpgc->options['from_name']." <".$wpgc->options['from_email'].">\r\n";
$mail_headers .= "X-Mailer: PHP/".phpversion()."\r\n";
wp_mail(/*$email*/$payer_paypal, $wpgc->options['success_email_subject'], $body, $mail_headers);
//BOF OWNER EMAIL
if($owner_email!=$payer_paypal) {
$tags_owner = array('{payer_name}', '{payer_email}','{owner_name}', '{owner_email}', '{certificate_title}', '{certificate_url}');
$vals_owner = array($payer_name, $payer_paypal,$owner_name, $owner_email, $campaign_title, get_bloginfo('wpurl').'/?wpgc-show='.$tx_str);
$body_owner = str_replace($tags_owner, $vals_owner, $wpgc->options['success_email_body_owner']);
wp_mail($owner_email, $wpgc->options['success_email_subject_owner'], $body_owner, $mail_headers);
}
//EOF OWNER EMAIL
$body = str_replace($tags, $vals, __('Dear Administrator!', 'wpgc').PHP_EOL.PHP_EOL.__('We would like to inform you that {payer_name} ({payer_email}) purchased certificate "{certificate_title}" via PayPal. The buyer received the following certificates:', 'wpgc').PHP_EOL.'{certificate_url}'.PHP_EOL.PHP_EOL.__('Thanks,', 'wpgc').PHP_EOL.'WPGC');
$mail_headers = "Content-Type: text/plain; charset=utf-8\r\n";
$mail_headers .= "From: ".$wpgc->options['from_name']." <".$wpgc->options['from_email'].">\r\n";
$mail_headers .= "X-Mailer: PHP/".phpversion()."\r\n";
wp_mail($wpgc->options['owner_email'], __('Certificate purchased', 'wpgc'), $body, $mail_headers);
} else if ($payment_status == "Failed" || $payment_status == "Pending" || $payment_status == "Processed" || $payment_status == "Unrecognized") {
$sql = "UPDATE ".$wpdb->prefix."wpgc_certificates SET status = '".WPGC_STATUS_ACTIVE."', blocked = '1' WHERE tx_str = '".$tx_str."'";
$wpdb->query($sql);
$tags = array('{payer_name}', '{payer_email}', '{payment_status}');
$vals = array($payer_name, $payer_paypal, $payment_status);
$body = str_replace($tags, $vals, $wpgc->options['failed_email_body']);
$mail_headers = "Content-Type: text/plain; charset=utf-8\r\n";
$mail_headers .= "From: ".$wpgc->options['from_name']." <".$wpgc->options['from_email'].">\r\n";
$mail_headers .= "X-Mailer: PHP/".phpversion()."\r\n";
wp_mail(/*$email*/$payer_paypal, $wpgc->options['failed_email_subject'], $body, $mail_headers);
//BOF OWNER EMAIL
if($owner_email!=$payer_paypal) {
$tags_owner = array('{payer_name}', '{payer_email}','{owner_name}', '{owner_email}', '{payment_status}');
$vals_owner = array($payer_name, $payer_paypal,$owner_name, $owner_email, $payment_status);
$body_owner = str_replace($tags_owner, $vals_owner,$wpgc->options['failed_email_body_owner']);
wp_mail($owner_email, $wpgc->options['failed_email_subject_owner'], $body_owner, $mail_headers);
}
//EOF OWNER EMAIL
$body = str_replace($tags, $vals, __('Dear Administrator!', 'wpgc').PHP_EOL.PHP_EOL.__('We would like to inform you that {payer_name} ({payer_email}) paid for gift certificate via PayPal. This is non-completed payment.', 'wpgc').PHP_EOL.__('Payment ststus: {payment_status}', 'wpgc').PHP_EOL.PHP_EOL.__('Gift certificates were not generated.', 'wpgc').PHP_EOL.PHP_EOL.__('Thanks,', 'wpgc').PHP_EOL.'WPGC');
$mail_headers = "Content-Type: text/plain; charset=utf-8\r\n";
$mail_headers .= "From: ".$wpgc->options['from_name']." <".$wpgc->options['from_email'].">\r\n";
$mail_headers .= "X-Mailer: PHP/".phpversion()."\r\n";
wp_mail($wpgc->options['owner_email'], __('Non-completed PayPal payment received', 'wpgc'), $body, $mail_headers);
}
}
exit;
}
}
}
if (!defined('WPGC_VERSION') || WPGC_VERSION < 2.00) {
add_action('admin_notices', 'wpgcpaypal_oldwpgc');
} else {
$wpgc_paypal = new wpgc_paypal();
}
} else {
add_action('admin_notices', 'wpgcpaypal_nowpgc');
}
}
function wpgcpaypal_nowpgc() {
echo '
<div class="updated"><p>'.__('Please activate <strong>WP Gift Certificate Reloaded</strong> plugin to use <strong>WP Gift Certificate Reloaded - PayPal Payment Module</strong>. Get your copy of WP Gift Certificate Reloaded <strong><a href="http://www.wpgiftcertificatereloaded.com/">here</a></strong>.', 'wpgc').'</p></div>';
}
function wpgcpaypal_oldwpgc() {
echo '
<div class="updated"><p>'.__('<strong>WP Gift Certificate Reloaded - PayPal Payment Module</strong> requires <strong>WP Gift Certificate Reloaded</strong> ver <strong>2.00</strong> or higher to be installed. Get latest copy of WP Gift Certificate Reloaded <strong><a href="http://www.wpgiftcertificatereloaded.com/">here</a></strong>.', 'wpgc').'</p></div>';
}
?> |
Partager