예제 #1
0
 /**
  * Proccess sale transaction
  *
  * @param Request $request
  * @param Transaction $transaction
  * @param Delivery $delivery
  *
  * @return stdClass
  */
 public function process(Request $request, Transaction $transaction, Delivery $delivery)
 {
     // in your controller
     $transactionService = $this->get('transaction');
     $data = $request->get('braintree');
     $nonce = $data['payment_method_nonce'];
     $result = $transactionService::sale(['amount' => $transaction->getTotalPrice(), 'paymentMethodNonce' => $nonce]);
     $em = $this->container->get('doctrine')->getManager();
     $pm = $em->getRepository('EcommerceBundle:PaymentMethod')->findOneBySlug('braintree');
     if ($result->success || !is_null($result->transaction)) {
         //UPDATE TRANSACTION
         $transaction->setStatus(Transaction::STATUS_PAID);
         $transaction->setPaymentMethod($pm);
         //details
         $details = new stdClass();
         $details->id = $result->transaction->id;
         $details->status = $result->transaction->status;
         $details->type = $result->transaction->type;
         $details->currencyIsoCode = $result->transaction->currencyIsoCode;
         $details->amount = $result->transaction->amount;
         $details->merchantAccountId = $result->transaction->merchantAccountId;
         $details->createdAt = $result->transaction->createdAt;
         $details->updatedAt = $result->transaction->updatedAt;
         $details->customer = $result->transaction->customer;
         $details->billing = $result->transaction->billing;
         $details->shipping = $result->transaction->shipping;
         $transaction->setPaymentDetails(json_encode($details));
         $em->persist($transaction);
         $em->flush();
         //confirmation payment
         $answer = new stdClass();
         $answer->redirectUrl = $this->container->get('router')->generate('ecommerce_checkout_confirmationpayment');
         return $answer;
     } else {
         //UPDATE TRANSACTION
         $transaction->setStatus(Transaction::STATUS_CANCELLED);
         $transaction->setPaymentMethod($pm);
         //details
         $errorString = "";
         foreach ($result->errors->deepAll() as $error) {
             $errorString .= 'Error: ' . $error->code . ": " . $error->message . "\n";
         }
         $this->container->get('session')->getFlashBag()->add('error', $errorString);
         $transaction->setPaymentDetails(json_encode($errorString));
         $em->persist($transaction);
         $em->flush();
         //cancel payment
         $answer = new stdClass();
         $answer->redirectUrl = $this->container->get('router')->generate('ecommerce_checkout_cancelationpayment');
         return $answer;
     }
 }
예제 #2
0
 public function createSale($agreement, $amount, $feeAmount, $netAmount, $details)
 {
     $transaction = new Transaction();
     $transaction->setTransactionKey(uniqid());
     $transaction->setStatus(Transaction::STATUS_PAID);
     $transaction->setTotalPrice($netAmount);
     $transaction->setTax(abs($feeAmount));
     $transaction->setPaymentMethod($agreement->getPaymentMethod());
     $transaction->setPaymentDetails($details);
     $transaction->setActor($agreement->getContract()->getActor());
     $agreement->addTransaction($transaction);
     $productPurchase = new ProductPurchase();
     $productPurchase->setPlan($agreement->getPlan());
     //this relation exist in productPurchase-transaction-agreement-plan
     $productPurchase->setQuantity(1);
     $productPurchase->setBasePrice($amount);
     $productPurchase->setTotalPrice($amount);
     $productPurchase->setTransaction($transaction);
     $productPurchase->setCreated(new \DateTime('now'));
     $productPurchase->setReturned(false);
     $this->manager->persist($productPurchase);
     $transaction->addItem($productPurchase);
     $this->manager->persist($transaction);
     $this->manager->flush();
     $this->createInvoice(null, $transaction);
 }