Completed
Pull Request — master (#11544)
by Claudio
07:11
created

WC_Gateway_Paypal_Refund   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
rs 10
wmc 4
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get_request() 0 3 1
A refund_order() 0 11 3
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
/**
8
 * Handles Refunds and other API requests such as capture.
9
 * @since 2.7.0
10
 */
11
class WC_Gateway_Paypal_API_Handler {
12
13
	/** @var string API Username */
14
	public static $api_username;
15
16
	/** @var string API Password */
17
	public static $api_password;
18
19
	/** @var string API Signature */
20
	public static $api_signature;
21
22
	/** @var string API Signature */
23
	public static $sandbox = false;
24
25
	/**
26
	 * Get capture request args.
27
	 * See https://developer.paypal.com/docs/classic/api/merchant/DoCapture_API_Operation_NVP/.
28
	 * @param  WC_Order $order
29
	 * @param  float    $amount
30
	 * @param  string   $reason
0 ignored issues
show
Bug introduced by
There is no parameter named $reason. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
31
	 * @return array
32
	 */
33
	public static function get_capture_request( $order, $amount = null ) {
34
		$request = array(
35
			'VERSION'         => '84.0',
36
			'SIGNATURE'       => self::$api_signature,
37
			'USER'            => self::$api_username,
38
			'PWD'             => self::$api_password,
39
			'METHOD'          => 'DoCapture',
40
			'AUTHORIZATIONID' => $order->get_transaction_id(),
41
			'AMT'             => number_format( is_null( $amount ) ? $order->get_total() : $amount, 2, '.', '' ),
42
			'CURRENCYCODE'    => $order->get_order_currency(),
43
			'COMPLETETYPE'    => 'Complete',
44
		);
45
		return apply_filters( 'woocommerce_paypal_capture_request', $request, $order, $amount );
46
	}
47
48
	/**
49
	 * Get refund request args.
50
	 * @param  WC_Order $order
51
	 * @param  float    $amount
52
	 * @param  string   $reason
53
	 * @return array
54
	 */
55
	public static function get_refund_request( $order, $amount = null, $reason = '' ) {
56
		$request = array(
57
			'VERSION'       => '84.0',
58
			'SIGNATURE'     => self::$api_signature,
59
			'USER'          => self::$api_username,
60
			'PWD'           => self::$api_password,
61
			'METHOD'        => 'RefundTransaction',
62
			'TRANSACTIONID' => $order->get_transaction_id(),
63
			'NOTE'          => html_entity_decode( wc_trim_string( $reason, 255 ), ENT_NOQUOTES, 'UTF-8' ),
64
			'REFUNDTYPE'    => 'Full'
65
		);
66
		if ( ! is_null( $amount ) ) {
67
			$request['AMT']          = number_format( $amount, 2, '.', '' );
68
			$request['CURRENCYCODE'] = $order->get_order_currency();
69
			$request['REFUNDTYPE']   = 'Partial';
70
		}
71
		return apply_filters( 'woocommerce_paypal_refund_request', $request, $order, $amount, $reason );
72
	}
73
74
	/**
75
	 * Capture an authorization.
76
	 * @param  WC_Order $order
77
	 * @param  float $amount
78
	 * @return object Either an object of name value pairs for a success, or a WP_ERROR object.
79
	 */
80 View Code Duplication
	public static function do_capture( $order, $amount = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
		$raw_response = wp_safe_remote_post(
82
			self::$sandbox ? 'https://api-3t.sandbox.paypal.com/nvp' : 'https://api-3t.paypal.com/nvp',
83
			array(
84
				'method'      => 'POST',
85
				'body'        => self::get_capture_request( $order, $amount ),
86
				'timeout'     => 70,
87
				'user-agent'  => 'WooCommerce',
88
				'httpversion' => '1.1'
89
			)
90
		);
91
92
		WC_Gateway_Paypal::log( 'DoCapture Response: ' . print_r( $raw_response, true ) );
93
94
		if ( empty( $raw_response['body'] ) ) {
95
			return new WP_Error( 'paypal-api', 'Empty Response' );
96
		} elseif ( is_wp_error( $raw_response ) ) {
97
			return $raw_response;
98
		}
99
100
		parse_str( $raw_response['body'], $response );
101
102
		return (object) $response;
103
	}
104
105
	/**
106
	 * Refund an order via PayPal.
107
	 * @param  WC_Order $order
108
	 * @param  float    $amount
109
	 * @param  string   $reason
110
	 * @return object Either an object of name value pairs for a success, or a WP_ERROR object.
111
	 */
112 View Code Duplication
	public static function refund_transaction( $order, $amount = null, $reason = '' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
		$raw_response = wp_safe_remote_post(
114
			self::$sandbox ? 'https://api-3t.sandbox.paypal.com/nvp' : 'https://api-3t.paypal.com/nvp',
115
			array(
116
				'method'      => 'POST',
117
				'body'        => self::get_refund_request( $order, $amount, $reason ),
118
				'timeout'     => 70,
119
				'user-agent'  => 'WooCommerce',
120
				'httpversion' => '1.1'
121
			)
122
		);
123
124
		WC_Gateway_Paypal::log( 'Refund Response: ' . print_r( $raw_response, true ) );
125
126
		if ( empty( $raw_response['body'] ) ) {
127
			return new WP_Error( 'paypal-api', 'Empty Response' );
128
		} elseif ( is_wp_error( $raw_response ) ) {
129
			return $raw_response;
130
		}
131
132
		parse_str( $raw_response['body'], $response );
133
134
		return (object) $response;
135
	}
136
}
137
138
/**
139
 * Here for backwards compatibility.
140
 * @since 2.7.0
141
 */
142
class WC_Gateway_Paypal_Refund extends WC_Gateway_Paypal_API_Handler {
143
	public static function get_request( $order, $amount = null, $reason = '' ) {
144
		return self::get_refund_request( $order, $amount, $reason );
145
	}
146
	public static function refund_order( $order, $amount = null, $reason = '', $sandbox = false ) {
147
		if ( $sandbox ) {
148
			self::$sandbox = $sandbox;
0 ignored issues
show
Documentation Bug introduced by
The property $sandbox was declared of type string, but $sandbox is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
149
		}
150
		$result = self::refund_transaction( $order, $amount, $reason );
151
		if ( is_wp_error( $result ) ) {
152
			return $result;
153
		} else {
154
			return (array) $result;
155
		}
156
	}
157
}
158