/** * @param \Enlight_Controller_ActionEventArgs $args */ public function onPostDispatchCheckout($args) { $action = $args->getSubject(); $request = $action->Request(); $view = $action->View(); $apiKey = $this->bootstrap->Config()->get('stripeSecretKey'); \Stripe\Stripe::setApiKey($apiKey); $token = $request->getPost('stripeToken'); if (!empty($token)) { try { $this->onStripeToken($request); } catch (\Stripe\Error\Card $e) { $eJson = $e->getJsonBody(); $error = $eJson['error']; $view->assign('sErrorMessages', [$error['message']]); if ($request->getControllerName() == 'checkout') { $action->forward('shippingPayment'); } else { $action->forward('payment'); } $request->setPost('stripeToken', null); $action->Response()->clearHeader('Location')->setHttpResponseCode(200); return; } } if (!empty($view->sPayments) && !empty($view->sUserData['additional']['user']['viisonStripeCustomerId'])) { $customerId = $view->sUserData['additional']['user']['viisonStripeCustomerId']; $customer = \Stripe\Customer::retrieve($customerId); $view->stripeSources = $this->convertCards($customer['sources']['data']); } }
public function indexAction() { if ($this->getPaymentShortName() != 'stripe') { $this->redirect(array('controller' => 'checkout')); return; } $apiKey = $this->plugin->Config()->get('stripeSecretKey'); \Stripe\Stripe::setApiKey($apiKey); $currency = $this->getCurrencyShortName(); $amount = $this->getAmount() * 100; $user = $this->getUser(); $email = $user['additional']['user']['email']; $customerNumber = $user['billingaddress']['customernumber']; $stripeCustomerId = $user['additional']['user']['viisonStripeCustomerId']; $chargeData = array("amount" => $amount, "currency" => $currency, "customer" => $stripeCustomerId, "description" => $email . ' / Kunden-Nr.: ' . $customerNumber, "metadata" => array("platform_name" => "UMXJ4nBknsWR3LN_shopware_v50")); if (!empty($this->session->stripeToken)) { unset($this->session->stripeToken); $chargeData['card'] = $this->session->stripeToken; } elseif (!empty($stripeCustomerId)) { $chargeData['customer'] = $stripeCustomerId; } else { $this->redirect(array('controller' => 'checkout', 'action' => 'shippingPayment')); return; } try { $charge = \Stripe\Charge::create($chargeData); } catch (\Stripe\Error\Card $e) { // The card has been declined return; } $uniqueId = sha1($charge->balance_transaction); $orderNumber = $this->saveOrder($charge->id, $uniqueId, 12); try { $charge->description .= ' / Bestell-Nr.: ' . $orderNumber; $charge->invoice = $orderNumber; $charge->save(); } catch (\Stripe\Error\Base $e) { } $this->redirect(array('controller' => 'checkout', 'action' => 'finish', 'sUniqueID' => $uniqueId)); }
/** * Gets the order id, total amount, refunded positions and an optional comment * from the request and uses them to create a new refund with Stripe. * If successful, the information abouth the refund are added to the internal coment * of ther order. Finally the new internal comment is added to the response. */ public function refundAction() { // Get order id, total amount, positions and comment from the request $orderId = $this->Request()->getParam('orderId'); if ($orderId === null) { // Missing orderId $this->View()->success = false; $this->View()->message = 'Required parameter "orderId" not found'; $this->Response()->setHttpResponseCode(400); return; } $amount = floatval($this->Request()->getParam('amount')); if ($amount <= 0.0) { // Invalid amount $this->View()->success = false; $this->View()->message = 'Required parameter "amount" must be greater zero'; $this->Response()->setHttpResponseCode(400); return; } $positions = $this->Request()->getParam('positions', array()); if (count($positions) === 0) { // Missing positions $this->View()->success = false; $this->View()->message = 'Required parameter "positions" not found or empty'; $this->Response()->setHttpResponseCode(400); return; } $comment = $this->Request()->getParam('comment'); // Try to get order /** @var Shopware\Models\Order\Order $order */ $order = $this->get('models')->getRepository('Shopware\\Models\\Order\\Order')->findOneById($orderId); if ($order === null) { // Order does not exist $this->View()->success = false; $this->View()->message = 'Order with id ' . $orderId . ' not found'; $this->Response()->setHttpResponseCode(404); return; } if ($order->getTransactionId() === null) { // Order wasn't payed with Stripe $this->View()->success = false; $this->View()->message = 'Order with id ' . $orderId . ' has no Stripe charge'; $this->Response()->setHttpResponseCode(404); return; } // Set the Stripe API key $apiKey = $this->plugin->Config()->get('stripeSecretKey'); \Stripe\Stripe::setApiKey($apiKey); // Load the charge and add new refund to it try { $charge = \Stripe\Charge::retrieve($order->getTransactionId()); $charge->refund(array('amount' => intval($amount * 100))); } catch (Exception $e) { // Try to get the error response if ($e->getJsonBody() !== null) { $body = $e->getJsonBody(); $message = $body['error']['message']; } else { $message = $e->getMessage(); } $this->View()->success = false; $this->View()->message = $message; $this->Response()->setHttpResponseCode(500); return; } // Add a new refund comment to the internal comment of the order $internalComment = $order->getInternalComment(); $internalComment .= "\n--------------------------------------------------------------\n" . 'Stripe Rückerstattung (' . date('d.m.Y, G:i:s') . ")\n" . 'Betrag: ' . number_format($amount, 2, ',', '.') . " €\n" . "Kommentar: {$comment}\n" . "Positionen:\n"; foreach ($positions as $position) { $price = number_format($position['price'], 2, ',', '.'); $totalPrice = number_format($position['total'], 2, ',', '.'); $internalComment .= ' - ' . $position['quantity'] . ' x ' . $position['articleNumber'] . ', je ' . $price . ' €, Gesamt: ' . $totalPrice . " €\n"; } $internalComment .= "--------------------------------------------------------------\n"; $order->setInternalComment($internalComment); $this->get('models')->flush($order); // Respond with the new internal comment $this->View()->success = true; $this->View()->internalComment = $internalComment; }