File "class-wc-gateway-paypal-pdt-handler.php"
Full path: /home/kosmetik/public_html/wp-content/plugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-pdt-handler.php
File
size: 5.13 B
MIME-type: text/x-php
Charset: utf-8
Download Open Edit Advanced Editor Back
<?php
use Automattic\Jetpack\Constants;
if (!defined('ABSPATH')) {
exit;
}
require_once dirname(__FILE__) . '/class-wc-gateway-paypal-response.php';
class WC_Gateway_Paypal_PDT_Handler extends WC_Gateway_Paypal_Response
{
protected $identity_token;
protected $receiver_email;
public function __construct($sandbox = false, $identity_token = '')
{
add_action('woocommerce_thankyou_paypal', array($this, 'check_response_for_order'));
$this->identity_token = $identity_token;
$this->sandbox = $sandbox;
}
public function set_receiver_email($receiver_email = '')
{
$this->receiver_email = $receiver_email;
}
protected function validate_transaction($transaction)
{
$pdt = array('body' => array('cmd' => '_notify-synch', 'tx' => $transaction, 'at' => $this->identity_token), 'timeout' => 60, 'httpversion' => '1.1', 'user-agent' => 'WooCommerce/' . Constants::get_constant('WC_VERSION'));
$response = wp_safe_remote_post($this->sandbox ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $pdt);
if (is_wp_error($response) || strpos($response['body'], 'SUCCESS') !== 0) {
return false;
}
$transaction_result = array_map('wc_clean', array_map('urldecode', explode("\n", $response['body'])));
$transaction_results = array();
foreach ($transaction_result as $line) {
$line = explode('=', $line);
$transaction_results[$line[0]] = isset($line[1]) ? $line[1] : '';
}
if (!empty($transaction_results['charset']) && function_exists('iconv')) {
foreach ($transaction_results as $key => $value) {
$transaction_results[$key] = iconv($transaction_results['charset'], 'utf-8', $value);
}
}
return $transaction_results;
}
public function check_response()
{
global $wp;
$order_id = apply_filters('woocommerce_thankyou_order_id', absint($wp->query_vars['order-received']));
$this->check_response_for_order($order_id);
}
public function check_response_for_order($wc_order_id)
{
if (empty($_REQUEST['tx'])) {
return;
}
$wc_order = wc_get_order($wc_order_id);
if (!$wc_order->needs_payment()) {
return;
}
$transaction = wc_clean(wp_unslash($_REQUEST['tx']));
$transaction_result = $this->validate_transaction($transaction);
if ($transaction_result) {
$status = strtolower($transaction_result['payment_status']);
$amount = isset($transaction_result['mc_gross']) ? $transaction_result['mc_gross'] : 0;
$order = $this->get_paypal_order($transaction_result['custom']);
if (!$order) {
return;
}
if ($wc_order->get_id() !== $order->get_id()) {
WC_Gateway_Paypal::log(sprintf(__('Received PDT notification for order %1$d on endpoint for order %2$d.', 'woocommerce'), $order->get_id(), $wc_order_id), 'error');
return;
}
if (0 !== strcasecmp(trim($transaction_result['receiver_email']), trim($this->receiver_email))) {
WC_Gateway_Paypal::log(sprintf(__('Received PDT notification for another account: %1$s. Order ID: %2$d.', 'woocommerce'), $transaction_result['receiver_email'], $order->get_id()), 'error');
return;
}
WC_Gateway_Paypal::log('PDT Transaction Status: ' . wc_print_r($status, true));
$order->add_meta_data('_paypal_status', $status);
$order->set_transaction_id($transaction);
if ('completed' === $status) {
if (number_format($order->get_total(), 2, '.', '') !== number_format($amount, 2, '.', '')) {
WC_Gateway_Paypal::log('Payment error: Amounts do not match (amt ' . $amount . ')', 'error');
$this->payment_on_hold($order, sprintf(__('Validation error: PayPal amounts do not match (amt %s).', 'woocommerce'), $amount));
} else {
if (!empty($transaction_result['mc_fee'])) {
$order->add_meta_data('PayPal Transaction Fee', wc_clean($transaction_result['mc_fee']));
}
if (!empty($transaction_result['payment_type'])) {
$order->add_meta_data('Payment type', wc_clean($transaction_result['payment_type']));
}
$this->payment_complete($order, $transaction, __('PDT payment completed', 'woocommerce'));
}
} else {
if ('authorization' === $transaction_result['pending_reason']) {
$this->payment_on_hold($order, __('Payment authorized. Change payment status to processing or complete to capture funds.', 'woocommerce'));
} else {
$this->payment_on_hold($order, sprintf(__('Payment pending (%s).', 'woocommerce'), $transaction_result['pending_reason']));
}
}
} else {
WC_Gateway_Paypal::log('Received invalid response from PayPal PDT');
}
}
}