2014-08-15 09:56:19 +00:00
|
|
|
<?php
|
2014-09-20 19:42:58 +00:00
|
|
|
|
2014-08-15 09:56:19 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simplify Commerce Gateway for subscriptions
|
|
|
|
*
|
2014-09-02 13:21:42 +00:00
|
|
|
* @class WC_Addons_Gateway_Simplify_Commerce
|
2014-08-15 09:56:19 +00:00
|
|
|
* @extends WC_Gateway_Simplify_Commerce
|
|
|
|
* @since 2.2.0
|
|
|
|
* @version 1.0.0
|
|
|
|
* @package WooCommerce/Classes/Payment
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
2014-09-02 13:21:42 +00:00
|
|
|
class WC_Addons_Gateway_Simplify_Commerce extends WC_Gateway_Simplify_Commerce {
|
2014-08-15 09:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
|
2014-09-02 14:11:21 +00:00
|
|
|
if ( class_exists( 'WC_Subscriptions_Order' ) ) {
|
2015-07-22 00:17:10 +00:00
|
|
|
add_action( 'woocommerce_scheduled_subscription_payment_' . $this->id, array( $this, 'scheduled_subscription_payment' ), 10, 2 );
|
2015-07-22 01:00:00 +00:00
|
|
|
add_action( 'woocommerce_subscription_failing_payment_method_updated_' . $this->id, array( $this, 'update_failing_payment_method' ), 10, 2 );
|
2015-07-21 23:49:45 +00:00
|
|
|
|
2015-07-22 00:46:00 +00:00
|
|
|
add_action( 'wcs_resubscribe_order_created', array( $this, 'delete_resubscribe_meta' ), 10 );
|
|
|
|
|
2015-07-21 23:49:45 +00:00
|
|
|
// Allow store managers to manually set Simplify as the payment method on a subscription
|
|
|
|
add_filter( 'woocommerce_subscription_payment_meta', array( $this, 'add_subscription_payment_meta' ), 10, 2 );
|
|
|
|
add_filter( 'woocommerce_subscription_validate_payment_meta', array( $this, 'validate_subscription_payment_meta' ), 10, 2 );
|
2014-09-02 14:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( class_exists( 'WC_Pre_Orders_Order' ) ) {
|
|
|
|
add_action( 'wc_pre_orders_process_pre_order_completion_payment_' . $this->id, array( $this, 'process_pre_order_release_payment' ) );
|
|
|
|
}
|
2015-01-12 13:43:27 +00:00
|
|
|
|
|
|
|
add_filter( 'woocommerce_simplify_commerce_hosted_args', array( $this, 'hosted_payment_args' ), 10, 2 );
|
|
|
|
add_action( 'woocommerce_api_wc_addons_gateway_simplify_commerce', array( $this, 'return_handler' ) );
|
2014-08-15 09:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-12 13:43:27 +00:00
|
|
|
* Hosted payment args
|
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
* @param int $order_id
|
2014-08-15 09:56:19 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2015-01-12 13:43:27 +00:00
|
|
|
public function hosted_payment_args( $args, $order_id ) {
|
|
|
|
if ( ( $this->order_contains_subscription( $order_id ) ) || ( $this->order_contains_pre_order( $order_id ) && WC_Pre_Orders_Order::order_requires_payment_tokenization( $order_id ) ) ) {
|
|
|
|
$args['operation'] = 'create.token';
|
|
|
|
}
|
|
|
|
|
|
|
|
$args['redirect-url'] = WC()->api_request_url( 'WC_Addons_Gateway_Simplify_Commerce' );
|
|
|
|
|
|
|
|
return $args;
|
|
|
|
}
|
2014-09-02 14:11:21 +00:00
|
|
|
|
2015-01-12 13:43:27 +00:00
|
|
|
/**
|
|
|
|
* Check if order contains subscriptions.
|
|
|
|
*
|
|
|
|
* @param int $order_id
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function order_contains_subscription( $order_id ) {
|
2015-07-22 00:52:58 +00:00
|
|
|
return function_exists( 'wcs_order_contains_subscription' ) && ( wcs_order_contains_subscription( $order_id ) || wcs_order_contains_renewal( $order_id ) );
|
2015-01-12 13:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if order contains pre-orders.
|
|
|
|
*
|
|
|
|
* @param int $order_id
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function order_contains_pre_order( $order_id ) {
|
|
|
|
return class_exists( 'WC_Pre_Orders_Order' ) && WC_Pre_Orders_Order::order_contains_pre_order( $order_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the subscription
|
|
|
|
*
|
|
|
|
* @param WC_Order $order
|
|
|
|
* @param string $cart_token
|
2015-03-27 15:15:40 +00:00
|
|
|
* @uses Simplify_ApiException
|
|
|
|
* @uses Simplify_BadRequestException
|
2015-01-12 13:43:27 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function process_subscription( $order, $cart_token = '' ) {
|
2014-09-02 14:11:21 +00:00
|
|
|
try {
|
2015-01-12 13:43:27 +00:00
|
|
|
if ( empty( $cart_token ) ) {
|
2014-09-02 14:11:21 +00:00
|
|
|
$error_msg = __( 'Please make sure your card details have been entered correctly and that your browser supports JavaScript.', 'woocommerce' );
|
|
|
|
|
|
|
|
if ( 'yes' == $this->sandbox ) {
|
|
|
|
$error_msg .= ' ' . __( 'Developers: Please make sure that you\'re including jQuery and there are no JavaScript errors on the page.', 'woocommerce' );
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Simplify_ApiException( $error_msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create customer
|
|
|
|
$customer = Simplify_Customer::createCustomer( array(
|
2015-01-12 13:43:27 +00:00
|
|
|
'token' => $cart_token,
|
2014-09-02 14:11:21 +00:00
|
|
|
'email' => $order->billing_email,
|
2015-07-08 21:00:19 +00:00
|
|
|
'name' => trim( $order->get_formatted_billing_full_name() ),
|
2014-09-02 14:11:21 +00:00
|
|
|
'reference' => $order->id
|
|
|
|
) );
|
|
|
|
|
|
|
|
if ( is_object( $customer ) && '' != $customer->id ) {
|
2015-07-22 00:17:10 +00:00
|
|
|
$this->save_subscription_meta( $order->id, $customer->id );
|
2014-09-02 14:11:21 +00:00
|
|
|
} else {
|
|
|
|
$error_msg = __( 'Error creating user in Simplify Commerce.', 'woocommerce' );
|
|
|
|
|
|
|
|
throw new Simplify_ApiException( $error_msg );
|
|
|
|
}
|
|
|
|
|
2015-07-22 00:17:10 +00:00
|
|
|
$payment_response = $this->process_subscription_payment( $order, $order->get_total() );
|
2014-09-02 14:11:21 +00:00
|
|
|
|
2015-04-15 12:48:56 +00:00
|
|
|
if ( is_wp_error( $payment_response ) ) {
|
2014-09-02 14:11:21 +00:00
|
|
|
throw new Exception( $payment_response->get_error_message() );
|
|
|
|
} else {
|
|
|
|
// Remove cart
|
|
|
|
WC()->cart->empty_cart();
|
|
|
|
|
|
|
|
// Return thank you page redirect
|
|
|
|
return array(
|
|
|
|
'result' => 'success',
|
|
|
|
'redirect' => $this->get_return_url( $order )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch ( Simplify_ApiException $e ) {
|
|
|
|
if ( $e instanceof Simplify_BadRequestException && $e->hasFieldErrors() && $e->getFieldErrors() ) {
|
|
|
|
foreach ( $e->getFieldErrors() as $error ) {
|
|
|
|
wc_add_notice( $error->getFieldName() . ': "' . $error->getMessage() . '" (' . $error->getErrorCode() . ')', 'error' );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
wc_add_notice( $e->getMessage(), 'error' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'result' => 'fail',
|
|
|
|
'redirect' => ''
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-22 00:17:10 +00:00
|
|
|
/**
|
|
|
|
* Store the customer and card IDs on the order and subscriptions in the order
|
|
|
|
*
|
|
|
|
* @param int $order_id
|
|
|
|
* @param string $customer_id
|
|
|
|
*/
|
|
|
|
protected function save_subscription_meta( $order_id, $customer_id ) {
|
|
|
|
|
|
|
|
$customer_id = wc_clean( $customer_id );
|
|
|
|
|
|
|
|
update_post_meta( $order_id, '_simplify_customer_id', $customer_id );
|
|
|
|
|
|
|
|
// Also store it on the subscriptions being purchased in the order
|
|
|
|
foreach( wcs_get_subscriptions_for_order( $order_id ) as $subscription ) {
|
|
|
|
update_post_meta( $subscription->id, '_simplify_customer_id', $customer_id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-02 14:11:21 +00:00
|
|
|
/**
|
|
|
|
* Process the pre-order
|
|
|
|
*
|
2015-01-12 13:43:27 +00:00
|
|
|
* @param WC_Order $order
|
|
|
|
* @param string $cart_token
|
2015-03-27 15:15:40 +00:00
|
|
|
* @uses Simplify_ApiException
|
|
|
|
* @uses Simplify_BadRequestException
|
2014-09-02 14:11:21 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2015-01-12 13:43:27 +00:00
|
|
|
protected function process_pre_order( $order, $cart_token = '' ) {
|
|
|
|
if ( WC_Pre_Orders_Order::order_requires_payment_tokenization( $order->id ) ) {
|
2014-08-15 09:56:19 +00:00
|
|
|
|
|
|
|
try {
|
2014-09-02 14:11:21 +00:00
|
|
|
if ( $order->order_total * 100 < 50 ) {
|
|
|
|
$error_msg = __( 'Sorry, the minimum allowed order total is 0.50 to use this payment method.', 'woocommerce' );
|
|
|
|
|
|
|
|
throw new Simplify_ApiException( $error_msg );
|
|
|
|
}
|
|
|
|
|
2015-01-12 13:43:27 +00:00
|
|
|
if ( empty( $cart_token ) ) {
|
2014-08-15 09:56:19 +00:00
|
|
|
$error_msg = __( 'Please make sure your card details have been entered correctly and that your browser supports JavaScript.', 'woocommerce' );
|
|
|
|
|
|
|
|
if ( 'yes' == $this->sandbox ) {
|
|
|
|
$error_msg .= ' ' . __( 'Developers: Please make sure that you\'re including jQuery and there are no JavaScript errors on the page.', 'woocommerce' );
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Simplify_ApiException( $error_msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create customer
|
|
|
|
$customer = Simplify_Customer::createCustomer( array(
|
2015-01-12 13:43:27 +00:00
|
|
|
'token' => $cart_token,
|
2014-08-15 09:56:19 +00:00
|
|
|
'email' => $order->billing_email,
|
2015-07-08 21:00:19 +00:00
|
|
|
'name' => trim( $order->get_formatted_billing_full_name() ),
|
2014-08-15 09:56:19 +00:00
|
|
|
'reference' => $order->id
|
|
|
|
) );
|
|
|
|
|
|
|
|
if ( is_object( $customer ) && '' != $customer->id ) {
|
|
|
|
$customer_id = wc_clean( $customer->id );
|
|
|
|
|
|
|
|
// Store the customer ID in the order
|
2015-01-12 13:43:27 +00:00
|
|
|
update_post_meta( $order->id, '_simplify_customer_id', $customer_id );
|
2014-08-15 09:56:19 +00:00
|
|
|
} else {
|
|
|
|
$error_msg = __( 'Error creating user in Simplify Commerce.', 'woocommerce' );
|
|
|
|
|
|
|
|
throw new Simplify_ApiException( $error_msg );
|
|
|
|
}
|
|
|
|
|
2014-09-02 14:11:21 +00:00
|
|
|
// Reduce stock levels
|
|
|
|
$order->reduce_order_stock();
|
2014-08-15 09:56:19 +00:00
|
|
|
|
2014-09-02 14:11:21 +00:00
|
|
|
// Remove cart
|
|
|
|
WC()->cart->empty_cart();
|
2014-08-15 09:56:19 +00:00
|
|
|
|
2014-09-02 14:11:21 +00:00
|
|
|
// Is pre ordered!
|
|
|
|
WC_Pre_Orders_Order::mark_order_as_pre_ordered( $order );
|
2014-08-15 09:56:19 +00:00
|
|
|
|
2014-09-02 14:11:21 +00:00
|
|
|
// Return thank you page redirect
|
|
|
|
return array(
|
|
|
|
'result' => 'success',
|
|
|
|
'redirect' => $this->get_return_url( $order )
|
|
|
|
);
|
2014-08-15 09:56:19 +00:00
|
|
|
|
|
|
|
} catch ( Simplify_ApiException $e ) {
|
|
|
|
if ( $e instanceof Simplify_BadRequestException && $e->hasFieldErrors() && $e->getFieldErrors() ) {
|
|
|
|
foreach ( $e->getFieldErrors() as $error ) {
|
|
|
|
wc_add_notice( $error->getFieldName() . ': "' . $error->getMessage() . '" (' . $error->getErrorCode() . ')', 'error' );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
wc_add_notice( $e->getMessage(), 'error' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'result' => 'fail',
|
|
|
|
'redirect' => ''
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2015-01-12 13:43:27 +00:00
|
|
|
return parent::process_standard_payments( $order, $cart_token );
|
2014-08-15 09:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-02 14:11:21 +00:00
|
|
|
/**
|
|
|
|
* Process the payment
|
|
|
|
*
|
|
|
|
* @param int $order_id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function process_payment( $order_id ) {
|
2015-01-12 13:43:27 +00:00
|
|
|
$cart_token = isset( $_POST['simplify_token'] ) ? wc_clean( $_POST['simplify_token'] ) : '';
|
|
|
|
$order = wc_get_order( $order_id );
|
|
|
|
|
2014-09-02 14:11:21 +00:00
|
|
|
// Processing subscription
|
2015-07-22 01:00:00 +00:00
|
|
|
if ( 'standard' == $this->mode && ( $this->order_contains_subscription( $order->id ) || ( function_exists( 'wcs_is_subscription' ) && wcs_is_subscription( $order_id ) ) ) ) {
|
2015-01-12 13:43:27 +00:00
|
|
|
return $this->process_subscription( $order, $cart_token );
|
2014-09-02 14:11:21 +00:00
|
|
|
|
|
|
|
// Processing pre-order
|
2015-01-12 13:43:27 +00:00
|
|
|
} elseif ( 'standard' == $this->mode && $this->order_contains_pre_order( $order->id ) ) {
|
|
|
|
return $this->process_pre_order( $order, $cart_token );
|
2014-09-02 14:11:21 +00:00
|
|
|
|
|
|
|
// Processing regular product
|
|
|
|
} else {
|
|
|
|
return parent::process_payment( $order_id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 09:56:19 +00:00
|
|
|
/**
|
|
|
|
* process_subscription_payment function.
|
|
|
|
*
|
|
|
|
* @param WC_order $order
|
2014-09-07 23:37:55 +00:00
|
|
|
* @param integer $amount (default: 0)
|
2015-03-27 15:15:40 +00:00
|
|
|
* @uses Simplify_BadRequestException
|
2014-08-15 09:56:19 +00:00
|
|
|
* @return bool|WP_Error
|
|
|
|
*/
|
2015-03-27 15:15:40 +00:00
|
|
|
public function process_subscription_payment( $order, $amount = 0 ) {
|
2015-04-13 13:46:11 +00:00
|
|
|
if ( 0 == $amount ) {
|
|
|
|
// Payment complete
|
|
|
|
$order->payment_complete();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 09:56:19 +00:00
|
|
|
|
|
|
|
if ( $amount * 100 < 50 ) {
|
|
|
|
return new WP_Error( 'simplify_error', __( 'Sorry, the minimum allowed order total is 0.50 to use this payment method.', 'woocommerce' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$customer_id = get_post_meta( $order->id, '_simplify_customer_id', true );
|
|
|
|
|
|
|
|
if ( ! $customer_id ) {
|
|
|
|
return new WP_Error( 'simplify_error', __( 'Customer not found', 'woocommerce' ) );
|
|
|
|
}
|
|
|
|
|
2015-03-11 21:49:57 +00:00
|
|
|
try {
|
|
|
|
// Charge the customer
|
|
|
|
$payment = Simplify_Payment::createPayment( array(
|
|
|
|
'amount' => $amount * 100, // In cents
|
|
|
|
'customer' => $customer_id,
|
2015-07-22 00:17:10 +00:00
|
|
|
'description' => sprintf( __( '%s - Order #%s', 'woocommerce' ), esc_html( get_bloginfo( 'name', 'display' ) ), $order->get_order_number() ),
|
2015-03-11 21:49:57 +00:00
|
|
|
'currency' => strtoupper( get_woocommerce_currency() ),
|
|
|
|
'reference' => $order->id,
|
|
|
|
'card.addressCity' => $order->billing_city,
|
|
|
|
'card.addressCountry' => $order->billing_country,
|
|
|
|
'card.addressLine1' => $order->billing_address_1,
|
|
|
|
'card.addressLine2' => $order->billing_address_2,
|
|
|
|
'card.addressState' => $order->billing_state,
|
|
|
|
'card.addressZip' => $order->billing_postcode
|
|
|
|
) );
|
|
|
|
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
|
|
|
$error_message = $e->getMessage();
|
|
|
|
|
|
|
|
if ( $e instanceof Simplify_BadRequestException && $e->hasFieldErrors() && $e->getFieldErrors() ) {
|
|
|
|
$error_message = '';
|
|
|
|
foreach ( $e->getFieldErrors() as $error ) {
|
|
|
|
$error_message .= ' ' . $error->getFieldName() . ': "' . $error->getMessage() . '" (' . $error->getErrorCode() . ')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$order->add_order_note( sprintf( __( 'Simplify payment error: %s', 'woocommerce' ), $error_message ) );
|
|
|
|
|
|
|
|
return new WP_Error( 'simplify_payment_declined', $e->getMessage(), array( 'status' => $e->getCode() ) );
|
|
|
|
}
|
2014-08-15 09:56:19 +00:00
|
|
|
|
|
|
|
if ( 'APPROVED' == $payment->paymentStatus ) {
|
|
|
|
// Payment complete
|
|
|
|
$order->payment_complete( $payment->id );
|
|
|
|
|
|
|
|
// Add order note
|
|
|
|
$order->add_order_note( sprintf( __( 'Simplify payment approved (ID: %s, Auth Code: %s)', 'woocommerce' ), $payment->id, $payment->authCode ) );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
$order->add_order_note( __( 'Simplify payment declined', 'woocommerce' ) );
|
|
|
|
|
2014-09-02 14:11:21 +00:00
|
|
|
return new WP_Error( 'simplify_payment_declined', __( 'Payment was declined - please try another card.', 'woocommerce' ) );
|
2014-08-15 09:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scheduled_subscription_payment function.
|
|
|
|
*
|
|
|
|
* @param float $amount_to_charge The amount to charge.
|
2015-07-22 00:17:10 +00:00
|
|
|
* @param WC_Order $renewal_order A WC_Order object created to record the renewal payment.
|
2014-08-15 09:56:19 +00:00
|
|
|
*/
|
2015-07-22 00:17:10 +00:00
|
|
|
public function scheduled_subscription_payment( $amount_to_charge, $renewal_order ) {
|
|
|
|
$result = $this->process_subscription_payment( $renewal_order, $amount_to_charge );
|
2014-08-15 09:56:19 +00:00
|
|
|
|
|
|
|
if ( is_wp_error( $result ) ) {
|
2015-07-22 00:17:10 +00:00
|
|
|
$renewal_order->update_status( 'failed', sprintf( __( 'Simplify Transaction Failed (%s)', 'woocommerce' ), $result->get_error_message() ) );
|
2014-08-15 09:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the customer_id for a subscription after using Simplify to complete a payment to make up for
|
|
|
|
* an automatic renewal payment which previously failed.
|
|
|
|
*
|
2015-07-22 01:00:00 +00:00
|
|
|
* @param WC_Subscription $subscription The subscription for which the failing payment method relates.
|
2014-08-15 09:56:19 +00:00
|
|
|
* @param WC_Order $renewal_order The order which recorded the successful payment (to make up for the failed automatic payment).
|
|
|
|
*/
|
2015-07-22 01:00:00 +00:00
|
|
|
public function update_failing_payment_method( $subscription, $renewal_order ) {
|
2015-08-20 20:51:08 +00:00
|
|
|
update_post_meta( $subscription->id, '_simplify_customer_id', get_post_meta( $renewal_order->id, '_simplify_customer_id', true ) );
|
2014-08-15 09:56:19 +00:00
|
|
|
}
|
2014-09-02 14:11:21 +00:00
|
|
|
|
2015-07-21 23:49:45 +00:00
|
|
|
/**
|
|
|
|
* Include the payment meta data required to process automatic recurring payments so that store managers can
|
|
|
|
* manually set up automatic recurring payments for a customer via the Edit Subscription screen in Subscriptions v2.0+.
|
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
* @param array $payment_meta associative array of meta data required for automatic payments
|
|
|
|
* @param WC_Subscription $subscription An instance of a subscription object
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function add_subscription_payment_meta( $payment_meta, $subscription ) {
|
|
|
|
|
|
|
|
$payment_meta[ $this->id ] = array(
|
|
|
|
'post_meta' => array(
|
|
|
|
'_simplify_customer_id' => array(
|
|
|
|
'value' => get_post_meta( $subscription->id, '_simplify_customer_id', true ),
|
|
|
|
'label' => 'Simplify Customer ID',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $payment_meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the payment meta data required to process automatic recurring payments so that store managers can
|
|
|
|
* manually set up automatic recurring payments for a customer via the Edit Subscription screen in Subscriptions 2.0+.
|
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
* @param string $payment_method_id The ID of the payment method to validate
|
|
|
|
* @param array $payment_meta associative array of meta data required for automatic payments
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function validate_subscription_payment_meta( $payment_method_id, $payment_meta ) {
|
|
|
|
if ( $this->id === $payment_method_id ) {
|
|
|
|
if ( ! isset( $payment_meta['post_meta']['_simplify_customer_id']['value'] ) || empty( $payment_meta['post_meta']['_simplify_customer_id']['value'] ) ) {
|
|
|
|
throw new Exception( 'A "_simplify_customer_id" value is required.' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-22 00:46:00 +00:00
|
|
|
/**
|
|
|
|
* Don't transfer customer meta to resubscribe orders.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function delete_resubscribe_meta( $resubscribe_order ) {
|
|
|
|
delete_post_meta( $resubscribe_order->id, '_simplify_customer_id' );
|
|
|
|
}
|
|
|
|
|
2014-09-02 14:11:21 +00:00
|
|
|
/**
|
|
|
|
* Process a pre-order payment when the pre-order is released
|
|
|
|
*
|
|
|
|
* @param WC_Order $order
|
2014-09-07 23:37:55 +00:00
|
|
|
* @return wp_error|null
|
2014-09-02 14:11:21 +00:00
|
|
|
*/
|
|
|
|
public function process_pre_order_release_payment( $order ) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
$order_items = $order->get_items();
|
|
|
|
$order_item = array_shift( $order_items );
|
2015-02-18 03:56:18 +00:00
|
|
|
$pre_order_name = sprintf( __( '%s - Pre-order for "%s"', 'woocommerce' ), esc_html( get_bloginfo( 'name', 'display' ) ), $order_item['name'] ) . ' ' . sprintf( __( '(Order #%s)', 'woocommerce' ), $order->get_order_number() );
|
2014-09-02 14:11:21 +00:00
|
|
|
|
|
|
|
$customer_id = get_post_meta( $order->id, '_simplify_customer_id', true );
|
|
|
|
|
|
|
|
if ( ! $customer_id ) {
|
|
|
|
return new WP_Error( 'simplify_error', __( 'Customer not found', 'woocommerce' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Charge the customer
|
|
|
|
$payment = Simplify_Payment::createPayment( array(
|
|
|
|
'amount' => $order->order_total * 100, // In cents
|
|
|
|
'customer' => $customer_id,
|
|
|
|
'description' => trim( substr( $pre_order_name, 0, 1024 ) ),
|
|
|
|
'currency' => strtoupper( get_woocommerce_currency() ),
|
|
|
|
'reference' => $order->id,
|
|
|
|
'card.addressCity' => $order->billing_city,
|
|
|
|
'card.addressCountry' => $order->billing_country,
|
|
|
|
'card.addressLine1' => $order->billing_address_1,
|
|
|
|
'card.addressLine2' => $order->billing_address_2,
|
|
|
|
'card.addressState' => $order->billing_state,
|
|
|
|
'card.addressZip' => $order->billing_postcode
|
|
|
|
) );
|
|
|
|
|
|
|
|
if ( 'APPROVED' == $payment->paymentStatus ) {
|
|
|
|
// Payment complete
|
|
|
|
$order->payment_complete( $payment->id );
|
|
|
|
|
|
|
|
// Add order note
|
|
|
|
$order->add_order_note( sprintf( __( 'Simplify payment approved (ID: %s, Auth Code: %s)', 'woocommerce' ), $payment->id, $payment->authCode ) );
|
|
|
|
} else {
|
|
|
|
return new WP_Error( 'simplify_payment_declined', __( 'Payment was declined - the customer need to try another card.', 'woocommerce' ) );
|
|
|
|
}
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
$order_note = sprintf( __( 'Simplify Transaction Failed (%s)', 'woocommerce' ), $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
|
2014-10-18 08:04:40 +00:00
|
|
|
if ( 'failed' != $order->get_status() ) {
|
2014-09-02 14:11:21 +00:00
|
|
|
$order->update_status( 'failed', $order_note );
|
|
|
|
} else {
|
|
|
|
$order->add_order_note( $order_note );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-12 13:43:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return handler for Hosted Payments
|
|
|
|
*/
|
|
|
|
public function return_handler() {
|
|
|
|
if ( ! isset( $_REQUEST['cardToken'] ) ) {
|
|
|
|
parent::return_handler();
|
|
|
|
}
|
|
|
|
|
|
|
|
@ob_clean();
|
|
|
|
header( 'HTTP/1.1 200 OK' );
|
|
|
|
|
2015-02-15 19:13:22 +00:00
|
|
|
$redirect_url = wc_get_page_permalink( 'cart' );
|
2015-01-12 13:43:27 +00:00
|
|
|
|
|
|
|
if ( isset( $_REQUEST['reference'] ) && isset( $_REQUEST['amount'] ) ) {
|
|
|
|
$cart_token = $_REQUEST['cardToken'];
|
|
|
|
$amount = absint( $_REQUEST['amount'] );
|
|
|
|
$order_id = absint( $_REQUEST['reference'] );
|
|
|
|
$order = wc_get_order( $order_id );
|
|
|
|
$order_total = absint( $order->order_total * 100 );
|
|
|
|
|
|
|
|
if ( $amount === $order_total ) {
|
|
|
|
if ( $this->order_contains_subscription( $order->id ) ) {
|
|
|
|
$response = $this->process_subscription( $order, $cart_token );
|
|
|
|
} elseif ( $this->order_contains_pre_order( $order->id ) ) {
|
|
|
|
$response = $this->process_pre_order( $order, $cart_token );
|
|
|
|
} else {
|
|
|
|
$response = parent::process_standard_payments( $order, $cart_token );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'success' == $response['result'] ) {
|
|
|
|
$redirect_url = $response['redirect'];
|
|
|
|
} else {
|
|
|
|
$order->update_status( 'failed', __( 'Payment was declined by Simplify Commerce.', 'woocommerce' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
wp_redirect( $redirect_url );
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wp_redirect( $redirect_url );
|
|
|
|
exit();
|
|
|
|
}
|
2014-09-02 13:21:42 +00:00
|
|
|
}
|