public function configure() { if (null !== ($response = $this->checkAuth(array(AdminResources::MODULE), array('Paypal'), AccessManager::UPDATE))) { return $response; } $conf = new PaypalConfig(); $one_is_done = 0; $tab = ""; // Case form is paypal.configure $form = new \Paypal\Form\ConfigurePaypal($this->getRequest()); try { $vform = $this->validateForm($form); $conf->setLogin($vform->get('login')->getData())->setPassword($vform->get('password')->getData())->setSignature($vform->get('signature')->getData())->write(); $one_is_done = 1; $tab = "configure_account"; } catch (\Exception $e) { } // Case form is paypal.configure.sandbox $form = new \Paypal\Form\ConfigureSandboxPaypal($this->getRequest()); try { $vform = $this->validateForm($form); $tab = "configure_sandbox"; $conf->setLoginSandbox($vform->get('login')->getData())->setPasswordSandbox($vform->get('password')->getData())->setSignatureSandbox($vform->get('signature')->getData())->setSandbox($vform->get('sandbox')->getData() ? "true" : ""); $conf->write(); } catch (\Exception $e) { } //Redirect to module configuration page $this->redirectToRoute("admin.module.configure", array(), array('module_code' => Paypal::getModCode(true), 'current_tab' => $tab, '_controller' => 'Thelia\\Controller\\Admin\\ModuleController::configureAction')); }
/** * @param $order_id * @return \Thelia\Core\HttpFoundation\Response */ public function ok($order_id) { /* * Check if token&order are valid */ $token = null; $order = $this->checkorder($order_id, $token); /* * $payerid string value returned by paypal * $logger PaypalApiLogManager used to log transctions with paypal */ $payerid = $this->getRequest()->get('PayerID'); $logger = new PaypalApiLogManager(); if (!empty($payerid)) { /* * $config ConfigInterface Object that contains configuration * $api PaypalApiCredentials Class used by the library to store and use 3T login(username, password, signature) * $sandbox bool true if sandbox is enabled */ $config = new PaypalConfig(); $config->pushValues(); $api = new PaypalApiCredentials($config); $sandbox = $api->getConfig()->getSandbox(); /* * Send getExpressCheckout & doExpressCheckout * empty cart */ $getExpressCheckout = new PaypalNvpOperationsGetExpressCheckoutDetails($api, $token); $request = new PaypalNvpMessageSender($getExpressCheckout, $sandbox); $response = $request->send(); $logger->logTransaction($response); $response = PaypalApiManager::nvpToArray($response); if (isset($response['ACK']) && $response['ACK'] === 'Success' && isset($response['PAYERID']) && $response['PAYERID'] === $payerid && isset($response['TOKEN']) && $response['TOKEN'] === $token) { $doExpressCheckout = new PaypalNvpOperationsDoExpressCheckoutPayment($api, round($order->getTotalAmount(), 2), $order->getCurrency()->getCode(), $payerid, PaypalApiManager::PAYMENT_TYPE_SALE, $token, URL::getInstance()->absoluteUrl("/module/paypal/listen")); $request = new PaypalNvpMessageSender($doExpressCheckout, $token); $response = $request->send(); $logger->logTransaction($response); $response = PaypalApiManager::nvpToArray($response); /* * In case of success, go to success page * In case of error, show it */ if (isset($response['ACK']) && $response['ACK'] === "Success" && isset($response['PAYMENTINFO_0_PAYMENTSTATUS']) && $response['PAYMENTINFO_0_PAYMENTSTATUS'] === "Completed" && isset($response['TOKEN']) && $response['TOKEN'] === $token) { /* * Set order status as paid */ $event = new OrderEvent($order); $event->setStatus(OrderStatusQuery::getPaidStatus()->getId()); $this->dispatch(TheliaEvents::ORDER_UPDATE_STATUS, $event); $this->redirectToSuccessPage($order_id); } } } /* * If no redirection done ( === error ): Empty cart */ return $this->render("order-failed", ["failed_order_id" => $order_id]); }
/** * Send request via Curl * * @return string APÏ response */ public function send() { $config = new PaypalConfig(); $config->pushValues(); $paypalApiManager = new PaypalApiManager($config); $url = $paypalApiManager->getApiUrl() . '?' . $this->message; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); return $response; }
/** * * in this function you add all the fields you need for your Form. * Form this you have to call add method on $this->formBuilder attribute : * * $this->formBuilder->add("name", "text") * ->add("email", "email", array( * "attr" => array( * "class" => "field" * ), * "label" => "email", * "constraints" => array( * new \Symfony\Component\Validator\Constraints\NotBlank() * ) * ) * ) * ->add('age', 'integer'); * * @return null */ protected function buildForm() { $config_data = PaypalConfig::read(); $this->formBuilder->add("login", "text", array('constraints' => array(new NotBlank()), 'label' => Translator::getInstance()->trans("login"), 'label_attr' => array('for' => 'login'), 'data' => isset($config_data['login_sandbox']) ? $config_data['login_sandbox'] : ""))->add("password", "text", array('constraints' => array(new NotBlank()), 'label' => Translator::getInstance()->trans("password"), 'label_attr' => array('for' => 'password'), 'data' => isset($config_data['password_sandbox']) ? $config_data['password_sandbox'] : ""))->add("signature", "text", array('constraints' => array(new NotBlank()), 'label' => Translator::getInstance()->trans("signature"), 'label_attr' => array('for' => 'signature'), 'data' => isset($config_data['signature_sandbox']) ? $config_data['signature_sandbox'] : ""))->add("sandbox", "checkbox", array('label' => Translator::getInstance()->trans("Activate sandbox mode"), 'label_attr' => array('for' => 'sandbox'), 'value' => isset($config_data['sandbox']) ? $config_data['sandbox'] : "")); }
public function go($order_id) { /* * vars used for setExpressCheckout * $order Order The order object, which is used to get products and prices * $config ConfigInterface Object that contains configuration * $api PaypalApiCredentials Class used by the library to store and use 3T login(username, password, signature) * $redirect_api PaypalApiManager Instance of PaypalApiManager, only used to get checkout url ( and redirect to paypal ) * $sandbox bool true if sandbox is enabled * $products array(array) 2D array that stores products in usable NVP format. * $i int counter * $logger PaypalApiLogManager used to log transactions with paypal */ $order = OrderQuery::create()->findPk($order_id); $config = new PaypalConfig(); $config->pushValues(); $api = new PaypalApiCredentials($config); $redirect_api = new PaypalApiManager($config); $sandbox = $api->getConfig()->getSandbox(); $products = array(array()); $i = 0; $logger = new PaypalApiLogManager(); /* * Store products into 2d array $products */ $products_amount = 0; foreach ($order->getOrderProducts() as $product) { if ($product !== null) { $amount = floatval($product->getWasInPromo() ? $product->getPromoPrice() : $product->getPrice()); foreach ($product->getOrderProductTaxes() as $tax) { $amount += $product->getWasInPromo() ? $tax->getPromoAmount() : $tax->getAmount(); } $products_amount += $amount * $product->getQuantity(); $products[0]["NAME" . $i] = urlencode($product->getTitle()); $products[0]["AMT" . $i] = urlencode(round($amount, 2)); $products[0]["QTY" . $i] = urlencode($product->getQuantity()); $i++; } } /* * Compute difference between prodcts total and cart amount * -> get Coupons. */ $delta = round($products_amount - $order->getTotalAmount($useless, false), 2); if ($delta > 0) { $products[0]["NAME" . $i] = Translator::getInstance()->trans("Discount"); $products[0]["AMT" . $i] = -$delta; $products[0]["QTY" . $i] = 1; } /* * Create setExpressCheckout request */ $setExpressCheckout = new PaypalNvpOperationsSetExpressCheckout($api, round($order->getTotalAmount(), 2), $order->getCurrency()->getCode(), Paypal::getPaypalURL('paiement', $order_id), Paypal::getPaypalURL('cancel', $order_id), 0, array("L_PAYMENTREQUEST" => $products, "PAYMENTREQUEST" => array(array("SHIPPINGAMT" => round($order->getPostage(), 2), "ITEMAMT" => round($order->getTotalAmount($useless, false), 2))))); /* * Try to get customer's delivery address */ $address = OrderAddressQuery::create()->findPk($order->getDeliveryOrderAddressId()); if ($address !== null) { /* * If address is found, set address in setExpressCheckout request */ $setExpressCheckout->setCustomerDeliveryAddress($address->getLastname(), $address->getAddress1(), $address->getAddress2(), $address->getCity(), "", $address->getZipcode(), CountryQuery::create()->findPk($address->getCountryId())->getIsoalpha2()); /* * $sender PaypalNvpMessageSender Instance of the class that sends requests * $response string NVP response of paypal for setExpressCheckout request * $req array array cast of NVP response */ $sender = new PaypalNvpMessageSender($setExpressCheckout, $sandbox); $response = $sender->send(); $logger->logTransaction($response); $response = PaypalApiManager::nvpToArray($response); /* * if setExpressCheckout is correct, store values in the session & redirect to paypal checkout page * else print error. ( return $this->render ... ) */ if (isset($response['ACK']) && $response['ACK'] === "Success" && isset($response['TOKEN']) && !empty($response['TOKEN'])) { $sess = $this->getRequest()->getSession(); $sess->set("Paypal.token", $response['TOKEN']); return new RedirectResponse($redirect_api->getExpressCheckoutUrl($response['TOKEN'])); } } return $this->render("gotopaypalfail", array(), 500); }
/** * Exclude object from result * * @param ChildPaypalConfig $paypalConfig Object to remove from the list of results * * @return ChildPaypalConfigQuery The current query, for fluid interface */ public function prune($paypalConfig = null) { if ($paypalConfig) { $this->addUsingAlias(PaypalConfigTableMap::NAME, $paypalConfig->getName(), Criteria::NOT_EQUAL); } return $this; }
/** * @param null $file */ public function write() { $dbvals = $this->getDbValues(); $isnew = array(); foreach ($dbvals as $var) { /** @var PaypalConfig $var */ $isnew[$var->getName()] = true; } $this->pushValues(); $vars = $this->getThisVars(); foreach ($vars as $key => $value) { $tmp = new PaypalConfig(); $tmp->setNew(!isset($isnew[$key])); $tmp->setName($key); $tmp->setValue($value); $tmp->save(); } }