class-wc-gateway-paypal-api-handler.php
5 years ago
class-wc-gateway-paypal-constants.php
6 months ago
class-wc-gateway-paypal-helper.php
6 months ago
class-wc-gateway-paypal-ipn-handler.php
6 months ago
class-wc-gateway-paypal-notices.php
6 months ago
class-wc-gateway-paypal-pdt-handler.php
6 months ago
class-wc-gateway-paypal-request.php
6 months ago
class-wc-gateway-paypal-response.php
1 year ago
class-wc-gateway-paypal-transact-account-manager.php
6 months ago
class-wc-gateway-paypal-webhook-handler.php
6 months ago
settings-paypal.php
9 months ago
class-wc-gateway-paypal-api-handler.php
203 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WC_Gateway_Paypal_API_Handler file. |
| 4 | * |
| 5 | * @package WooCommerce\Gateways |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Handles Refunds and other API requests such as capture. |
| 14 | * |
| 15 | * @since 3.0.0 |
| 16 | */ |
| 17 | class WC_Gateway_Paypal_API_Handler { |
| 18 | |
| 19 | /** |
| 20 | * API Username |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | public static $api_username; |
| 25 | |
| 26 | /** |
| 27 | * API Password |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | public static $api_password; |
| 32 | |
| 33 | /** |
| 34 | * API Signature |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public static $api_signature; |
| 39 | |
| 40 | /** |
| 41 | * Sandbox |
| 42 | * |
| 43 | * @var bool |
| 44 | */ |
| 45 | public static $sandbox = false; |
| 46 | |
| 47 | /** |
| 48 | * Get capture request args. |
| 49 | * See https://developer.paypal.com/docs/classic/api/merchant/DoCapture_API_Operation_NVP/. |
| 50 | * |
| 51 | * @param WC_Order $order Order object. |
| 52 | * @param float $amount Amount. |
| 53 | * @return array |
| 54 | */ |
| 55 | public static function get_capture_request( $order, $amount = null ) { |
| 56 | $request = array( |
| 57 | 'VERSION' => '84.0', |
| 58 | 'SIGNATURE' => self::$api_signature, |
| 59 | 'USER' => self::$api_username, |
| 60 | 'PWD' => self::$api_password, |
| 61 | 'METHOD' => 'DoCapture', |
| 62 | 'AUTHORIZATIONID' => $order->get_transaction_id(), |
| 63 | 'AMT' => number_format( is_null( $amount ) ? $order->get_total() : $amount, 2, '.', '' ), |
| 64 | 'CURRENCYCODE' => $order->get_currency(), |
| 65 | 'COMPLETETYPE' => 'Complete', |
| 66 | ); |
| 67 | return apply_filters( 'woocommerce_paypal_capture_request', $request, $order, $amount ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get refund request args. |
| 72 | * |
| 73 | * @param WC_Order $order Order object. |
| 74 | * @param float $amount Refund amount. |
| 75 | * @param string $reason Refund reason. |
| 76 | * @return array |
| 77 | */ |
| 78 | public static function get_refund_request( $order, $amount = null, $reason = '' ) { |
| 79 | $request = array( |
| 80 | 'VERSION' => '84.0', |
| 81 | 'SIGNATURE' => self::$api_signature, |
| 82 | 'USER' => self::$api_username, |
| 83 | 'PWD' => self::$api_password, |
| 84 | 'METHOD' => 'RefundTransaction', |
| 85 | 'TRANSACTIONID' => $order->get_transaction_id(), |
| 86 | 'NOTE' => html_entity_decode( wc_trim_string( $reason, 255 ), ENT_NOQUOTES, 'UTF-8' ), |
| 87 | 'REFUNDTYPE' => 'Full', |
| 88 | ); |
| 89 | if ( ! is_null( $amount ) ) { |
| 90 | $request['AMT'] = number_format( $amount, 2, '.', '' ); |
| 91 | $request['CURRENCYCODE'] = $order->get_currency(); |
| 92 | $request['REFUNDTYPE'] = 'Partial'; |
| 93 | } |
| 94 | return apply_filters( 'woocommerce_paypal_refund_request', $request, $order, $amount, $reason ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Capture an authorization. |
| 99 | * |
| 100 | * @param WC_Order $order Order object. |
| 101 | * @param float $amount Amount. |
| 102 | * @return object Either an object of name value pairs for a success, or a WP_ERROR object. |
| 103 | */ |
| 104 | public static function do_capture( $order, $amount = null ) { |
| 105 | $raw_response = wp_safe_remote_post( |
| 106 | self::$sandbox ? 'https://api-3t.sandbox.paypal.com/nvp' : 'https://api-3t.paypal.com/nvp', |
| 107 | array( |
| 108 | 'method' => 'POST', |
| 109 | 'body' => self::get_capture_request( $order, $amount ), |
| 110 | 'timeout' => 70, |
| 111 | 'user-agent' => 'WooCommerce/' . WC()->version, |
| 112 | 'httpversion' => '1.1', |
| 113 | ) |
| 114 | ); |
| 115 | |
| 116 | WC_Gateway_Paypal::log( 'DoCapture Response: ' . wc_print_r( $raw_response, true ) ); |
| 117 | |
| 118 | if ( is_wp_error( $raw_response ) ) { |
| 119 | return $raw_response; |
| 120 | } elseif ( empty( $raw_response['body'] ) ) { |
| 121 | return new WP_Error( 'paypal-api', 'Empty Response' ); |
| 122 | } |
| 123 | |
| 124 | parse_str( $raw_response['body'], $response ); |
| 125 | |
| 126 | return (object) $response; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Refund an order via PayPal. |
| 131 | * |
| 132 | * @param WC_Order $order Order object. |
| 133 | * @param float $amount Refund amount. |
| 134 | * @param string $reason Refund reason. |
| 135 | * @return object Either an object of name value pairs for a success, or a WP_ERROR object. |
| 136 | */ |
| 137 | public static function refund_transaction( $order, $amount = null, $reason = '' ) { |
| 138 | $raw_response = wp_safe_remote_post( |
| 139 | self::$sandbox ? 'https://api-3t.sandbox.paypal.com/nvp' : 'https://api-3t.paypal.com/nvp', |
| 140 | array( |
| 141 | 'method' => 'POST', |
| 142 | 'body' => self::get_refund_request( $order, $amount, $reason ), |
| 143 | 'timeout' => 70, |
| 144 | 'user-agent' => 'WooCommerce/' . WC()->version, |
| 145 | 'httpversion' => '1.1', |
| 146 | ) |
| 147 | ); |
| 148 | |
| 149 | WC_Gateway_Paypal::log( 'Refund Response: ' . wc_print_r( $raw_response, true ) ); |
| 150 | |
| 151 | if ( is_wp_error( $raw_response ) ) { |
| 152 | return $raw_response; |
| 153 | } elseif ( empty( $raw_response['body'] ) ) { |
| 154 | return new WP_Error( 'paypal-api', 'Empty Response' ); |
| 155 | } |
| 156 | |
| 157 | parse_str( $raw_response['body'], $response ); |
| 158 | |
| 159 | return (object) $response; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Here for backwards compatibility. |
| 165 | * |
| 166 | * @since 3.0.0 |
| 167 | */ |
| 168 | class WC_Gateway_Paypal_Refund extends WC_Gateway_Paypal_API_Handler { |
| 169 | /** |
| 170 | * Get refund request args. Proxy to WC_Gateway_Paypal_API_Handler::get_refund_request(). |
| 171 | * |
| 172 | * @param WC_Order $order Order object. |
| 173 | * @param float $amount Refund amount. |
| 174 | * @param string $reason Refund reason. |
| 175 | * |
| 176 | * @return array |
| 177 | */ |
| 178 | public static function get_request( $order, $amount = null, $reason = '' ) { |
| 179 | return self::get_refund_request( $order, $amount, $reason ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Process an order refund. |
| 184 | * |
| 185 | * @param WC_Order $order Order object. |
| 186 | * @param float $amount Refund amount. |
| 187 | * @param string $reason Refund reason. |
| 188 | * @param bool $sandbox Whether to use sandbox mode or not. |
| 189 | * @return object Either an object of name value pairs for a success, or a WP_ERROR object. |
| 190 | */ |
| 191 | public static function refund_order( $order, $amount = null, $reason = '', $sandbox = false ) { |
| 192 | if ( $sandbox ) { |
| 193 | self::$sandbox = $sandbox; |
| 194 | } |
| 195 | $result = self::refund_transaction( $order, $amount, $reason ); |
| 196 | if ( is_wp_error( $result ) ) { |
| 197 | return $result; |
| 198 | } else { |
| 199 | return (array) $result; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 |