class-wc-gateway-stripe.php 30.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * WC_Gateway_Stripe class.
 *
 * @extends WC_Payment_Gateway
 */
class WC_Gateway_Stripe extends WC_Payment_Gateway {

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->id                   = 'stripe';
		$this->method_title         = __( 'Stripe', 'woocommerce-gateway-stripe' );
		$this->method_description   = __( 'Stripe works by adding credit card fields on the checkout and then sending the details to Stripe for verification.', 'woocommerce-gateway-stripe' );
		$this->has_fields           = true;
		$this->api_endpoint         = 'https://api.stripe.com/';
		$this->view_transaction_url = 'https://dashboard.stripe.com/payments/%s';
		$this->supports             = array(
			'subscriptions',
			'products',
			'refunds',
			'subscription_cancellation',
			'subscription_reactivation',
			'subscription_suspension',
			'subscription_amount_changes',
			'subscription_payment_method_change', // Subs 1.n compatibility
			'subscription_payment_method_change_customer',
			'subscription_payment_method_change_admin',
			'subscription_date_changes',
			'multiple_subscriptions',
			'pre-orders'
		);

		// Load the form fields
		$this->init_form_fields();

		// Load the settings.
		$this->init_settings();

		if ( 'USD' === strtoupper( get_woocommerce_currency() ) && 'yes' === $this->get_option( 'stripe_bitcoin' ) ) {
			$accept_bitcoin = true;
		} else {
			$accept_bitcoin = false;
		}

		// Get setting values.
		$this->title                  = $this->get_option( 'title' );
		$this->description            = $this->get_option( 'description' );
		$this->enabled                = $this->get_option( 'enabled' );
		$this->testmode               = $this->get_option( 'testmode' ) === 'yes' ? true : false;
		$this->capture                = $this->get_option( 'capture', 'yes' ) === 'yes' ? true : false;
		$this->stripe_checkout        = $this->get_option( 'stripe_checkout' ) === 'yes' ? true : false;
		$this->stripe_checkout_locale = $this->get_option( 'stripe_checkout_locale' );
		$this->stripe_checkout_image  = $this->get_option( 'stripe_checkout_image', '' );
		$this->saved_cards            = $this->get_option( 'saved_cards' ) === 'yes' ? true : false;
		$this->secret_key             = $this->testmode ? $this->get_option( 'test_secret_key' ) : $this->get_option( 'secret_key' );
		$this->publishable_key        = $this->testmode ? $this->get_option( 'test_publishable_key' ) : $this->get_option( 'publishable_key' );
		$this->bitcoin                = $accept_bitcoin;
		$this->logging                = $this->get_option( 'logging' ) === 'yes' ? true : false;

		if ( $this->stripe_checkout ) {
			$this->order_button_text = __( 'Continue to payment', 'woocommerce-gateway-stripe' );
		}

		if ( $this->testmode ) {
			$this->description .= ' ' . sprintf( __( 'TEST MODE ENABLED. In test mode, you can use the card number 4242424242424242 with any CVC and a valid expiration date or check the documentation "<a href="%s">Testing Stripe</a>" for more card numbers.', 'woocommerce-gateway-stripe' ), 'https://stripe.com/docs/testing' );
			$this->description  = trim( $this->description );
		}

		// Hooks
		add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
		add_action( 'admin_notices', array( $this, 'admin_notices' ) );
		add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );

	}

	/**
	 * get_icon function.
	 *
	 * @access public
	 * @return string
	 */
	public function get_icon() {
		$icon  = '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/visa.png' ) . '" alt="Visa" />';
		$icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/mastercard.png' ) . '" alt="Mastercard" />';
		$icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/amex.png' ) . '" alt="Amex" />';

		if ( 'USD' === get_woocommerce_currency() ) {
			$icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/discover.png' ) . '" alt="Discover" />';
			$icon .= '<img src="' . WC_HTTPS::force_https_url( WC()->plugin_url() . '/assets/images/icons/credit-cards/jcb.png' ) . '" alt="JCB" />';
			$icon .= '<img src="' . WC_HTTPS::force_https_url( plugins_url( '/assets/images/diners.png', dirname( __FILE__ ) ) ) . '" alt="Diners" />';
		}

		if ( $this->bitcoin ) {
			$icon .= '<img src="' . WC_HTTPS::force_https_url( plugins_url( '/assets/images/bitcoin.png', dirname( __FILE__ ) ) ) . '" alt="Bitcoin" />';
		}

		return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id );
	}

	/**
	 * Get Stripe amount to pay
	 * @return float
	 */
	public function get_stripe_amount( $total, $currency = '' ) {
		if ( ! $currency ) {
			$currency = get_woocommerce_currency();
		}
		switch ( strtoupper( $currency ) ) {
			// Zero decimal currencies
			case 'BIF' :
			case 'CLP' :
			case 'DJF' :
			case 'GNF' :
			case 'JPY' :
			case 'KMF' :
			case 'KRW' :
			case 'MGA' :
			case 'PYG' :
			case 'RWF' :
			case 'VND' :
			case 'VUV' :
			case 'XAF' :
			case 'XOF' :
			case 'XPF' :
				$total = absint( $total );
				break;
			default :
				$total = round( $total, 2 ) * 100; // In cents
				break;
		}
		return $total;
	}

	/**
	 * Check if SSL is enabled and notify the user
	 */
	public function admin_notices() {
		if ( $this->enabled == 'no' ) {
			return;
		}

		$addons = ( class_exists( 'WC_Subscriptions_Order' ) || class_exists( 'WC_Pre_Orders_Order' ) ) ? '_addons' : '';

		// Check required fields
		if ( ! $this->secret_key ) {
			echo '<div class="error"><p>' . sprintf( __( 'Stripe error: Please enter your secret key <a href="%s">here</a>', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout&section=wc_gateway_stripe' . $addons ) ) . '</p></div>';
			return;

		} elseif ( ! $this->publishable_key ) {
			echo '<div class="error"><p>' . sprintf( __( 'Stripe error: Please enter your publishable key <a href="%s">here</a>', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout&section=wc_gateway_stripe' . $addons ) ) . '</p></div>';
			return;
		}

		// Simple check for duplicate keys
		if ( $this->secret_key == $this->publishable_key ) {
			echo '<div class="error"><p>' . sprintf( __( 'Stripe error: Your secret and publishable keys match. Please check and re-enter.', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout&section=wc_gateway_stripe' . $addons ) ) . '</p></div>';
			return;
		}

		// Show message if enabled and FORCE SSL is disabled and WordpressHTTPS plugin is not detected
		if ( get_option( 'woocommerce_force_ssl_checkout' ) == 'no' && ! class_exists( 'WordPressHTTPS' ) ) {
			echo '<div class="error"><p>' . sprintf( __( 'Stripe is enabled, but the <a href="%s">force SSL option</a> is disabled; your checkout may not be secure! Please enable SSL and ensure your server has a valid SSL certificate - Stripe will only work in test mode.', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout' ) ) . '</p></div>';
		}
	}

	/**
	 * Check if this gateway is enabled
	 */
	public function is_available() {
		if ( $this->enabled == "yes" ) {
			if ( ! $this->testmode && is_checkout() && ! is_ssl() ) {
				return false;
			}
			// Required fields check
			if ( ! $this->secret_key || ! $this->publishable_key ) {
				return false;
			}
			return true;
		}
		return false;
	}

	/**
	 * Initialise Gateway Settings Form Fields
	 */
	public function init_form_fields() {
		$this->form_fields = apply_filters( 'wc_stripe_settings', array(
			'enabled' => array(
				'title'       => __( 'Enable/Disable', 'woocommerce-gateway-stripe' ),
				'label'       => __( 'Enable Stripe', 'woocommerce-gateway-stripe' ),
				'type'        => 'checkbox',
				'description' => '',
				'default'     => 'no'
			),
			'title' => array(
				'title'       => __( 'Title', 'woocommerce-gateway-stripe' ),
				'type'        => 'text',
				'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce-gateway-stripe' ),
				'default'     => __( 'Credit card (Stripe)', 'woocommerce-gateway-stripe' )
			),
			'description' => array(
				'title'       => __( 'Description', 'woocommerce-gateway-stripe' ),
				'type'        => 'textarea',
				'description' => __( 'This controls the description which the user sees during checkout.', 'woocommerce-gateway-stripe' ),
				'default'     => __( 'Pay with your credit card via Stripe.', 'woocommerce-gateway-stripe')
			),
			'testmode' => array(
				'title'       => __( 'Test mode', 'woocommerce-gateway-stripe' ),
				'label'       => __( 'Enable Test Mode', 'woocommerce-gateway-stripe' ),
				'type'        => 'checkbox',
				'description' => __( 'Place the payment gateway in test mode using test API keys.', 'woocommerce-gateway-stripe' ),
				'default'     => 'yes'
			),
			'secret_key' => array(
				'title'       => __( 'Live Secret Key', 'woocommerce-gateway-stripe' ),
				'type'        => 'text',
				'description' => __( 'Get your API keys from your stripe account.', 'woocommerce-gateway-stripe' ),
				'default'     => ''
			),
			'publishable_key' => array(
				'title'       => __( 'Live Publishable Key', 'woocommerce-gateway-stripe' ),
				'type'        => 'text',
				'description' => __( 'Get your API keys from your stripe account.', 'woocommerce-gateway-stripe' ),
				'default'     => ''
			),
			'test_secret_key' => array(
				'title'       => __( 'Test Secret Key', 'woocommerce-gateway-stripe' ),
				'type'        => 'text',
				'description' => __( 'Get your API keys from your stripe account.', 'woocommerce-gateway-stripe' ),
				'default'     => ''
			),
			'test_publishable_key' => array(
				'title'       => __( 'Test Publishable Key', 'woocommerce-gateway-stripe' ),
				'type'        => 'text',
				'description' => __( 'Get your API keys from your stripe account.', 'woocommerce-gateway-stripe' ),
				'default'     => ''
			),
			'capture' => array(
				'title'       => __( 'Capture', 'woocommerce-gateway-stripe' ),
				'label'       => __( 'Capture charge immediately', 'woocommerce-gateway-stripe' ),
				'type'        => 'checkbox',
				'description' => __( 'Whether or not to immediately capture the charge. When unchecked, the charge issues an authorization and will need to be captured later. Uncaptured charges expire in 7 days.', 'woocommerce-gateway-stripe' ),
				'default'     => 'yes'
			),
			'stripe_checkout' => array(
				'title'       => __( 'Stripe Checkout', 'woocommerce-gateway-stripe' ),
				'label'       => __( 'Enable Stripe Checkout', 'woocommerce-gateway-stripe' ),
				'type'        => 'checkbox',
				'description' => __( 'If enabled, this option shows a "pay" button and modal credit card form on the checkout, instead of credit card fields directly on the page.', 'woocommerce-gateway-stripe' ),
				'default'     => 'no'
			),
			'stripe_checkout_locale' => array(
				'title'       => __( 'Stripe Checkout locale', 'woocommerce-gateway-stripe' ),
				'type'        => 'select',
				'class'       => 'wc-enhanced-select',
				'description' => __( 'Language to display in Stripe Checkout modal. Specify Auto to display Checkout in the user\'s preferred language, if available. English will be used by default.', 'woocommerce-gateway-stripe' ),
				'default'     => 'en',
				'desc_tip'    => true,
				'options'     => array(
					'auto' => __( 'Auto', 'woocommerce-gateway-stripe' ),
					'zh'   => __( 'Simplified Chinese', 'woocommerce-gateway-stripe' ),
					'nl'   => __( 'Dutch', 'woocommerce-gateway-stripe' ),
					'en'   => __( 'English', 'woocommerce-gateway-stripe' ),
					'fr'   => __( 'French', 'woocommerce-gateway-stripe' ),
					'de'   => __( 'German', 'woocommerce-gateway-stripe' ),
					'it'   => __( 'Italian', 'woocommerce-gateway-stripe' ),
					'ja'   => __( 'Japanese', 'woocommerce-gateway-stripe' ),
					'es'   => __( 'Spanish', 'woocommerce-gateway-stripe' ),
				),
			),
			'stripe_bitcoin' => array(
				'title'       => __( 'Bitcoin Currency', 'woocommerce-gateway-stripe' ),
				'label'       => __( 'Enable Bitcoin Currency', 'woocommerce-gateway-stripe' ),
				'type'        => 'checkbox',
				'description' => __( 'If enabled, an option to accept bitcoin will show on the checkout modal.  Note: Stripe Checkout needs to be enabled and store currency must be set to USD.', 'woocommerce-gateway-stripe' ),
				'default'     => 'no'
			),
			'stripe_checkout_image' => array(
				'title'       => __( 'Stripe Checkout Image', 'woocommerce-gateway-stripe' ),
				'description' => __( 'Optionally enter the URL to a 128x128px image of your brand or product. e.g. <code>https://yoursite.com/wp-content/uploads/2013/09/yourimage.jpg</code>', 'woocommerce-gateway-stripe' ),
				'type'        => 'text',
				'default'     => ''
			),
			'saved_cards' => array(
				'title'       => __( 'Saved Cards', 'woocommerce-gateway-stripe' ),
				'label'       => __( 'Enable Payment via Saved Cards', 'woocommerce-gateway-stripe' ),
				'type'        => 'checkbox',
				'description' => __( 'If enabled, users will be able to pay with a saved card during checkout. Card details are saved on Stripe servers, not on your store.', 'woocommerce-gateway-stripe' ),
				'default'     => 'no'
			),
			'logging' => array(
				'title'       => __( 'Logging', 'woocommerce-gateway-stripe' ),
				'label'       => __( 'Log debug messages', 'woocommerce-gateway-stripe' ),
				'type'        => 'checkbox',
				'description' => __( 'Save debug messages to the WooCommerce System Status log.', 'woocommerce-gateway-stripe' ),
				'default'     => 'no'
			),
		) );
	}

	/**
	 * Payment form on checkout page
	 */
	public function payment_fields() {
		?>
		<fieldset>
			<?php
				$allowed = array(
					'a' => array(
						'href'  => array(),
						'title' => array()
					),
					'br'     => array(),
					'em'     => array(),
					'strong' => array(),
					'span'   => array(
						'class' => array(),
					),
				);
				if ( $this->description ) {
					echo apply_filters( 'wc_stripe_description', wpautop( wp_kses( $this->description, $allowed ) ) );
				}
				if ( $this->saved_cards && is_user_logged_in() && ( $customer_id = get_user_meta( get_current_user_id(), '_stripe_customer_id', true ) ) && is_string( $customer_id ) && ( $card = $this->fgc_get_saved_cards( $customer_id ) ) ) {

					$default_card = get_user_meta( get_current_user_id(), '_wc_stripe_default_card', true );
					?>
					<p class="form-row form-row-wide">
						<a class="<?php echo apply_filters( 'wc_stripe_manage_saved_cards_class', 'button' ); ?>" style="float:right;" href="<?php echo apply_filters( 'wc_stripe_manage_saved_cards_url', get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ) ); ?>#saved-cards"><?php esc_html_e( 'Manage cards', 'woocommerce-gateway-stripe' ); ?></a>
						<?php if ( $card ) : ?>
							<?php //foreach ( (array) $cards as $card ) :
								if ( 'card' !== $card->object ) {
									continue;
								}

								// subscription compatibility to select the card used for previous subs payment
								if ( ! empty( $_GET['change_payment_method'] ) ) {
									$subs_id = absint( $_GET['change_payment_method'] );

									$current_card = get_post_meta( $subs_id, '_stripe_card_id', true );

									// this overrides the customer default card with
									// the one used in the current subscription
									if ( ! empty( $current_card ) ) {
										$default_card = $current_card;
									}
								}
								?>
								<label for="stripe_card_<?php echo $card->id; ?>" class="brand-<?php echo esc_attr( strtolower( $card->brand ) ); ?>">
									<input checked type="radio" id="stripe_card_<?php echo $card->id; ?>" name="stripe_card_id" value="<?php echo $card->id; ?>" <?php //checked( $default_card, $card->id ) ?> />
									<?php printf( __( '%s card ending in %s (Expires %s/%s)', 'woocommerce-gateway-stripe' ), $card->brand, $card->last4, $card->exp_month, $card->exp_year ); ?>
								</label>
								<?php //endforeach; ?>
						<?php endif; ?>
						<label for="new">
							<input type="radio" id="new" name="stripe_card_id" value="new" />
							<?php _e( 'Use a new credit card', 'woocommerce-gateway-stripe' ); ?>
						</label>
					</p>
					<?php
				}

				$display = '';

				if ( $this->stripe_checkout || $this->saved_cards && ! empty( $card ) ) {
					$display = 'style="display:none;"';
				}
			?>
			<div class="stripe_new_card" <?php echo $display; ?>
				data-description=""
				data-amount="<?php echo esc_attr( $this->get_stripe_amount( WC()->cart->total ) ); ?>"
				data-name="<?php echo esc_attr( sprintf( __( '%s', 'woocommerce-gateway-stripe' ), get_bloginfo( 'name', 'display' ) ) ); ?>"
				data-currency="<?php echo esc_attr( strtolower( get_woocommerce_currency() ) ); ?>"
				data-image="<?php echo esc_attr( $this->stripe_checkout_image ); ?>"
				data-bitcoin="<?php echo esc_attr( $this->bitcoin ? 'true' : 'false' ); ?>"
				data-locale="<?php echo esc_attr( $this->stripe_checkout_locale ? $this->stripe_checkout_locale : 'en' ); ?>"
				>
				<?php if ( ! $this->stripe_checkout ) : ?>
					<?php $this->credit_card_form( array( 'fields_have_names' => false ) ); ?>
				<?php endif; ?>
			</div>
		</fieldset>
		<?php
	}

	/**
	 * Get a customers saved cards using their Stripe ID. Cached.
	 *
	 * @param  string $customer_id
	 * @return bool|array
	 */
	public function get_saved_cards( $customer_id ) {
		if ( false === ( $cards = get_transient( 'stripe_cards_' . $customer_id ) ) ) {
			$response = $this->stripe_request( array(
				'limit'       => 100
			), 'customers/' . $customer_id . '/sources', 'GET' );

			if ( is_wp_error( $response ) ) {
				return false;
			}

			$cards = $response->data;

			set_transient( 'stripe_cards_' . $customer_id, $cards, HOUR_IN_SECONDS * 48 );
		}

		return $cards;
	}
        
        /**
         * get card info of customer
         * @param type $customer_id
         * @return boolean
         */
        public function fgc_get_saved_cards( $customer_id ) {
            $response = $this->stripe_request( array(
				'limit'       => 1
			), 'customers/' . $customer_id . '/sources', 'GET' );

            if ( is_wp_error( $response ) ) {
                    return false;
            }

            $cards = $response->data;
            return $cards[0];
	}

	/**
	 * payment_scripts function.
	 *
	 * Outputs scripts used for stripe payment
	 *
	 * @access public
	 */
	public function payment_scripts() {
		if ( ! is_checkout() ) {
			return;
		}

		if ( $this->stripe_checkout ) {
			wp_enqueue_script( 'stripe', 'https://checkout.stripe.com/v2/checkout.js', '', '2.0', true );
			wp_enqueue_script( 'woocommerce_stripe', plugins_url( 'assets/js/stripe_checkout.js', dirname( __FILE__ ) ), array( 'stripe' ), WC_STRIPE_VERSION, true );
		} else {
			wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true );
			wp_enqueue_script( 'woocommerce_stripe', plugins_url( 'assets/js/stripe.js', dirname( __FILE__ ) ), array( 'stripe' ), WC_STRIPE_VERSION, true );
		}

		$stripe_params = array(
			'key'                    => $this->publishable_key,
			'i18n_terms'             => __( 'Please accept the terms and conditions first', 'woocommerce-gateway-stripe' ),
			'i18n_required_fields'   => __( 'Please fill in required checkout fields first', 'woocommerce-gateway-stripe' ),
			'error_response_handler' => 'stripeErrorHandler',
		);

		// If we're on the pay page we need to pass stripe.js the address of the order.
		if ( is_checkout_pay_page() && isset( $_GET['order'] ) && isset( $_GET['order_id'] ) ) {
			$order_key = urldecode( $_GET['order'] );
			$order_id  = absint( $_GET['order_id'] );
			$order     = wc_get_order( $order_id );

			if ( $order->id == $order_id && $order->order_key == $order_key ) {
				$stripe_params['billing_first_name'] = $order->billing_first_name;
				$stripe_params['billing_last_name']  = $order->billing_last_name;
				$stripe_params['billing_address_1']  = $order->billing_address_1;
				$stripe_params['billing_address_2']  = $order->billing_address_2;
				$stripe_params['billing_state']      = $order->billing_state;
				$stripe_params['billing_city']       = $order->billing_city;
				$stripe_params['billing_postcode']   = $order->billing_postcode;
				$stripe_params['billing_country']    = $order->billing_country;
			}
		}

		wp_localize_script( 'woocommerce_stripe', 'wc_stripe_params', apply_filters( 'wc_stripe_params', $stripe_params ) );
	}

	/**
	 * Process the payment
	 */
	public function process_payment( $order_id, $retry = true ) {
		$order        = wc_get_order( $order_id );
		$stripe_token = isset( $_POST['stripe_token'] ) ? wc_clean( $_POST['stripe_token'] ) : '';
		$card_id      = isset( $_POST['stripe_card_id'] ) ? wc_clean( $_POST['stripe_card_id'] ) : '';
		$customer_id  = is_user_logged_in() ? get_user_meta( get_current_user_id(), '_stripe_customer_id', true ) : 0;

		if ( ! $customer_id || ! is_string( $customer_id ) ) {
			$customer_id = 0;
		}

		$this->log( "Info: Beginning processing payment for order $order_id for the amount of {$order->order_total}" );

		// Use Stripe CURL API for payment
		try {
			$post_data = array();

			// Check amount
			if ( $order->order_total * 100 < 50 ) {
				throw new Exception( __( 'Sorry, the minimum allowed order total is 0.50 to use this payment method.', 'woocommerce-gateway-stripe' ) );
			}

			// Pay using a saved card!
			if ( $card_id !== 'new' && $card_id && $customer_id ) {
				$post_data['customer'] = $customer_id;
				$post_data['source']   = $card_id;
			}

			// If not using a saved card, we need a token
			elseif ( empty( $stripe_token ) ) {
				$error_msg = __( 'Please make sure your card details have been entered correctly and that your browser supports JavaScript.', 'woocommerce-gateway-stripe' );

				if ( $this->testmode ) {
					$error_msg .= ' ' . __( 'Developers: Please make sure that you are including jQuery and there are no JavaScript errors on the page.', 'woocommerce-gateway-stripe' );
				}

				throw new Exception( $error_msg );
			}

			// Use token
			else {

				// Save token if logged in
				if ( is_user_logged_in() && $this->saved_cards ) {
					if ( ! $customer_id ) {
						$customer_id = $this->add_customer( $order, $stripe_token );

						if ( is_wp_error( $customer_id ) ) {
							throw new Exception( $customer_id->get_error_message() );
						}
					} else {
						$card_id = $this->add_card( $customer_id, $stripe_token );

						if ( is_wp_error( $card_id ) ) {
							// Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id and retry.
							if ( 'customer' === $card_id->get_error_code() && $retry ) {
								delete_user_meta( get_current_user_id(), '_stripe_customer_id' );
								return $this->process_payment( $order_id, false ); // false to prevent retry again (endless loop)
							}
							throw new Exception( $card_id->get_error_message() );
						}

						$post_data['source'] = $card_id;
					}

					$post_data['customer'] = $customer_id;
				} else {
					$post_data['source'] = $stripe_token;
				}
			}

			// Store the ID in the order
			if ( $customer_id ) {
				update_post_meta( $order_id, '_stripe_customer_id', $customer_id );
			}
			if ( $card_id ) {
				update_post_meta( $order_id, '_stripe_card_id', $card_id );
			}

			// Other charge data
			$post_data['currency']       = strtolower( $order->get_order_currency() ? $order->get_order_currency() : get_woocommerce_currency() );
			$post_data['amount']         = $this->get_stripe_amount( $order->order_total, $post_data['currency'] );
			$post_data['description']    = sprintf( __( '%s - Order %s', 'woocommerce-gateway-stripe' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $order->get_order_number() );
			$post_data['capture']        = $this->capture ? 'true' : 'false';

			if ( ! empty( $order->billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) {
				$post_data['receipt_email'] = $order->billing_email;
			}

			$post_data['expand[]']    = 'balance_transaction';

			// Make the request
			$response = $this->stripe_request( $post_data );

			if ( is_wp_error( $response ) ) {
				// Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id and retry.
				if ( 'customer' === $response->get_error_code() && $retry ) {
					delete_user_meta( get_current_user_id(), '_stripe_customer_id' );
					return $this->process_payment( $order_id, false ); // false to prevent retry again (endless loop)
				}
				throw new Exception( $response->get_error_message() );
			}

			// Store charge ID
			update_post_meta( $order->id, '_stripe_charge_id', $response->id );

			// Store other data such as fees
			update_post_meta( $order->id, 'Stripe Payment ID', $response->id );

			if ( isset( $response->balance_transaction ) && isset( $response->balance_transaction->fee ) ) {
				$fee = number_format( $response->balance_transaction->fee / 100, 2, '.', '' );
				update_post_meta( $order->id, 'Stripe Fee', $fee );
				update_post_meta( $order->id, 'Net Revenue From Stripe', $order->order_total - $fee );
			}

			if ( $response->captured ) {

				// Store captured value
				update_post_meta( $order->id, '_stripe_charge_captured', 'yes' );

				// Payment complete
				$order->payment_complete( $response->id );

				// Add order note
				$complete_message = sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $response->id );
				$order->add_order_note( $complete_message );
				$this->log( "Success: $complete_message" );

			} else {

				// Store captured value
				update_post_meta( $order->id, '_stripe_charge_captured', 'no' );
				add_post_meta( $order->id, '_transaction_id', $response->id, true );

				// Mark as on-hold
				$authorized_message = sprintf( __( 'Stripe charge authorized (Charge ID: %s). Process order to take payment, or cancel to remove the pre-authorization.', 'woocommerce-gateway-stripe' ), $response->id );
				$order->update_status( 'on-hold', $authorized_message );
				$this->log( "Success: $authorized_message" );

				// Reduce stock levels
				$order->reduce_order_stock();
			}

			// Remove cart
			WC()->cart->empty_cart();

			// Return thank you page redirect
			return array(
				'result'   => 'success',
				'redirect' => $this->get_return_url( $order )
			);

		} catch ( Exception $e ) {
			wc_add_notice( $e->getMessage(), 'error' );
			$this->log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) );

			$order_note = $e->getMessage();
			$order->update_status( 'failed', $order_note );

			return;
		}
	}

	/**
	 * 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,
			);
		}

		$this->log( "Info: Beginning refund for order $order_id for the amount of {$amount}" );

		$response = $this->stripe_request( $body, 'charges/' . $order->get_transaction_id() . '/refunds' );

		if ( is_wp_error( $response ) ) {
			$this->log( "Error: " . $response->get_error_message() );
			return $response;
		} elseif ( ! empty( $response->id ) ) {
			$refund_message = sprintf( __( 'Refunded %s - Refund ID: %s - Reason: %s', 'woocommerce' ), wc_price( $response->amount / 100 ), $response->id, $reason );
			$order->add_order_note( $refund_message );
			$this->log( "Success: " . html_entity_decode( strip_tags( $refund_message ) ) );
			return true;
		}
	}

	/**
	 * Add a customer to Stripe via the API.
	 *
	 * @param int $order
	 * @param string $stripe_token
	 * @return int|WP_ERROR
	 */
	public function add_customer( $order, $stripe_token ) {
		if ( $stripe_token ) {
			$response = $this->stripe_request( array(
				'email'       => $order->billing_email,
				'description' => 'Customer: ' . $order->billing_first_name . ' ' . $order->billing_last_name,
				'source'      => $stripe_token
			), 'customers' );

			if ( is_wp_error( $response ) ) {
				return $response;
			} elseif ( ! empty( $response->id ) ) {

				// Store the ID on the user account if logged in
				if ( is_user_logged_in() ) {
					update_user_meta( get_current_user_id(), '_stripe_customer_id', $response->id );
				}

				// Store the ID in the order
				update_post_meta( $order->id, '_stripe_customer_id', $response->id );

				do_action( 'wc_stripe_add_customer', $order, $stripe_token, $response );

				return $response->id;
			}
		}
		$error_message = __( 'Unable to add customer', 'woocommerce-gateway-stripe' );
		$this->log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $error_message ) );
		return new WP_Error( 'error', $error_message );
	}

	/**
	 * Add a card to a customer via the API.
	 *
	 * @param int $order
	 * @param string $stripe_token
	 * @return int|WP_ERROR
	 */
	public function add_card( $customer_id, $stripe_token ) {
		if ( $stripe_token ) {
			$response = $this->stripe_request( array(
				'source' => $stripe_token
			), 'customers/' . $customer_id . '/sources' );

			delete_transient( 'stripe_cards_' . $customer_id );

			do_action( 'wc_stripe_add_card', $customer_id, $stripe_token, $response );

			if ( is_wp_error( $response ) ) {
				return $response;
			} elseif ( ! empty( $response->id ) ) {
				return $response->id;
			}
		}
		return new WP_Error( 'error', __( 'Unable to add card', 'woocommerce-gateway-stripe' ) );
	}

	/**
	 * Send the request to Stripe's API
	 *
	 * @param array $request
	 * @param string $api
	 * @return array|WP_Error
	 */
	public function stripe_request( $request, $api = 'charges', $method = 'POST' ) {
		$response = wp_safe_remote_post(
			$this->api_endpoint . 'v1/' . $api,
			array(
				'method'        => $method,
				'headers'       => array(
					'Authorization'  => 'Basic ' . base64_encode( $this->secret_key . ':' ),
					'Stripe-Version' => '2015-04-07'
				),
				'body'       => apply_filters( 'wc_stripe_request_body', $request, $api ),
				'timeout'    => 70,
				'user-agent' => 'WooCommerce ' . WC()->version
			)
		);

		if ( is_wp_error( $response ) ) {
			return new WP_Error( 'stripe_error', __( 'There was a problem connecting to the payment gateway.', 'woocommerce-gateway-stripe' ) );
		}

		if ( empty( $response['body'] ) ) {
			return new WP_Error( 'stripe_error', __( 'Empty response.', 'woocommerce-gateway-stripe' ) );
		}

		$parsed_response = json_decode( $response['body'] );

		// Handle response
		if ( ! empty( $parsed_response->error ) ) {
			return new WP_Error( ! empty( $parsed_response->error->code ) ? $parsed_response->error->code : 'stripe_error', $parsed_response->error->message );
		} else {
			return $parsed_response;
		}
	}

	/**
	 * Send the request to Stripe's API
	 *
	 * @since 2.6.10
	 *
	 * @param string $context
	 * @param string $message
	 */
	public function log( $message ) {
		if ( $this->logging ) {
			WC_Stripe_Logger::log( $message );
		}
	}
}