/** * 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')); } }
private function loadWePayAPILibraries() { $root = dirname(phutil_get_library_root('phabricator')); require_once $root . '/externals/wepay/wepay.php'; WePay::useStaging($this->getWePayClientID(), $this->getWePayClientSecret()); return new WePay($this->getWePayAccessToken()); }
/** * 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; } }
/** * @phutil-external-symbol class WePay */ public function processControllerRequest(PhortuneProviderController $controller, AphrontRequest $request) { $viewer = $request->getUser(); $cart = $controller->loadCart($request->getInt('cartID')); if (!$cart) { return new Aphront404Response(); } $cart_uri = '/phortune/cart/' . $cart->getID() . '/'; $root = dirname(phutil_get_library_root('phabricator')); require_once $root . '/externals/wepay/wepay.php'; WePay::useStaging($this->getWePayClientID(), $this->getWePayClientSecret()); $wepay = new WePay($this->getWePayAccessToken()); switch ($controller->getAction()) { case 'checkout': $return_uri = $this->getControllerURI('charge', array('cartID' => $cart->getID())); $cancel_uri = $this->getControllerURI('cancel', array('cartID' => $cart->getID())); $total_in_cents = $cart->getTotalPriceInCents(); $price = PhortuneCurrency::newFromUSDCents($total_in_cents); $params = array('account_id' => $this->getWePayAccountID(), 'short_description' => 'Services', 'type' => 'SERVICE', 'amount' => $price->formatBareValue(), 'long_description' => 'Services', 'reference_id' => $cart->getPHID(), 'app_fee' => 0, 'fee_payer' => 'Payee', 'redirect_uri' => $return_uri, 'fallback_uri' => $cancel_uri, 'auto_capture' => true, 'require_shipping' => 0, 'shipping_fee' => 0, 'charge_tax' => 0, 'mode' => 'regular', 'funding_sources' => 'bank,cc'); $result = $wepay->request('checkout/create', $params); // TODO: We must store "$result->checkout_id" on the Cart since the // user might not end up back here. Really this needs a bunch of junk. $uri = new PhutilURI($result->checkout_uri); return id(new AphrontRedirectResponse())->setIsExternal(true)->setURI($uri); case 'charge': $checkout_id = $request->getInt('checkout_id'); $params = array('checkout_id' => $checkout_id); $checkout = $wepay->request('checkout', $params); if ($checkout->reference_id != $cart->getPHID()) { throw new Exception(pht('Checkout reference ID does not match cart PHID!')); } switch ($checkout->state) { case 'authorized': case 'reserved': case 'captured': break; default: throw new Exception(pht('Checkout is in bad state "%s"!', $result->state)); } $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); $charge = id(new PhortuneCharge())->setAmountInCents((int) $checkout->gross * 100)->setAccountPHID($cart->getAccount()->getPHID())->setAuthorPHID($viewer->getPHID())->setPaymentProviderKey($this->getProviderKey())->setCartPHID($cart->getPHID())->setStatus(PhortuneCharge::STATUS_CHARGING)->save(); $cart->openTransaction(); $charge->setStatus(PhortuneCharge::STATUS_CHARGED); $charge->save(); $cart->setStatus(PhortuneCart::STATUS_PURCHASED); $cart->save(); $cart->saveTransaction(); unset($unguarded); return id(new AphrontRedirectResponse())->setIsExternal(true)->setURI($cart_uri); case 'cancel': var_dump($_REQUEST); break; } throw new Exception("The rest of this isn't implemented yet."); }