/** * Do AIM payment. */ public function executeAuthorizeNetAIM() { include_once AB_PATH . '/lib/payment/authorize.net/autoload.php'; $response = null; $userData = new AB_UserBookingData($this->getParameter('form_id')); if ($userData->load()) { define("AUTHORIZENET_API_LOGIN_ID", get_option('ab_authorizenet_api_login_id')); define("AUTHORIZENET_TRANSACTION_KEY", get_option('ab_authorizenet_transaction_key')); define("AUTHORIZENET_SANDBOX", (bool) get_option('ab_authorizenet_sandbox')); $price = $userData->getFinalServicePrice() * $userData->get('number_of_persons'); $sale = new AuthorizeNetAIM(); $sale->amount = $price; $sale->card_num = $this->getParameter('ab_card_number'); $sale->card_code = $this->getParameter('ab_card_code'); $sale->exp_date = $this->getParameter('ab_card_month') . '/' . $this->getParameter('ab_card_year'); $sale->first_name = $userData->get('name'); $sale->email = $userData->get('email'); $sale->phone = $userData->get('phone'); $response = $sale->authorizeAndCapture(); if ($response->approved) { /** @var AB_Appointment $appointment */ $appointment = $userData->save(); $customer_appointment = new AB_CustomerAppointment(); $customer_appointment->loadBy(array('appointment_id' => $appointment->get('id'), 'customer_id' => $userData->getCustomerId())); $payment = new AB_Payment(); $payment->set('total', $price); $payment->set('type', 'authorizeNet'); $payment->set('customer_appointment_id', $customer_appointment->get('id')); $payment->set('created', current_time('mysql')); $payment->save(); $response = array('state' => 'success'); } else { $response = array('status' => 'error', 'error' => $response->response_reason_text); } } else { $response = array('status' => 'error', 'error' => __('Session error.', 'bookly')); } wp_send_json($response); }
public function executeStripe() { $response = null; $userData = new AB_UserBookingData($this->getParameter('form_id')); if ($userData->load()) { if ($userData->get('service_id')) { Stripe::setApiKey(get_option('ab_stripe_secret_key')); Stripe::setApiVersion("2014-10-07"); $price = $userData->getFinalServicePrice() * $userData->get('number_of_persons'); $stripe_data = array('number' => $this->getParameter('ab_card_number'), 'exp_month' => $this->getParameter('ab_card_month'), 'exp_year' => $this->getParameter('ab_card_year'), 'cvc' => $this->getParameter('ab_card_code')); try { $charge = Stripe_Charge::create(array('card' => $stripe_data, 'amount' => intval($price * 100), 'currency' => get_option('ab_paypal_currency'), 'description' => "Charge for " . $userData->get('email'))); if ($charge->paid) { $appointment = $userData->save(); $customer_appointment = new AB_CustomerAppointment(); $customer_appointment->loadBy(array('appointment_id' => $appointment->get('id'), 'customer_id' => $userData->getCustomerId())); $payment = new AB_Payment(); $payment->set('total', $price); $payment->set('type', 'stripe'); $payment->set('customer_appointment_id', $customer_appointment->get('id')); $payment->set('created', current_time('mysql')); $payment->save(); $response = array('status' => 'success'); } else { $response = array('status' => 'error', 'error' => 'unknown error'); } } catch (Exception $e) { $response = array('status' => 'error', 'error' => $e->getMessage()); } } } else { $response = array('status' => 'error', 'error' => __('Session error.', 'bookly')); } // Output JSON response. wp_send_json($response); }
/** * Authentification WP_User in booking form */ public function executeWpUserLogin() { /** @var WP_User $user */ $user = wp_signon(); if (is_wp_error($user)) { wp_send_json_error(array('message' => __('Incorrect username or password.'))); } else { $customer = new AB_Customer(); if ($customer->loadBy(array('wp_user_id' => $user->ID))) { $user_info = array('name' => $customer->get('name'), 'email' => $customer->get('email'), 'phone' => $customer->get('phone')); } else { $user_info = array('name' => $user->display_name, 'email' => $user->user_email); } $userData = new AB_UserBookingData($this->getParameter('form_id')); $userData->load(); $userData->saveData($user_info); wp_send_json_success($user_info); } }
/** * Process the Express Checkout RETURNURL */ public function paypalResponseSuccess() { $form_id = $_GET['ab_fid']; $paypal = new AB_PayPal(); if (isset($_GET["token"]) && isset($_GET["PayerID"])) { $token = $_GET["token"]; $payer_id = $_GET["PayerID"]; // send the request to PayPal $response = $paypal->sendNvpRequest('GetExpressCheckoutDetails', sprintf('&TOKEN=%s', $token)); if (strtoupper($response["ACK"]) == "SUCCESS") { $data = sprintf('&TOKEN=%s&PAYERID=%s&PAYMENTREQUEST_0_PAYMENTACTION=Sale', $token, $payer_id); // response keys containing useful data to send via DoExpressCheckoutPayment operation $response_data_keys_pattern = sprintf('/^(%s)/', implode('|', array('PAYMENTREQUEST_0_AMT', 'PAYMENTREQUEST_0_ITEMAMT', 'PAYMENTREQUEST_0_CURRENCYCODE', 'L_PAYMENTREQUEST_0'))); foreach ($response as $key => $value) { // collect product data from response using defined response keys if (preg_match($response_data_keys_pattern, $key)) { $data .= sprintf('&%s=%s', $key, $value); } } //We need to execute the "DoExpressCheckoutPayment" at this point to Receive payment from user. $response = $paypal->sendNvpRequest('DoExpressCheckoutPayment', $data); if ("SUCCESS" == strtoupper($response["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($response["ACK"])) { // get transaction info $response = $paypal->sendNvpRequest('GetTransactionDetails', "&TRANSACTIONID=" . urlencode($response["PAYMENTINFO_0_TRANSACTIONID"])); if ("SUCCESS" == strtoupper($response["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($response["ACK"])) { // need session to get Total and Token $token = $_SESSION['bookly'][$form_id]['paypal_response'][0]['TOKEN']; $userData = new AB_UserBookingData($form_id); $userData->load(); if ($userData->get('service_id')) { $appointment = $userData->save(); $customer_appointment = new AB_CustomerAppointment(); $customer_appointment->loadBy(array('appointment_id' => $appointment->get('id'), 'customer_id' => $userData->getCustomerId())); $payment = new AB_Payment(); $payment->set('token', urldecode($token)); $payment->set('total', $userData->getFinalServicePrice() * $userData->get('number_of_persons')); $payment->set('customer_appointment_id', $customer_appointment->get('id')); $payment->set('transaction', urlencode($response["TRANSACTIONID"])); $payment->set('created', current_time('mysql')); $payment->save(); $userData->setPayPalStatus('success'); } @wp_redirect(remove_query_arg(array('action', 'token', 'PayerID', 'ab_fid'), AB_Utils::getCurrentPageURL())); exit(0); } else { header('Location: ' . wp_sanitize_redirect(add_query_arg(array('action' => 'ab-paypal-errorurl', 'ab_fid' => $form_id, 'error_msg' => $response["L_LONGMESSAGE0"]), AB_Utils::getCurrentPageURL()))); exit; } } else { header('Location: ' . wp_sanitize_redirect(add_query_arg(array('action' => 'ab-paypal-errorurl', 'ab_fid' => $form_id, 'error_msg' => $response["L_LONGMESSAGE0"]), AB_Utils::getCurrentPageURL()))); exit; } } else { header('Location: ' . wp_sanitize_redirect(add_query_arg(array('action' => 'ab-paypal-errorurl', 'ab_fid' => $form_id, 'error_msg' => 'Invalid token provided'), AB_Utils::getCurrentPageURL()))); exit; } } else { throw new Exception('Token parameter not found!'); } }
/** * Add product to cart * * @return string JSON */ public function executeAddToWoocommerceCart() { if (!get_option('ab_woocommerce')) { exit(0); } $response = null; $userData = new AB_UserBookingData($this->getParameter('form_id')); if ($userData->load()) { $session = WC()->session; /** @var WC_Session_Handler $session */ if ($session instanceof WC_Session_Handler and $session->get_session_cookie() === false) { $session->set_customer_session_cookie(true); } WC()->cart->add_to_cart($this->product_id, $userData->get('number_of_persons'), '', array(), array('bookly' => $userData->getData())); $response = array('status' => 'success'); } else { $response = array('status' => 'error', 'error' => __('Session error.', 'bookly')); } // Output JSON response. wp_send_json($response); }
/** * Apply coupon */ public function executeApplyCoupon() { $form_id = $this->getParameter('form_id'); $ab_coupon = $this->getParameter('ab_coupon'); $response = null; if (get_option('ab_settings_coupons') and $form_id) { $userData = new AB_UserBookingData($form_id); $userData->load(); if ($userData->hasData()) { $price = $this->getWpdb()->get_var($this->getWpdb()->prepare('SELECT price FROM ab_staff_service WHERE staff_id = %d AND service_id = %d', $userData->getStaffId(), $userData->getServiceId())); if ($ab_coupon === '') { $userData->setCoupon(NULL); $response = array('status' => 'reset', 'text' => $this->_prepareInfoText(4, $userData, $price)); } else { $discount = $this->getWpdb()->get_var($this->getWpdb()->prepare('SELECT `discount` FROM `ab_coupons` WHERE UPPER(`code`) = %s AND `used` = 0', strtoupper($ab_coupon))); if ($discount) { $userData->setCoupon($ab_coupon); $price -= $price * $discount / 100; $response = array('status' => 'success', 'text' => $this->_prepareInfoText(4, $userData, $price)); } else { $userData->setCoupon(NULL); $response = array('status' => 'error', 'error' => __('* This coupon code is invalid or has been used', 'ab'), 'text' => $this->_prepareInfoText(4, $userData, $price)); } } } } // Output JSON response. if ($response === null) { $response = array('status' => 'no-data'); } header('Content-Type: application/json'); echo json_encode($response); exit(0); }