2014-11-19 14:34:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2016-01-05 05:23:01 +00:00
|
|
|
exit;
|
2014-11-19 14:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-05 05:23:01 +00:00
|
|
|
* Handles Responses.
|
2014-11-19 14:34:34 +00:00
|
|
|
*/
|
|
|
|
abstract class WC_Gateway_Paypal_Response {
|
|
|
|
|
|
|
|
/** @var bool Sandbox mode */
|
|
|
|
protected $sandbox = false;
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Get the order from the PayPal 'Custom' variable.
|
2015-06-10 10:45:26 +00:00
|
|
|
* @param string $raw_custom JSON Data passed back by PayPal
|
2014-11-19 14:34:34 +00:00
|
|
|
* @return bool|WC_Order object
|
|
|
|
*/
|
2015-06-10 10:45:26 +00:00
|
|
|
protected function get_paypal_order( $raw_custom ) {
|
2016-01-05 05:23:01 +00:00
|
|
|
// We have the data in the correct format, so get the order.
|
2015-06-10 17:08:05 +00:00
|
|
|
if ( ( $custom = json_decode( $raw_custom ) ) && is_object( $custom ) ) {
|
2015-06-10 10:45:26 +00:00
|
|
|
$order_id = $custom->order_id;
|
|
|
|
$order_key = $custom->order_key;
|
2014-11-19 14:34:34 +00:00
|
|
|
|
2015-06-10 17:08:05 +00:00
|
|
|
// Fallback to serialized data if safe. This is @deprecated in 2.3.11
|
|
|
|
} elseif ( preg_match( '/^a:2:{/', $raw_custom ) && ! preg_match( '/[CO]:\+?[0-9]+:"/', $raw_custom ) && ( $custom = maybe_unserialize( $raw_custom ) ) ) {
|
|
|
|
$order_id = $custom[0];
|
|
|
|
$order_key = $custom[1];
|
2014-11-19 14:34:34 +00:00
|
|
|
|
2016-01-05 05:23:01 +00:00
|
|
|
// Nothing was found.
|
2015-06-10 17:08:05 +00:00
|
|
|
} else {
|
2015-04-09 11:00:42 +00:00
|
|
|
WC_Gateway_Paypal::log( 'Error: Order ID and key were not found in "custom".' );
|
2014-11-19 14:34:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:08:05 +00:00
|
|
|
if ( ! $order = wc_get_order( $order_id ) ) {
|
2016-01-05 05:23:01 +00:00
|
|
|
// We have an invalid $order_id, probably because invoice_prefix has changed.
|
2015-06-10 17:08:05 +00:00
|
|
|
$order_id = wc_get_order_id_by_order_key( $order_key );
|
|
|
|
$order = wc_get_order( $order_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $order || $order->order_key !== $order_key ) {
|
|
|
|
WC_Gateway_Paypal::log( 'Error: Order Keys do not match.' );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-19 14:34:34 +00:00
|
|
|
return $order;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Complete order, add transaction ID and note.
|
2014-11-19 14:34:34 +00:00
|
|
|
* @param WC_Order $order
|
2016-01-05 05:23:01 +00:00
|
|
|
* @param string $txn_id
|
|
|
|
* @param string $note
|
2014-11-19 14:34:34 +00:00
|
|
|
*/
|
|
|
|
protected function payment_complete( $order, $txn_id = '', $note = '' ) {
|
|
|
|
$order->add_order_note( $note );
|
|
|
|
$order->payment_complete( $txn_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Hold order and add note.
|
2014-11-19 14:34:34 +00:00
|
|
|
* @param WC_Order $order
|
2016-01-05 05:23:01 +00:00
|
|
|
* @param string $reason
|
2014-11-19 14:34:34 +00:00
|
|
|
*/
|
|
|
|
protected function payment_on_hold( $order, $reason = '' ) {
|
|
|
|
$order->update_status( 'on-hold', $reason );
|
2015-05-13 13:28:17 +00:00
|
|
|
$order->reduce_order_stock();
|
|
|
|
WC()->cart->empty_cart();
|
2014-11-19 14:34:34 +00:00
|
|
|
}
|
|
|
|
}
|