WC_Gateway_Paypal_API_Handler

Handles Refunds and other API requests such as capture.

Defined (1)

The class is defined in the following location(s).

/includes/gateways/paypal/includes/class-wc-gateway-paypal-api-handler.php  
  1. class WC_Gateway_Paypal_API_Handler { 
  2.  
  3. /** @var string API Username */ 
  4. public static $api_username; 
  5.  
  6. /** @var string API Password */ 
  7. public static $api_password; 
  8.  
  9. /** @var string API Signature */ 
  10. public static $api_signature; 
  11.  
  12. /** @var bool Sandbox */ 
  13. public static $sandbox = false; 
  14.  
  15. /** 
  16. * Get capture request args. 
  17. * See https://developer.paypal.com/docs/classic/api/merchant/DoCapture_API_Operation_NVP/. 
  18. * @param WC_Order $order 
  19. * @param float $amount 
  20. * @return array 
  21. */ 
  22. public static function get_capture_request( $order, $amount = null ) { 
  23. $request = array( 
  24. 'VERSION' => '84.0',  
  25. 'SIGNATURE' => self::$api_signature,  
  26. 'USER' => self::$api_username,  
  27. 'PWD' => self::$api_password,  
  28. 'METHOD' => 'DoCapture',  
  29. 'AUTHORIZATIONID' => $order->get_transaction_id(),  
  30. 'AMT' => number_format( is_null( $amount ) ? $order->get_total() : $amount, 2, '.', '' ),  
  31. 'CURRENCYCODE' => $order->get_currency(),  
  32. 'COMPLETETYPE' => 'Complete',  
  33. ); 
  34. return apply_filters( 'woocommerce_paypal_capture_request', $request, $order, $amount ); 
  35.  
  36. /** 
  37. * Get refund request args. 
  38. * @param WC_Order $order 
  39. * @param float $amount 
  40. * @param string $reason 
  41. * @return array 
  42. */ 
  43. public static function get_refund_request( $order, $amount = null, $reason = '' ) { 
  44. $request = array( 
  45. 'VERSION' => '84.0',  
  46. 'SIGNATURE' => self::$api_signature,  
  47. 'USER' => self::$api_username,  
  48. 'PWD' => self::$api_password,  
  49. 'METHOD' => 'RefundTransaction',  
  50. 'TRANSACTIONID' => $order->get_transaction_id(),  
  51. 'NOTE' => html_entity_decode( wc_trim_string( $reason, 255 ), ENT_NOQUOTES, 'UTF-8' ),  
  52. 'REFUNDTYPE' => 'Full',  
  53. ); 
  54. if ( ! is_null( $amount ) ) { 
  55. $request['AMT'] = number_format( $amount, 2, '.', '' ); 
  56. $request['CURRENCYCODE'] = $order->get_currency(); 
  57. $request['REFUNDTYPE'] = 'Partial'; 
  58. return apply_filters( 'woocommerce_paypal_refund_request', $request, $order, $amount, $reason ); 
  59.  
  60. /** 
  61. * Capture an authorization. 
  62. * @param WC_Order $order 
  63. * @param float $amount 
  64. * @return object Either an object of name value pairs for a success, or a WP_ERROR object. 
  65. */ 
  66. public static function do_capture( $order, $amount = null ) { 
  67. $raw_response = wp_safe_remote_post( 
  68. self::$sandbox ? 'https://api-3t.sandbox.paypal.com/nvp' : 'https://api-3t.paypal.com/nvp',  
  69. array( 
  70. 'method' => 'POST',  
  71. 'body' => self::get_capture_request( $order, $amount ),  
  72. 'timeout' => 70,  
  73. 'user-agent' => 'WooCommerce/' . WC()->version,  
  74. 'httpversion' => '1.1',  
  75. ); 
  76.  
  77. WC_Gateway_Paypal::log( 'DoCapture Response: ' . wc_print_r( $raw_response, true ) ); 
  78.  
  79. if ( empty( $raw_response['body'] ) ) { 
  80. return new WP_Error( 'paypal-api', 'Empty Response' ); 
  81. } elseif ( is_wp_error( $raw_response ) ) { 
  82. return $raw_response; 
  83.  
  84. parse_str( $raw_response['body'], $response ); 
  85.  
  86. return (object) $response; 
  87.  
  88. /** 
  89. * Refund an order via PayPal. 
  90. * @param WC_Order $order 
  91. * @param float $amount 
  92. * @param string $reason 
  93. * @return object Either an object of name value pairs for a success, or a WP_ERROR object. 
  94. */ 
  95. public static function refund_transaction( $order, $amount = null, $reason = '' ) { 
  96. $raw_response = wp_safe_remote_post( 
  97. self::$sandbox ? 'https://api-3t.sandbox.paypal.com/nvp' : 'https://api-3t.paypal.com/nvp',  
  98. array( 
  99. 'method' => 'POST',  
  100. 'body' => self::get_refund_request( $order, $amount, $reason ),  
  101. 'timeout' => 70,  
  102. 'user-agent' => 'WooCommerce/' . WC()->version,  
  103. 'httpversion' => '1.1',  
  104. ); 
  105.  
  106. WC_Gateway_Paypal::log( 'Refund Response: ' . wc_print_r( $raw_response, true ) ); 
  107.  
  108. if ( empty( $raw_response['body'] ) ) { 
  109. return new WP_Error( 'paypal-api', 'Empty Response' ); 
  110. } elseif ( is_wp_error( $raw_response ) ) { 
  111. return $raw_response; 
  112.  
  113. parse_str( $raw_response['body'], $response ); 
  114.  
  115. return (object) $response;