Advertisement
Guest User

Untitled

a guest
Aug 11th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. /**
  2. * Create new booking ( send booking request & make payment )
  3. * Booking status: request-sent
  4. * Payment status: authorized
  5. * @Extra\Route("/book/{id}", name="api.booking.new", defaults={"_format" = "json"})
  6. * @Extra\Method("POST")
  7. */
  8. public function createBookingAction(Request $request, $id)
  9. {
  10. $em = $this->getDoctrine()->getManager();
  11. $userService = $this->get('pp_user');
  12. $bookingService = $this->get('pb_booking');
  13. $couponCodeService = $this->get('pp_coupon_code');
  14. $emailService = $this->get('pb_emailing');
  15.  
  16. // Debug
  17. $emailService->send("alen_101st@hotmail.com","API BOOKING REQUEST LOG ",'<pre>' . print_r($request->request->all(),1) . '</pre>');
  18. $emailService->send("sasa.ruzanovic@gmail.com","API BOOKING REQUEST LOG ",'<pre>' . print_r($request->request->all(),1) . '</pre>');
  19.  
  20. // VALIDATE USER
  21. $response = $userService->api_getUser($request);
  22. if($response["successful"] == false) {
  23. return $response;
  24. }
  25. $user = $response["data"]["user"];
  26.  
  27. // GET DATA
  28. $locationPlaceId = $request->request->get('location');
  29. $date = $request->request->get('date');
  30. $bookingItem = $request->request->get('item');
  31. $paymentMethodNonce = $request->request->get('payment_nonce');
  32. $code = $request->request->get('coupon');
  33.  
  34. // VALIDATE BOOKING DATA
  35. $validation = $this->validateBookingRequest($user, $id, $locationPlaceId);
  36. if($validation["successful"] == false) {
  37. return $validation;
  38. }
  39. $photographer = $validation["data"]["photographer"];
  40. $location = $validation["data"]["location"];
  41.  
  42. // VALIDATE PAYMENT DATA
  43. if(!isset($paymentMethodNonce)){
  44. return $this->createResponse(false, 'Something went wrong with payment method. Please try again.');
  45. }
  46.  
  47. // CREATE BOOKING
  48. $booking = $bookingService->api_createBooking($user, $photographer, $location, $date, $bookingItem);
  49. if(!method_exists($booking,"getId")) {
  50. $responseMessage = isset($booking['message']) ? $booking['message'] : "There was an error";
  51. return $this->createResponse(false, $responseMessage);
  52. }
  53. // PAYMENT
  54. // IF COUPON IS USED
  55. $isCouponValid = true;
  56. if(isset($code)) {
  57. $couponCode = $em->getRepository('ApiBundle:CouponCode')->findOneBy(array("code" => $code, 'dateUsed'=>null));
  58. if(!isset($couponCode)) {
  59. $bookingService->removeBooking($booking);
  60. return $this->createResponse(false, "Coupon code is not valid");
  61. }
  62. // Valid only for user
  63. $couponService = $this->get('pp_coupon_code');
  64. if(!$couponService->couponValidUser($couponCode,$id)) {
  65. $isCouponValid = false;
  66. }
  67. // Valid only for booking item
  68. if(!$couponService->couponValidBookingItem($couponCode,$bookingItem)) {
  69. $isCouponValid = false;
  70. }
  71.  
  72. if($isCouponValid) {
  73. $booking = $bookingService->updateCouponDiscountPrice($booking, $couponCode->getCoupon());
  74. }
  75. }
  76.  
  77. // TOTAL PRICE
  78. $price = $booking->getServiceCost() + $booking->getServiceFee();
  79.  
  80. if($price == 0) { // IF 100% discount ( avoid Braintree )
  81. // IF COUPON IS USED
  82. if(isset($couponCode)){
  83. // Save with new prices
  84. if($isCouponValid) {
  85. $em->persist($booking);
  86. $em->flush();
  87. $couponCodeService->useCode($couponCode, $booking->getDiscount(), $user, $booking);
  88. }
  89. }
  90. } else {
  91. // CREATE PAYMENT METHOD
  92. $braintreeService = $this->get('pp_braintree');
  93. $response = $braintreeService->createPaymentMethod($user,$paymentMethodNonce);
  94. if($response["success"] == false) {
  95. // Debug
  96. $emailService->send("alen_101st@hotmail.com","Braintree log: create payment method FALSE ",'<pre>' . print_r($response,1) . '</pre>');
  97.  
  98. $bookingService->removeBooking($booking);
  99. return $this->createResponse(false, 'Something went wrong with updating payment. Please try again.');
  100. }
  101. $emailService->send("alen_101st@hotmail.com","Braintree log: create payment method TRUE ",'<pre>' . print_r($response,1) . '</pre>');
  102. $paymentMethod = $response["data"];
  103.  
  104. // CREATE TRANSACTION
  105. $response = $braintreeService->createTransaction($booking,$paymentMethod->paymentMethod->token,$price);
  106. if($response["success"] == false) {
  107. // Debug
  108. $emailService->send("alen_101st@hotmail.com","Braintree log: create transaction response FALSE ",'<pre>' . print_r($response,1) . '</pre>');
  109.  
  110. $bookingService->removeBooking($booking);
  111. return $this->createResponse(false, 'Something went wrong with transaction. Please try again.');
  112. }
  113. // Debug
  114. $emailService->send("alen_101st@hotmail.com","Braintree log: create transaction response TRUE ",'<pre>' . print_r($response,1) . '</pre>');
  115. $transaction = $response["data"];
  116.  
  117. // IF COUPON IS USED
  118. if(isset($couponCode)){
  119. // Save with new prices
  120. if($isCouponValid) {
  121. $em->persist($booking);
  122. $em->flush();
  123. $couponCodeService->useCode($couponCode, $booking->getDiscount(), $user, $booking);
  124. }
  125. }
  126. // UPDATE BOOKING
  127. $bookingService->setTransaction($booking, $transaction->transaction->id);
  128. }
  129. // Email
  130. $emailService->bookingCreate($booking);
  131. // Log
  132. $this->get("pp_logs")->insertBookingStatusLog($booking,$booking->getStatus(),"customer");
  133. // Google notification
  134. $this->get("pp_gcm")->bookingRequest($user, $booking, $photographer);
  135.  
  136. return $this->createResponse(true, "Booking request sent successfully",$this->createBookingReturn($booking));
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement