enabled ) {
return;
}
// PHP Version
if ( version_compare( phpversion(), '5.3', '<' ) ) {
echo '
' . sprintf( __( 'Simplify Commerce Error: Simplify commerce requires PHP 5.3 and above. You are using version %s.', 'woocommerce' ), phpversion() ) . '
' . __( 'Simplify Commerce Error: Please enter your public and private keys', 'woocommerce' ) . '
';
}
// Show message when using standard mode and no SSL on the checkout page
elseif ( 'standard' == $this->mode && ! wc_checkout_is_https() ) {
echo '
' . sprintf( __( 'Simplify Commerce is enabled, but the force SSL option is disabled; your checkout may not be secure! Please enable SSL and ensure your server has a valid SSL certificate - Simplify Commerce will only work in sandbox mode.', 'woocommerce'), admin_url( 'admin.php?page=wc-settings&tab=checkout' ) ) . '
';
}
}
/**
* Check if this gateway is enabled.
*
* @return bool
*/
public function is_available() {
if ( 'yes' !== $this->enabled ) {
return false;
}
if ( 'standard' === $this->mode && 'yes' !== $this->sandbox && ! wc_checkout_is_https() ) {
return false;
}
if ( ! $this->public_key || ! $this->private_key ) {
return false;
}
return true;
}
/**
* Initialise Gateway Settings Form Fields.
*/
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'label' => __( 'Enable Simplify Commerce', 'woocommerce' ),
'type' => 'checkbox',
'description' => '',
'default' => 'no'
),
'title' => array(
'title' => __( 'Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'Credit card', 'woocommerce' ),
'desc_tip' => true
),
'description' => array(
'title' => __( 'Description', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the description which the user sees during checkout.', 'woocommerce' ),
'default' => 'Pay with your credit card via Simplify Commerce by MasterCard.',
'desc_tip' => true
),
'mode' => array(
'title' => __( 'Payment Mode', 'woocommerce' ),
'label' => __( 'Enable Hosted Payments', 'woocommerce' ),
'type' => 'select',
'description' => sprintf( __( 'Standard will display the credit card fields on your store (SSL required). %1$s Hosted Payments will display a Simplify Commerce modal dialog on your store (if SSL) or will redirect the customer to Simplify Commerce hosted page (if not SSL). %1$s Note: Hosted Payments need a new API Key pair with the hosted payments flag selected. %2$sFor more details check the Simplify Commerce docs%3$s.', 'woocommerce' ), ' ', '', '' ),
'default' => 'standard',
'options' => array(
'standard' => __( 'Standard', 'woocommerce' ),
'hosted' => __( 'Hosted Payments', 'woocommerce' )
)
),
'modal_color' => array(
'title' => __( 'Modal Color', 'woocommerce' ),
'type' => 'color',
'description' => __( 'Set the color of the buttons and titles on the modal dialog.', 'woocommerce' ),
'default' => '#a46497',
'desc_tip' => true
),
'sandbox' => array(
'title' => __( 'Sandbox', 'woocommerce' ),
'label' => __( 'Enable Sandbox Mode', 'woocommerce' ),
'type' => 'checkbox',
'description' => __( 'Place the payment gateway in sandbox mode using sandbox API keys (real payments will not be taken).', 'woocommerce' ),
'default' => 'yes'
),
'sandbox_public_key' => array(
'title' => __( 'Sandbox Public Key', 'woocommerce' ),
'type' => 'text',
'description' => __( 'Get your API keys from your Simplify account: Settings > API Keys.', 'woocommerce' ),
'default' => '',
'desc_tip' => true
),
'sandbox_private_key' => array(
'title' => __( 'Sandbox Private Key', 'woocommerce' ),
'type' => 'text',
'description' => __( 'Get your API keys from your Simplify account: Settings > API Keys.', 'woocommerce' ),
'default' => '',
'desc_tip' => true
),
'public_key' => array(
'title' => __( 'Public Key', 'woocommerce' ),
'type' => 'text',
'description' => __( 'Get your API keys from your Simplify account: Settings > API Keys.', 'woocommerce' ),
'default' => '',
'desc_tip' => true
),
'private_key' => array(
'title' => __( 'Private Key', 'woocommerce' ),
'type' => 'text',
'description' => __( 'Get your API keys from your Simplify account: Settings > API Keys.', 'woocommerce' ),
'default' => '',
'desc_tip' => true
),
);
}
/**
* Payment form on checkout page.
*/
public function payment_fields() {
$description = $this->get_description();
if ( 'yes' == $this->sandbox ) {
$description .= ' ' . sprintf( __( 'TEST MODE ENABLED. Use a test card: %s', 'woocommerce' ), 'https://www.simplify.com/commerce/docs/tutorial/index#testing' );
}
if ( $description ) {
echo wpautop( wptexturize( trim( $description ) ) );
}
if ( 'standard' == $this->mode ) {
new WC_Payment_Gateway_Form( $this );
}
}
/**
* Outputs scripts used for simplify payment.
*/
public function payment_scripts() {
if ( ! is_checkout() || ! $this->is_available() ) {
return;
}
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_script( 'simplify-commerce', 'https://www.simplify.com/commerce/v1/simplify.js', array( 'jquery' ), WC_VERSION, true );
wp_enqueue_script( 'wc-simplify-commerce', WC()->plugin_url() . '/includes/gateways/simplify-commerce/assets/js/simplify-commerce' . $suffix . '.js', array( 'simplify-commerce', 'wc-credit-card-form' ), WC_VERSION, true );
wp_localize_script( 'wc-simplify-commerce', 'Simplify_commerce_params', array(
'key' => $this->public_key,
'card.number' => __( 'Card Number', 'woocommerce' ),
'card.expMonth' => __( 'Expiry Month', 'woocommerce' ),
'card.expYear' => __( 'Expiry Year', 'woocommerce' ),
'is_invalid' => __( 'is invalid', 'woocommerce' ),
'mode' => $this->mode,
'is_ssl' => is_ssl()
) );
}
/**
* Process standard payments.
*
* @param WC_Order $order
* @param string $cart_token
* @uses Simplify_ApiException
* @uses Simplify_BadRequestException
* @return array
*/
protected function process_standard_payments( $order, $cart_token = '' ) {
try {
if ( empty( $cart_token ) ) {
$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 );
}
$payment = Simplify_Payment::createPayment( array(
'amount' => $order->order_total * 100, // In cents.
'token' => $cart_token,
'description' => sprintf( __( '%s - Order #%s', 'woocommerce' ), esc_html( get_bloginfo( 'name', 'display' ) ), $order->get_order_number() ),
'currency' => strtoupper( get_woocommerce_currency() ),
'reference' => $order->id
) );
$order_complete = $this->process_order_status( $order, $payment->id, $payment->paymentStatus, $payment->authCode );
if ( $order_complete ) {
// Return thank you page redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
} else {
$order->add_order_note( __( 'Simplify payment declined', 'woocommerce' ) );
throw new Simplify_ApiException( __( 'Payment was declined - please try another card.', 'woocommerce' ) );
}
} 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' => ''
);
}
}
/**
* Process standard payments.
*
* @param WC_Order $order
* @return array
*/
protected function process_hosted_payments( $order ) {
return array(
'result' => 'success',
'redirect' => $order->get_checkout_payment_url( true )
);
}
/**
* Process the payment.
*
* @param int $order_id
*/
public function process_payment( $order_id ) {
$cart_token = isset( $_POST['simplify_token'] ) ? wc_clean( $_POST['simplify_token'] ) : '';
$order = wc_get_order( $order_id );
if ( 'hosted' == $this->mode ) {
return $this->process_hosted_payments( $order );
} else {
return $this->process_standard_payments( $order, $cart_token );
}
}
/**
* Hosted payment args.
*
* @param WC_Order $order
*
* @return array
*/
protected function get_hosted_payments_args( $order ) {
$args = apply_filters( 'woocommerce_simplify_commerce_hosted_args', array(
'sc-key' => $this->public_key,
'amount' => $order->order_total * 100,
'reference' => $order->id,
'name' => esc_html( get_bloginfo( 'name', 'display' ) ),
'description' => sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ),
'receipt' => 'false',
'color' => $this->modal_color,
'redirect-url' => WC()->api_request_url( 'WC_Gateway_Simplify_Commerce' ),
'address' => $order->billing_address_1 . ' ' . $order->billing_address_2,
'address-city' => $order->billing_city,
'address-state' => $order->billing_state,
'address-zip' => $order->billing_postcode,
'address-country' => $order->billing_country
), $order->id );
return $args;
}
/**
* Receipt page.
*
* @param int $order_id
*/
public function receipt_page( $order_id ) {
$order = wc_get_order( $order_id );
echo '
' . __( 'Thank you for your order, please click the button below to pay with credit card using Simplify Commerce by MasterCard.', 'woocommerce' ) . '