/**
  * process_subscription_payment function.
  *
  * @param WC_order $order
  * @param integer $amount (default: 0)
  * @uses  Simplify_BadRequestException
  * @return bool|WP_Error
  */
 public function process_subscription_payment($order, $amount = 0)
 {
     if (0 == $amount) {
         // Payment complete
         $order->payment_complete();
         return true;
     }
     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'));
     }
     $order_items = $order->get_items();
     $order_item = array_shift($order_items);
     $subscription_name = sprintf(__('%s - Subscription for "%s"', 'woocommerce'), esc_html(get_bloginfo('name', 'display')), $order_item['name']) . ' ' . sprintf(__('(Order #%s)', 'woocommerce'), $order->get_order_number());
     $customer_id = get_post_meta($order->id, '_simplify_customer_id', true);
     if (!$customer_id) {
         return new WP_Error('simplify_error', __('Customer not found', 'woocommerce'));
     }
     try {
         // Charge the customer
         $payment = Simplify_Payment::createPayment(array('amount' => $amount * 100, 'customer' => $customer_id, 'description' => trim(substr($subscription_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));
     } 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()));
     }
     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'));
         return new WP_Error('simplify_payment_declined', __('Payment was declined - please try another card.', 'woocommerce'));
     }
 }
 /**
  * do payment function.
  *
  * @param WC_order $order
  * @param int $amount (default: 0)
  * @uses  Simplify_BadRequestException
  * @return bool|WP_Error
  */
 public function do_payment($order, $amount = 0, $token = array())
 {
     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'));
     }
     try {
         // Charge the customer
         $data = array('amount' => $amount * 100, 'description' => sprintf(__('%1$s - Order #%2$s', 'woocommerce'), esc_html(get_bloginfo('name', 'display')), $order->get_order_number()), 'currency' => strtoupper(get_woocommerce_currency()), 'reference' => $order->get_id());
         $data = array_merge($data, $token);
         $payment = Simplify_Payment::createPayment($data);
     } 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()));
     }
     if ('APPROVED' == $payment->paymentStatus) {
         // Payment complete
         $order->payment_complete($payment->id);
         // Add order note
         $order->add_order_note(sprintf(__('Simplify payment approved (ID: %1$s, Auth Code: %2$s)', 'woocommerce'), $payment->id, $payment->authCode));
         return true;
     } else {
         $order->add_order_note(__('Simplify payment declined', 'woocommerce'));
         return new WP_Error('simplify_payment_declined', __('Payment was declined - please try another card.', 'woocommerce'));
     }
 }
 /**
  * process_subscription_payment function.
  *
  * @param WC_order $order
  * @param integer $amount (default: 0)
  * @return bool|WP_Error
  */
 public function process_subscription_payment($order = '', $amount = 0)
 {
     $order_items = $order->get_items();
     $order_item = array_shift($order_items);
     $subscription_name = sprintf(__('%s - Subscription for "%s"', 'woocommerce'), esc_html(get_bloginfo('name')), $order_item['name']) . ' ' . sprintf(__('(Order %s)', 'woocommerce'), $order->get_order_number());
     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'));
     }
     // Charge the customer
     $payment = Simplify_Payment::createPayment(array('amount' => $amount * 100, 'customer' => $customer_id, 'description' => trim(substr($subscription_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));
         return true;
     } else {
         $order->add_order_note(__('Simplify payment declined', 'woocommerce'));
         return new WP_Error('simplify_payment_declined', __('Payment was declined - please try another card.', 'woocommerce'));
     }
 }
 /**
  * process_subscription_payment function.
  *
  * @param WC_order $order
  * @param int      $amount (default: 0)
  *
  * @return bool|WP_Error
  */
 public function process_subscription_payment($order = '', $amount = 0)
 {
     if ('yes' == $this->debug) {
         $this->log->add($this->id, 'Processing a subscription payment for order ' . $order->get_order_number());
     }
     $charge = $this->api->create_charge($order);
     if (isset($charge['errors']) && !empty($charge['errors'])) {
         $error = is_array($charge['errors']) ? current($charge['errors']) : $charge['errors'];
         return new WP_Error('iugu_subscription_error', $error);
     }
     $payment_data = array_map('sanitize_text_field', array('pdf' => $charge['pdf']));
     update_post_meta($order->id, '_iugu_wc_transaction_data', $payment_data);
     update_post_meta($order->id, __('Iugu Bank Slip URL', 'iugu-woocommerce'), $payment_data['pdf']);
     update_post_meta($order->id, '_transaction_id', sanitize_text_field($charge['invoice_id']));
     // Save only in old versions.
     if (defined('WC_VERSION') && version_compare(WC_VERSION, '2.1.12', '<=')) {
         update_post_meta($order->id, __('Iugu Transaction details', 'iugu-woocommerce'), 'https://iugu.com/a/invoices/' . sanitize_text_field($charge['invoice_id']));
     }
     $order_note = __('Iugu: The customer generated a bank slip, awaiting payment confirmation.', 'iugu-woocommerce');
     if ('pending' == $order->get_status()) {
         $order->update_status('on-hold', $order_note);
     } else {
         $order->add_order_note($order_note);
     }
     return true;
 }
 /**
  * process_subscription_payment function.
  *
  * @param WC_order $order
  * @param int      $amount (default: 0)
  *
  * @return bool|WP_Error
  */
 public function process_subscription_payment($order = '', $amount = 0)
 {
     if ('yes' == $this->debug) {
         $this->log->add($this->id, 'Processing a subscription payment for order ' . $order->get_order_number());
     }
     $payment_method_id = get_post_meta($order->id, '_iugu_customer_payment_method_id', true);
     if (!$payment_method_id) {
         if ('yes' == $this->debug) {
             $this->log->add($this->id, 'Missing customer payment method ID in subscription payment for order ' . $order->get_order_number());
         }
         return new WP_Error('iugu_subscription_error', __('Customer payment method not found!', 'iugu-woocommerce'));
     }
     $charge = $this->api->create_charge($order, array('customer_payment_method_id' => $payment_method_id));
     if (isset($charge['errors']) && !empty($charge['errors'])) {
         $error = is_array($charge['errors']) ? current($charge['errors']) : $charge['errors'];
         return new WP_Error('iugu_subscription_error', $error);
     }
     update_post_meta($order->id, '_transaction_id', sanitize_text_field($charge['invoice_id']));
     // Save only in old versions.
     if (defined('WC_VERSION') && version_compare(WC_VERSION, '2.1.12', '<=')) {
         update_post_meta($order->id, __('Iugu Transaction details', 'iugu-woocommerce'), 'https://iugu.com/a/invoices/' . sanitize_text_field($charge['invoice_id']));
     }
     if (true == $charge['success']) {
         $order->add_order_note(__('Iugu: Subscription paid successfully by credit card.', 'iugu-woocommerce'));
         $order->payment_complete();
         return true;
     } else {
         return new WP_Error('iugu_subscription_error', __('Iugu: Subscription payment failed. Credit card declined.', 'iugu-woocommerce'));
     }
 }
   function generate_begateway_form($order_id)
   {
       //creates a self-submitting form to pass the user through to the beGateway server
       global $woocommerce;
       $order = new WC_order($order_id);
       if ('yes' == $this->debug) {
           $this->log->add('begateway', 'Generating payment form for order ' . $order->get_order_number());
       }
       // Order number & Cart Contents for description field - may change
       $item_loop = 0;
       //grab the langauge
       $lang = explode('-', get_bloginfo('language'));
       $lang = $lang[0];
       if (in_array($lang, \beGateway\Language::getSupportedLanguages())) {
           $language = $lang;
       } else {
           $language = 'en';
       }
       $token = new \beGateway\GetPaymentToken();
       if ($this->transaction_type == 'authorization') {
           $token->setAuthorizationTransactionType();
       }
       $token->money->setCurrency(get_woocommerce_currency());
       $token->money->setAmount($order->order_total);
       $token->setDescription(__('Order', 'woocommerce') . ' # ' . $order->get_order_number());
       $token->setTrackingId(ltrim($order->get_order_number(), '#'));
       $token->customer->setFirstName($order->billing_first_name);
       $token->customer->setLastName($order->billing_last_name);
       $token->customer->setCountry($order->billing_country);
       $token->customer->setCity($order->billing_city);
       $token->customer->setPhone($order->billing_phone);
       $token->customer->setZip($order->billing_postcode);
       $token->customer->setAddress($order->billing_address_1 . $order->billing_address_2);
       $token->customer->setEmail($order->billing_email);
       if (in_array($order->billing_country, array('US', 'CA'))) {
           $token->customer->setState($order->billing_state);
       }
       $token->setSuccessUrl(esc_url_raw($this->get_return_url($order)));
       $token->setDeclineUrl(esc_url_raw($order->get_cancel_order_url_raw()));
       $token->setFailUrl(esc_url_raw($order->get_cancel_order_url_raw()));
       $token->setCancelUrl(esc_url_raw($order->get_cancel_order_url_raw()));
       $token->setNotificationUrl($this->notify_url);
       $token->setLanguage($language);
       $token->setAddressHidden();
       if ('yes' == $this->debug) {
           $this->log->add('begateway', 'Requesting token for order ' . $order->get_order_number());
       }
       $response = $token->submit();
       if (!$response->isSuccess()) {
           if ('yes' == $this->debug) {
               $this->log->add('begateway', 'Unable to get payment token on order: ' . $order_id . 'Reason: ' . $response->getMessage());
           }
           $woocommerce->add_error(__('Unable to contact the payment server at this time. Please try later.'));
           $woocommerce->add_error($response->getMessage());
           exit;
       }
       //now look to the result array for the token
       if ($response->getToken()) {
           $payment_url = $response->getRedirectUrlScriptName();
           update_post_meta(ltrim($order->get_order_number(), '#'), '_Token', $token);
           if ('yes' == $this->debug) {
               $this->log->add('begateway', 'Token received, forwarding customer to: ' . $payment_url);
           }
       } else {
           $woocommerce->add_error(__('Payment error: ') . $response->getMessage());
           if ('yes' == $this->debug) {
               $this->log->add('begateway', 'Payment error order ' . $order_id . '  ' . $error_to_show);
           }
           exit('Sorry - there was an error contacting the bank server, please try later');
       }
       wc_enqueue_js('
   jQuery("body").block({
     message: "' . __('Thank you for your order. We are now redirecting you to make the payment.', 'woocommerce-begateway') . '",
       overlayCSS: {
         background: "#fff",
         opacity: 0.6
       },
       css: {
         padding:        20,
         textAlign:      "center",
         color:          "#555",
         border:         "3px solid #aaa",
         backgroundColor:"#fff",
         cursor:         "wait",
         lineHeight:		"32px"
       }
   });
   jQuery("#submit_begateway_payment_form").click();
 ');
       return '<form action="' . $payment_url . '" method="post" id="begateway_payment_form">
   <input type="hidden" name="token" value="' . $response->getToken() . '">
   <input type="submit" class="button-alt" id="submit_begateway_payment_form" value="' . __('Make payment', 'woocommerce-begateway') . '" /> <a class="button cancel" href="' . $order->get_cancel_order_url() . '">' . __('Cancel order &amp; restore cart', 'woothemes') . '</a>
   </form>';
   }
 /**
  * process_subscription_payment function.
  *
  * @param  WC_order $order
  * @param  integer $amount (default: 0)
  * @return bool|WP_Error
  */
 public function process_subscription_payment($order = '', $amount = 0)
 {
     $order_items = $order->get_items();
     $order_item = array_shift($order_items);
     $subscription_name = sprintf(__('%s - Subscription for "%s"', 'woocommerce-payment-gateway-boilerplate'), esc_html(get_bloginfo('name')), $order_item['name']) . ' ' . sprintf(__('(Order %s)', 'woocommerce-payment-gateway-boilerplate'), $order->get_order_number());
     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-payment-gateway-boilerplate'));
     }
     if ('APPROVED' == $payment['status']) {
         // Payment complete
         $order->payment_complete($payment->id);
         // Add order note
         $order->add_order_note(sprintf(__('Gateway name payment approved (ID: %s)', 'woocommerce-payment-gateway-boilerplate'), $payment['id']));
         return true;
     } else {
         $order->add_order_note(__('Gateway name payment declined', 'woocommerce-payment-gateway-boilerplate'));
         return new WP_Error('gateway_name_payment_declined', __('Payment was declined - please try again.', 'woocommerce-payment-gateway-boilerplate'));
     }
 }