/** * Send the request to Stripe's API * * @param array $request * @param string $api * @return array|WP_Error */ public static function request($request, $api = 'charges', $method = 'POST') { WC_Stripe::log("{$api} request: " . print_r($request, true)); $response = wp_safe_remote_post(self::ENDPOINT . $api, array('method' => $method, 'headers' => array('Authorization' => 'Basic ' . base64_encode(self::get_secret_key() . ':'), 'Stripe-Version' => '2016-03-07'), 'body' => apply_filters('woocommerce_stripe_request_body', $request, $api), 'timeout' => 70, 'user-agent' => 'WooCommerce ' . WC()->version)); if (is_wp_error($response) || empty($response['body'])) { WC_Stripe::log("Error Response: " . print_r($response, true)); return new WP_Error('stripe_error', __('There was a problem connecting to the payment gateway.', 'woocommerce-gateway-stripe')); } $parsed_response = json_decode($response['body']); // Handle response if (!empty($parsed_response->error)) { if (!empty($parsed_response->error->param)) { $code = $parsed_response->error->param; } elseif (!empty($parsed_response->error->code)) { $code = $parsed_response->error->code; } else { $code = 'stripe_error'; } return new WP_Error($code, $parsed_response->error->message); } else { return $parsed_response; } }
/** * process_subscription_payment function. * @param mixed $order * @param int $amount (default: 0) * @param string $stripe_token (default: '') * @param bool initial_payment */ public function process_subscription_payment($order = '', $amount = 0) { if ($amount * 100 < 50) { return new WP_Error('stripe_error', __('Sorry, the minimum allowed order total is 0.50 to use this payment method.', 'woocommerce-gateway-stripe')); } // Get source from order $source = $this->get_order_source($order); // If no order source was defined, use user source instead. if (!$source->customer) { $source = $this->get_source($order->customer_user); } // Or fail :( if (!$source->customer) { return new WP_Error('stripe_error', __('Customer not found', 'woocommerce-gateway-stripe')); } WC_Stripe::log("Info: Begin processing subscriotion payment for order {$order->id} for the amount of {$amount}"); // Make the request $request = $this->generate_payment_request($order, $source); $request['capture'] = 'true'; $request['amount'] = $this->get_stripe_amount($amount, $request['currency']); $request['metadata'] = array('payment_type' => 'recurring'); $response = WC_Stripe_API::request($request); // Process valid response if (!is_wp_error($response)) { $this->process_response($response, $order); } return $response; }
/** * Refund a charge * @param int $order_id * @param float $amount * @return bool */ public function process_refund($order_id, $amount = null, $reason = '') { $order = wc_get_order($order_id); if (!$order || !$order->get_transaction_id()) { return false; } $body = array(); if (!is_null($amount)) { $body['amount'] = $this->get_stripe_amount($amount); } if ($reason) { $body['metadata'] = array('reason' => $reason); } WC_Stripe::log("Info: Beginning refund for order {$order_id} for the amount of {$amount}"); $response = WC_Stripe_API::request($body, 'charges/' . $order->get_transaction_id() . '/refunds'); if (is_wp_error($response)) { WC_Stripe::log("Error: " . $response->get_error_message()); return $response; } elseif (!empty($response->id)) { $refund_message = sprintf(__('Refunded %s - Refund ID: %s - Reason: %s', 'woocommerce-gateway-stripe'), wc_price($response->amount / 100), $response->id, $reason); $order->add_order_note($refund_message); WC_Stripe::log("Success: " . html_entity_decode(strip_tags($refund_message))); return true; } }
/** * What rolls down stairs * alone or in pairs, * and over your neighbor's dog? * What's great for a snack, * And fits on your back? * It's log, log, log */ public static function log($message) { if (empty(self::$log)) { self::$log = new WC_Logger(); } self::$log->add('woocommerce-gateway-stripe', $message); if (defined('WP_DEBUG') && WP_DEBUG) { error_log($message); } }