/**
  * Complete a order purchase by creating an order and generating a
  * success url to redirect the customer towards.
  *
  * {@inheritDoc}
  */
 public function success(PayableInterface $payable, $reference, MethodInterface $method)
 {
     // Build the payment and add it to the order
     $payment = new Payment();
     $payment->method = $method;
     $payment->amount = $payable->getPayableAmount();
     $payment->reference = $reference;
     $payable->payments->append(new OrderPayment($payment));
     // Create the order
     $payable = $this->get('order.create')->setUser($payable->user)->create($payable);
     // Generate a success url
     $salt = $this->get('cfg')->payment->salt;
     $successful = $this->generateUrl('ms.ecom.checkout.payment.successful', array('orderID' => $payable->id, 'hash' => $this->get('checkout.hash')->encrypt($payable->id, $salt)), UrlGeneratorInterface::ABSOLUTE_URL);
     // Save gateway against payment
     $gateway = $this->get('http.session')->get('gateway.current');
     // If gateway is not set on session, get default gateway
     if (null === $gateway) {
         $gatewayNames = $this->get('cfg')->payment->gateway;
         $gatewayName = is_array($gatewayNames) ? array_shift($gatewayNames) : $gatewayNames;
         $gateway = $this->get('gateway.collection')->get($gatewayName);
     }
     $this->get('payment.gateway.edit')->save($payment, $gateway);
     // Create json response with the success url
     return new JsonResponse(['url' => $successful]);
 }
 /**
  * {@inheritDoc}
  */
 public function purchase(PayableInterface $payable, array $stages, array $options = null)
 {
     // Forward to the method for completing the payable and capture the
     // response containing the success url
     $successResponse = $this->forward($stages['success'], ['payable' => $payable, 'reference' => static::REFERENCE_PREFIX . $payable->getPayableTransactionID(), 'method' => $this->get('order.payment.methods')->get('manual')]);
     $successData = (array) json_decode($successResponse->getContent());
     return $this->redirect($successData['url']);
 }
 /**
  * Complete an exchange balance payment reducing the return's remaining
  * balance by the amount paid and appending a new payment to the
  * related order.
  *
  * {@inheritDoc}
  */
 public function success(PayableInterface $payable, $reference, MethodInterface $method)
 {
     // Get the amount before adjusting the balance to ensure we can use it
     // afterwards
     $amount = $payable->getPayableAmount();
     $this->get('return.edit')->addPayment($payable, $method, $amount, $reference);
     // Generate the successful url
     $salt = $this->get('cfg')->payment->salt;
     $hash = $this->get('checkout.hash')->encrypt($payable->id, $salt);
     $successful = $this->generateUrl('ms.ecom.return.balance.successful', ['returnID' => $payable->id, 'hash' => $hash], UrlGeneratorInterface::ABSOLUTE_URL);
     // Return a JSON response with the successful url
     $response = new JsonResponse();
     $response->setData(['url' => $successful]);
     return $response;
 }
 /**
  * Check if the payable is valid against the address validation rules.
  *
  * @todo Add validation of the address against valid countries.
  *
  * {@inheritDoc}
  */
 public function isValid(PayableInterface $payable)
 {
     $valid = true;
     $address = $payable->getPayableAddress($this->_type);
     // Check the address exists
     if (!$address) {
         $this->_errors[] = sprintf("%s address is required", ucfirst($this->_type));
         return false;
     }
     // Check the address parts
     foreach ($this->_parts as $key => $part) {
         if ($key === "lines") {
             for ($line = 1; $line <= $part; $line++) {
                 if (!property_exists($address, "lines") or !is_array($address->lines) or !isset($address->lines[$line])) {
                     $valid = false;
                     $this->_errors[] = sprintf("%s address line %d is required", ucfirst($this->_type), $line);
                 }
             }
         } else {
             if (!property_exists($address, $part) or !$address->{$part}) {
                 $valid = false;
                 $this->_errors[] = sprintf("%s address %s is required", ucfirst($this->_type), $part);
             }
         }
     }
     // Check the address state is set where required by the country and
     // matches the states within that country.
     $states = $this->_states->all();
     if (isset($states[$address->countryID])) {
         if (empty($address->stateID)) {
             $valid = false;
             $this->_errors[] = sprintf("%s address state is required", ucfirst($this->_type));
         } elseif (!isset($states[$address->countryID][$address->stateID])) {
             $valid = false;
             $this->_errors[] = sprintf("%s address state '%s' is not in the country '%s'", ucfirst($this->_type), $address->stateID, $address->countryID);
         }
     }
     return $valid;
 }
 public function getAmountBySmallestCurrencyUnit(PayableInterface $payable)
 {
     return in_array($payable->getPayableCurrency(), $this->_zeroDecimalCurrencies) ? (int) $payable->getPayableAmount() : (int) ($payable->getPayableAmount() * 100);
 }
 public function failure(PayableInterface $payable)
 {
     $this->addFlash('error', sprintf('Could not refund %s %s to customer. Please refund the amount manually.', number_format($payable->getPayableAmount(), 2), $payable->getPayableCurrency()));
     return $this->redirectToRoute($this->_url, ['orderID' => $payable->getOrder()->id]);
 }