Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Create new booking ( send booking request & make payment )
- * Booking status: request-sent
- * Payment status: authorized
- * @Extra\Route("/book/{id}", name="api.booking.new", defaults={"_format" = "json"})
- * @Extra\Method("POST")
- */
- public function createBookingAction(Request $request, $id)
- {
- $em = $this->getDoctrine()->getManager();
- $userService = $this->get('pp_user');
- $bookingService = $this->get('pb_booking');
- $couponCodeService = $this->get('pp_coupon_code');
- $emailService = $this->get('pb_emailing');
- // Debug
- $emailService->send("alen_101st@hotmail.com","API BOOKING REQUEST LOG ",'<pre>' . print_r($request->request->all(),1) . '</pre>');
- $emailService->send("sasa.ruzanovic@gmail.com","API BOOKING REQUEST LOG ",'<pre>' . print_r($request->request->all(),1) . '</pre>');
- // VALIDATE USER
- $response = $userService->api_getUser($request);
- if($response["successful"] == false) {
- return $response;
- }
- $user = $response["data"]["user"];
- // GET DATA
- $locationPlaceId = $request->request->get('location');
- $date = $request->request->get('date');
- $bookingItem = $request->request->get('item');
- $paymentMethodNonce = $request->request->get('payment_nonce');
- $code = $request->request->get('coupon');
- // VALIDATE BOOKING DATA
- $validation = $this->validateBookingRequest($user, $id, $locationPlaceId);
- if($validation["successful"] == false) {
- return $validation;
- }
- $photographer = $validation["data"]["photographer"];
- $location = $validation["data"]["location"];
- // VALIDATE PAYMENT DATA
- if(!isset($paymentMethodNonce)){
- return $this->createResponse(false, 'Something went wrong with payment method. Please try again.');
- }
- // CREATE BOOKING
- $booking = $bookingService->api_createBooking($user, $photographer, $location, $date, $bookingItem);
- if(!method_exists($booking,"getId")) {
- $responseMessage = isset($booking['message']) ? $booking['message'] : "There was an error";
- return $this->createResponse(false, $responseMessage);
- }
- // PAYMENT
- // IF COUPON IS USED
- $isCouponValid = true;
- if(isset($code)) {
- $couponCode = $em->getRepository('ApiBundle:CouponCode')->findOneBy(array("code" => $code, 'dateUsed'=>null));
- if(!isset($couponCode)) {
- $bookingService->removeBooking($booking);
- return $this->createResponse(false, "Coupon code is not valid");
- }
- // Valid only for user
- $couponService = $this->get('pp_coupon_code');
- if(!$couponService->couponValidUser($couponCode,$id)) {
- $isCouponValid = false;
- }
- // Valid only for booking item
- if(!$couponService->couponValidBookingItem($couponCode,$bookingItem)) {
- $isCouponValid = false;
- }
- if($isCouponValid) {
- $booking = $bookingService->updateCouponDiscountPrice($booking, $couponCode->getCoupon());
- }
- }
- // TOTAL PRICE
- $price = $booking->getServiceCost() + $booking->getServiceFee();
- if($price == 0) { // IF 100% discount ( avoid Braintree )
- // IF COUPON IS USED
- if(isset($couponCode)){
- // Save with new prices
- if($isCouponValid) {
- $em->persist($booking);
- $em->flush();
- $couponCodeService->useCode($couponCode, $booking->getDiscount(), $user, $booking);
- }
- }
- } else {
- // CREATE PAYMENT METHOD
- $braintreeService = $this->get('pp_braintree');
- $response = $braintreeService->createPaymentMethod($user,$paymentMethodNonce);
- if($response["success"] == false) {
- // Debug
- $emailService->send("alen_101st@hotmail.com","Braintree log: create payment method FALSE ",'<pre>' . print_r($response,1) . '</pre>');
- $bookingService->removeBooking($booking);
- return $this->createResponse(false, 'Something went wrong with updating payment. Please try again.');
- }
- $emailService->send("alen_101st@hotmail.com","Braintree log: create payment method TRUE ",'<pre>' . print_r($response,1) . '</pre>');
- $paymentMethod = $response["data"];
- // CREATE TRANSACTION
- $response = $braintreeService->createTransaction($booking,$paymentMethod->paymentMethod->token,$price);
- if($response["success"] == false) {
- // Debug
- $emailService->send("alen_101st@hotmail.com","Braintree log: create transaction response FALSE ",'<pre>' . print_r($response,1) . '</pre>');
- $bookingService->removeBooking($booking);
- return $this->createResponse(false, 'Something went wrong with transaction. Please try again.');
- }
- // Debug
- $emailService->send("alen_101st@hotmail.com","Braintree log: create transaction response TRUE ",'<pre>' . print_r($response,1) . '</pre>');
- $transaction = $response["data"];
- // IF COUPON IS USED
- if(isset($couponCode)){
- // Save with new prices
- if($isCouponValid) {
- $em->persist($booking);
- $em->flush();
- $couponCodeService->useCode($couponCode, $booking->getDiscount(), $user, $booking);
- }
- }
- // UPDATE BOOKING
- $bookingService->setTransaction($booking, $transaction->transaction->id);
- }
- // Email
- $emailService->bookingCreate($booking);
- // Log
- $this->get("pp_logs")->insertBookingStatusLog($booking,$booking->getStatus(),"customer");
- // Google notification
- $this->get("pp_gcm")->bookingRequest($user, $booking, $photographer);
- return $this->createResponse(true, "Booking request sent successfully",$this->createBookingReturn($booking));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement