/**
  * Set default card in Stripe
  * @param string $card_id
  */
 public function set_default_card($card_id)
 {
     $response = WC_Stripe_API::request(array('default_source' => sanitize_text_field($card_id)), 'customers/' . $this->get_id(), 'POST');
     $this->clear_cache();
     if (!is_wp_error($response)) {
         do_action('wc_stripe_set_default_card', $this->get_id(), $response);
         return true;
     }
     return false;
 }
 /**
  * Set secret API Key.
  * @param string $key
  */
 public static function set_secret_key($secret_key)
 {
     self::$secret_key = $secret_key;
 }
 /**
  * Process a pre-order payment when the pre-order is released
  * @param WC_Order $order
  * @return void
  */
 public function process_pre_order_release_payment($order)
 {
     try {
         // Define some callbacks if the first attempt fails.
         $retry_callbacks = array('remove_order_source_before_retry', 'remove_order_customer_before_retry');
         while (1) {
             $source = $this->get_order_source($order);
             $response = WC_Stripe_API::request($this->generate_payment_request($order, $source));
             if (is_wp_error($response)) {
                 if (0 === sizeof($retry_callbacks)) {
                     throw new Exception($response->get_error_message());
                 } else {
                     $retry_callback = array_shift($retry_callbacks);
                     call_user_func(array($this, $retry_callback), $order);
                 }
             } else {
                 // Successful
                 $this->process_response($response, $order);
                 break;
             }
         }
     } catch (Exception $e) {
         $order_note = sprintf(__('Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe'), $e->getMessage());
         // Mark order as failed if not already set,
         // otherwise, make sure we add the order note so we can detect when someone fails to check out multiple times
         if (!$order->has_status('failed')) {
             $order->update_status('failed', $order_note);
         } else {
             $order->add_order_note($order_note);
         }
     }
 }
 /**
  * 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;
     }
 }
 /**
  * Cancel pre-auth on refund/cancellation
  *
  * @param  int $order_id
  */
 public function cancel_payment($order_id)
 {
     $order = wc_get_order($order_id);
     if ('stripe' === $order->payment_method) {
         $charge = get_post_meta($order_id, '_stripe_charge_id', true);
         if ($charge) {
             $result = WC_Stripe_API::request(array('amount' => $order->get_total() * 100), 'charges/' . $charge . '/refund');
             if (is_wp_error($result)) {
                 $order->add_order_note(__('Unable to refund charge!', 'woocommerce-gateway-stripe') . ' ' . $result->get_error_message());
             } else {
                 $order->add_order_note(sprintf(__('Stripe charge refunded (Charge ID: %s)', 'woocommerce-gateway-stripe'), $result->id));
                 delete_post_meta($order->id, '_stripe_charge_captured');
                 delete_post_meta($order->id, '_stripe_charge_id');
             }
         }
     }
 }