Merge pull request #9946 from shivapoudel/paypal-tweaks

Tweak paypal gateway docblocks and comments
This commit is contained in:
Mike Jolley 2016-01-05 10:09:09 +00:00
commit 7b58db9ecb
6 changed files with 72 additions and 84 deletions

View File

@ -1,9 +1,4 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* PayPal Standard Payment Gateway.
*
@ -15,6 +10,14 @@ if ( ! defined( 'ABSPATH' ) ) {
* @package WooCommerce/Classes/Payment
* @author WooThemes
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC_Gateway_Paypal Class.
*/
class WC_Gateway_Paypal extends WC_Payment_Gateway {
/** @var boolean Whether or not logging is enabled */
@ -41,7 +44,7 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
$this->init_form_fields();
$this->init_settings();
// Define user set variables
// Define user set variables.
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->testmode = 'yes' === $this->get_option( 'testmode', 'no' );
@ -69,7 +72,7 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
/**
* Logging method.
* @param string $message
* @param string $message
*/
public static function log( $message ) {
if ( self::$log_enabled ) {
@ -82,7 +85,6 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
/**
* get_icon function.
*
* @return string
*/
public function get_icon() {
@ -181,7 +183,6 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
/**
* Check if this gateway is enabled and available in the user's country.
*
* @return bool
*/
public function is_valid_for_use() {
@ -213,9 +214,7 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
/**
* Get the transaction URL.
*
* @param WC_Order $order
*
* @return string
*/
public function get_transaction_url( $order ) {
@ -229,8 +228,7 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
/**
* Process the payment and return the result.
*
* @param int $order_id
* @param int $order_id
* @return array
*/
public function process_payment( $order_id ) {
@ -256,10 +254,10 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
/**
* Process a refund if supported.
* @param int $order_id
* @param float $amount
* @param int $order_id
* @param float $amount
* @param string $reason
* @return boolean True or false based on success, or a WP_Error object
* @return boolean True or false based on success, or a WP_Error object
*/
public function process_refund( $order_id, $amount = null, $reason = '' ) {
$order = wc_get_order( $order_id );

View File

@ -1,7 +1,7 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit;
}
include_once( 'class-wc-gateway-paypal-response.php' );
@ -32,10 +32,11 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
if ( ! empty( $_POST ) && $this->validate_ipn() ) {
$posted = wp_unslash( $_POST );
do_action( "valid-paypal-standard-ipn-request", $posted );
do_action( 'valid-paypal-standard-ipn-request', $posted );
exit;
}
wp_die( "PayPal IPN Request Failure", "PayPal IPN", array( 'response' => 500 ) );
wp_die( 'PayPal IPN Request Failure', 'PayPal IPN', array( 'response' => 500 ) );
}
/**
@ -45,10 +46,10 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
public function valid_response( $posted ) {
if ( ! empty( $posted['custom'] ) && ( $order = $this->get_paypal_order( $posted['custom'] ) ) ) {
// Lowercase returned variables
// Lowercase returned variables.
$posted['payment_status'] = strtolower( $posted['payment_status'] );
// Sandbox fix
// Sandbox fix.
if ( isset( $posted['test_ipn'] ) && 1 == $posted['test_ipn'] && 'pending' == $posted['payment_status'] ) {
$posted['payment_status'] = 'completed';
}
@ -82,13 +83,13 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
'user-agent' => 'WooCommerce/' . WC()->version
);
// Post back to get a response
// Post back to get a response.
$response = wp_safe_remote_post( $this->sandbox ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params );
WC_Gateway_Paypal::log( 'IPN Request: ' . print_r( $params, true ) );
WC_Gateway_Paypal::log( 'IPN Response: ' . print_r( $response, true ) );
// check to see if the request was valid
// Check to see if the request was valid.
if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) {
WC_Gateway_Paypal::log( 'Received valid response from PayPal' );
return true;
@ -105,7 +106,7 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
/**
* Check for a valid transaction type.
* @param string $txn_type
* @param string $txn_type
*/
protected function validate_transaction_type( $txn_type ) {
$accepted_types = array( 'cart', 'instant', 'express_checkout', 'web_accept', 'masspay', 'send_money' );
@ -118,15 +119,14 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
/**
* Check currency from IPN matches the order.
* @param WC_Order $order
* @param string $currency
* @param WC_Order $order
* @param string $currency
*/
protected function validate_currency( $order, $currency ) {
// Validate currency
if ( $order->get_order_currency() != $currency ) {
WC_Gateway_Paypal::log( 'Payment error: Currencies do not match (sent "' . $order->get_order_currency() . '" | returned "' . $currency . '")' );
// Put this order on-hold for manual checking
// Put this order on-hold for manual checking.
$order->update_status( 'on-hold', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'woocommerce' ), $currency ) );
exit;
}
@ -134,13 +134,13 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
/**
* Check payment amount from IPN matches the order.
* @param WC_Order $order
* @param WC_Order $order
*/
protected function validate_amount( $order, $amount ) {
if ( number_format( $order->get_total(), 2, '.', '' ) != number_format( $amount, 2, '.', '' ) ) {
WC_Gateway_Paypal::log( 'Payment error: Amounts do not match (gross ' . $amount . ')' );
// Put this order on-hold for manual checking
// Put this order on-hold for manual checking.
$order->update_status( 'on-hold', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'woocommerce' ), $amount ) );
exit;
}
@ -149,22 +149,21 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
/**
* Check receiver email from PayPal. If the receiver email in the IPN is different than what is stored in.
* WooCommerce -> Settings -> Checkout -> PayPal, it will log an error about it.
* @param WC_Order $order
* @param WC_Order $order
*/
protected function validate_receiver_email( $order, $receiver_email ) {
if ( strcasecmp( trim( $receiver_email ), trim( $this->receiver_email ) ) != 0 ) {
WC_Gateway_Paypal::log( "IPN Response is for another account: {$receiver_email}. Your email is {$this->receiver_email}" );
// Put this order on-hold for manual checking
// Put this order on-hold for manual checking.
$order->update_status( 'on-hold', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'woocommerce' ), $receiver_email ) );
exit;
}
}
/**
* Handle a completed payment.
* @param WC_Order $order
* @param WC_Order $order
*/
protected function payment_status_completed( $order, $posted ) {
if ( $order->has_status( 'completed' ) ) {
@ -182,7 +181,7 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
$this->payment_complete( $order, ( ! empty( $posted['txn_id'] ) ? wc_clean( $posted['txn_id'] ) : '' ), __( 'IPN payment completed', 'woocommerce' ) );
if ( ! empty( $posted['mc_fee'] ) ) {
// log paypal transaction fee
// Log paypal transaction fee.
update_post_meta( $order->id, 'PayPal Transaction Fee', wc_clean( $posted['mc_fee'] ) );
}
@ -193,7 +192,7 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
/**
* Handle a pending payment.
* @param WC_Order $order
* @param WC_Order $order
*/
protected function payment_status_pending( $order, $posted ) {
$this->payment_status_completed( $order, $posted );
@ -201,7 +200,7 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
/**
* Handle a failed payment.
* @param WC_Order $order
* @param WC_Order $order
*/
protected function payment_status_failed( $order, $posted ) {
$order->update_status( 'failed', sprintf( __( 'Payment %s via IPN.', 'woocommerce' ), wc_clean( $posted['payment_status'] ) ) );
@ -209,7 +208,7 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
/**
* Handle a denied payment.
* @param WC_Order $order
* @param WC_Order $order
*/
protected function payment_status_denied( $order, $posted ) {
$this->payment_status_failed( $order, $posted );
@ -217,7 +216,7 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
/**
* Handle an expired payment.
* @param WC_Order $order
* @param WC_Order $order
*/
protected function payment_status_expired( $order, $posted ) {
$this->payment_status_failed( $order, $posted );
@ -225,7 +224,7 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
/**
* Handle a voided payment.
* @param WC_Order $order
* @param WC_Order $order
*/
protected function payment_status_voided( $order, $posted ) {
$this->payment_status_failed( $order, $posted );
@ -233,13 +232,13 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
/**
* Handle a refunded order.
* @param WC_Order $order
* @param WC_Order $order
*/
protected function payment_status_refunded( $order, $posted ) {
// Only handle full refunds, not partial
// Only handle full refunds, not partial.
if ( $order->get_total() == ( $posted['mc_gross'] * -1 ) ) {
// Mark order as refunded
// Mark order as refunded.
$order->update_status( 'refunded', sprintf( __( 'Payment %s via IPN.', 'woocommerce' ), strtolower( $posted['payment_status'] ) ) );
$this->send_ipn_email_notification(
@ -251,7 +250,7 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
/**
* Handle a reveral.
* @param WC_Order $order
* @param WC_Order $order
*/
protected function payment_status_reversed( $order, $posted ) {
$order->update_status( 'on-hold', sprintf( __( 'Payment %s via IPN.', 'woocommerce' ), wc_clean( $posted['payment_status'] ) ) );
@ -264,7 +263,7 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
/**
* Handle a cancelled reveral.
* @param WC_Order $order
* @param WC_Order $order
*/
protected function payment_status_canceled_reversal( $order, $posted ) {
$this->send_ipn_email_notification(
@ -294,8 +293,8 @@ class WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_Response {
/**
* Send a notification to the user handling orders.
* @param string $subject
* @param string $message
* @param string $subject
* @param string $message
*/
protected function send_ipn_email_notification( $subject, $message ) {
$new_order_settings = get_option( 'woocommerce_new_order_settings', array() );

View File

@ -1,7 +1,7 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit;
}
include_once( 'class-wc-gateway-paypal-response.php' );
@ -41,7 +41,7 @@ class WC_Gateway_Paypal_PDT_Handler extends WC_Gateway_Paypal_Response {
'user-agent' => 'WooCommerce/' . WC_VERSION
);
// Post back to get a response
// Post back to get a response.
$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 ) {
@ -76,7 +76,7 @@ class WC_Gateway_Paypal_PDT_Handler extends WC_Gateway_Paypal_Response {
$this->payment_complete( $order, $transaction, __( 'PDT payment completed', 'woocommerce' ) );
if ( ! empty( $_REQUEST['mc_fee'] ) ) {
// log paypal transaction fee
// Log paypal transaction fee.
update_post_meta( $order->id, 'PayPal Transaction Fee', wc_clean( $_REQUEST['mc_fee'] ) );
}
}

View File

@ -1,11 +1,11 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit;
}
/**
* Handles refunds.
* Handles Refunds.
*/
class WC_Gateway_Paypal_Refund {
@ -21,8 +21,8 @@ class WC_Gateway_Paypal_Refund {
/**
* Get refund request args.
* @param WC_Order $order
* @param float $amount
* @param string $reason
* @param float $amount
* @param string $reason
* @return array
*/
public static function get_request( $order, $amount = null, $reason = '' ) {
@ -47,9 +47,9 @@ class WC_Gateway_Paypal_Refund {
/**
* Refund an order via PayPal.
* @param WC_Order $order
* @param float $amount
* @param string $reason
* @param boolean $sandbox
* @param float $amount
* @param string $reason
* @param boolean $sandbox
* @return array|wp_error The parsed response from paypal, or a WP_Error object
*/
public static function refund_order( $order, $amount = null, $reason = '', $sandbox = false ) {

View File

@ -1,7 +1,7 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit;
}
/**
@ -38,8 +38,8 @@ class WC_Gateway_Paypal_Request {
/**
* Get the PayPal request URL for an order.
* @param WC_Order $order
* @param boolean $sandbox
* @param WC_Order $order
* @param boolean $sandbox
* @return string
*/
public function get_request_url( $order, $sandbox = false ) {
@ -54,8 +54,7 @@ class WC_Gateway_Paypal_Request {
/**
* Get PayPal Args for passing to PP.
*
* @param WC_Order $order
* @param WC_Order $order
* @return array
*/
protected function get_paypal_args( $order ) {
@ -236,7 +235,6 @@ class WC_Gateway_Paypal_Request {
/**
* Get line items to send to paypal.
*
* @param WC_Order $order
* @return bool
*/
@ -262,12 +260,12 @@ class WC_Gateway_Paypal_Request {
}
}
// Shipping Cost item - paypal only allows shipping per item, we want to send shipping for the order
// Shipping Cost item - paypal only allows shipping per item, we want to send shipping for the order.
if ( $order->get_total_shipping() > 0 && ! $this->add_line_item( sprintf( __( 'Shipping via %s', 'woocommerce' ), $order->get_shipping_method() ), 1, $this->round( $order->get_total_shipping(), $order ) ) ) {
return false;
}
// Check for mismatched totals
// Check for mismatched totals.
if ( $this->number_format( $calculated_total + $order->get_total_tax() + $this->round( $order->get_total_shipping(), $order ) - $this->round( $order->get_total_discount(), $order ), $order ) != $this->number_format( $order->get_total(), $order ) ) {
return false;
}
@ -277,10 +275,10 @@ class WC_Gateway_Paypal_Request {
/**
* Add PayPal Line Item.
* @param string $item_name
* @param integer $quantity
* @param integer $amount
* @param string $item_number
* @param string $item_name
* @param integer $quantity
* @param integer $amount
* @param string $item_number
* @return bool successfully added or not
*/
protected function add_line_item( $item_name, $quantity = 1, $amount = 0, $item_number = '' ) {
@ -320,9 +318,7 @@ class WC_Gateway_Paypal_Request {
/**
* Check if currency has decimals.
*
* @param string $currency
*
* @return bool
*/
protected function currency_has_decimals( $currency ) {
@ -335,10 +331,8 @@ class WC_Gateway_Paypal_Request {
/**
* Round prices.
*
* @param double $price
* @param WC_Order $order
*
* @return double
*/
protected function round( $price, $order ) {
@ -353,10 +347,8 @@ class WC_Gateway_Paypal_Request {
/**
* Format prices.
*
* @param float|int $price
* @param WC_Order $order
*
* @return string
*/
protected function number_format( $price, $order ) {

View File

@ -1,11 +1,11 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit;
}
/**
* Handles refunds.
* Handles Responses.
*/
abstract class WC_Gateway_Paypal_Response {
@ -14,12 +14,11 @@ abstract class WC_Gateway_Paypal_Response {
/**
* Get the order from the PayPal 'Custom' variable.
*
* @param string $raw_custom JSON Data passed back by PayPal
* @return bool|WC_Order object
*/
protected function get_paypal_order( $raw_custom ) {
// We have the data in the correct format, so get the order
// We have the data in the correct format, so get the order.
if ( ( $custom = json_decode( $raw_custom ) ) && is_object( $custom ) ) {
$order_id = $custom->order_id;
$order_key = $custom->order_key;
@ -29,14 +28,14 @@ abstract class WC_Gateway_Paypal_Response {
$order_id = $custom[0];
$order_key = $custom[1];
// Nothing was found
// Nothing was found.
} else {
WC_Gateway_Paypal::log( 'Error: Order ID and key were not found in "custom".' );
return false;
}
if ( ! $order = wc_get_order( $order_id ) ) {
// We have an invalid $order_id, probably because invoice_prefix has changed
// We have an invalid $order_id, probably because invoice_prefix has changed.
$order_id = wc_get_order_id_by_order_key( $order_key );
$order = wc_get_order( $order_id );
}
@ -52,8 +51,8 @@ abstract class WC_Gateway_Paypal_Response {
/**
* Complete order, add transaction ID and note.
* @param WC_Order $order
* @param string $txn_id
* @param string $note
* @param string $txn_id
* @param string $note
*/
protected function payment_complete( $order, $txn_id = '', $note = '' ) {
$order->add_order_note( $note );
@ -63,7 +62,7 @@ abstract class WC_Gateway_Paypal_Response {
/**
* Hold order and add note.
* @param WC_Order $order
* @param string $reason
* @param string $reason
*/
protected function payment_on_hold( $order, $reason = '' ) {
$order->update_status( 'on-hold', $reason );