/** * Tell the WePay API if we're using staging or production values */ public function __construct() { // if not production && useStaging then use stage // else if not production && not useStaging use production // else use production if (!\App::environment('production')) { if (\Config::get('wepay-laravel::useStaging')) { \WePay::useStaging(\Config::get('wepay-laravel::staging.client_id'), \Config::get('wepay-laravel::staging.client_secret')); } else { \WePay::useProduction(\Config::get('wepay-laravel::production.client_id'), \Config::get('wepay-laravel::production.client_secret')); } } else { \WePay::useProduction(\Config::get('wepay-laravel::production.client_id'), \Config::get('wepay-laravel::production.client_secret')); } }
/** * Use this to do the final payment. Create the order then process the payment. If * you know the payment is successful right away go ahead and change the order status * as well. * Call $mp->cart_checkout_error($msg, $context); to handle errors. If no errors * it will redirect to the next step. * * @param array $cart. Contains the cart contents for the current blog, global cart if $mp->global_cart is true * @param array $shipping_info. Contains shipping info and email in case you need it */ function process_payment($cart, $shipping_info) { global $mp; $settings = get_option('mp_settings'); //make sure token is set at this point if (!isset($_SESSION['payment_method_id'])) { $mp->cart_checkout_error(__('The WePay Card Token was not generated correctly. Please go back and try again.', 'mp')); return false; } $order_id = $mp->generate_order_id(); //Get the WePay SDK require $mp->plugin_dir . 'plugins-gateway/wepay-files/wepay-sdk.php'; $totals = array(); $coupon_code = $mp->get_coupon_code(); foreach ($cart as $product_id => $variations) { foreach ($variations as $variation => $data) { $price = $mp->coupon_value_product($coupon_code, $data['price'] * $data['quantity'], $product_id); $totals[] = $price; } } $total = array_sum($totals); //shipping line if ($shipping_price = $mp->shipping_price()) { $total += $shipping_price; } //tax line if ($tax_price = $mp->tax_price()) { $total += $tax_price; } try { // Application settings $account_id = $this->account_id; $client_id = $this->client_id; $client_secret = $this->client_secret; $access_token = $this->access_token; // Credit card id to charge $credit_card_id = $_SESSION['payment_method_id']; if ($this->mode == 'staging') { WePay::useStaging($this->client_id, $this->client_secret); } else { WePay::useProduction($this->client_id, $this->client_secret); } $wepay = new WePay($access_token); // charge the credit card $response = $wepay->request('checkout/create', array('account_id' => $account_id, 'amount' => number_format($total, 2, '.', ''), 'currency' => 'USD', 'short_description' => $order_id, 'type' => $this->checkout_type, 'payment_method_id' => $credit_card_id, 'payment_method_type' => 'credit_card')); if (isset($response->state) && $response->state == 'authorized') { $credit_card_response = $wepay->request('/credit_card', array('client_id' => $this->client_id, 'client_secret' => $this->client_secret, 'credit_card_id' => $_SESSION['payment_method_id'])); //setup our payment details $payment_info = array(); $payment_info['gateway_public_name'] = $this->public_name; $payment_info['gateway_private_name'] = $this->admin_name; $payment_info['method'] = sprintf(__('%1$s', 'mp'), $credit_card_response->credit_card_name); $payment_info['transaction_id'] = $order_id; $timestamp = time(); $payment_info['status'][$timestamp] = __('Paid', 'mp'); $payment_info['total'] = $total; $payment_info['currency'] = $this->currency; $order = $mp->create_order($order_id, $cart, $_SESSION['mp_shipping_info'], $payment_info, true); unset($_SESSION['payment_method_id']); $mp->set_cart_cookie(array()); } } catch (Exception $e) { unset($_SESSION['payment_method_id']); $mp->cart_checkout_error(sprintf(__('There was an error processing your card: "%s". Please <a href="%s">go back and try again</a>.', 'mp'), $e->getMessage(), mp_checkout_step_url('checkout'))); return false; } }