/** * Create the shipment for sticker. * * @param null $driver * * @return \Illuminate\Http\JsonResponse */ public function shipment($driver = null) { try { \DB::beginTransaction(); if (is_null($driver)) { $driver = $this->getAuthenticatedDriver(); } $new_shipment = ["sticker_id" => $driver->sticker->id, "address" => \Input::get('address', $driver->address), "status" => "waiting", "paypal_payment_status" => "pending"]; with(new CreateShipmentValidator())->validate($new_shipment); // Add paypal_metadata_id for future payment. $driver->paypal_metadata_id = \Input::get('metadata_id'); if ($capturedPayment = $this->paypal->buySticker($driver)) { $new_shipment['paypal_payment_id'] = $capturedPayment->parent_payment; if ($shipment = Shipment::create($new_shipment)) { \DB::commit(); return $this->setStatusCode(201)->respondWithItem($shipment); } return $this->errorInternalError('Shipment for buying sticker created failed.'); } return $this->errorInternalError(); } catch (ValidationException $e) { return $this->errorWrongArgs($e->getErrors()); } catch (\Exception $e) { print_r($e->getMessage()); return $this->errorInternalError(); } }
/** * Paypal payment for sticker. * * @param null $driver * * @return \Illuminate\Http\JsonResponse */ public function buySticker($driver = null) { try { \DB::beginTransaction(); if (is_null($driver)) { $driver = $driver = $this->getAuthenticatedDriver(); } if (!($payment = Input::get('payment'))) { return $this->errorWrongArgs(); } if ($payment['state'] == 'approved' && $payment['intent'] == 'sale') { if (!($verification = $this->paypal->getStickerPaymentVerification($payment['id']))) { throw new PaymentNotValidException('PayPal Payment not valid.'); } if ($this->verifyStickerPayment($verification, $driver)) { if (!($shipment = EloquentShipmentRepository::find(Input::get('shipment_id')))) { return $this->errorNotFound('Shipment not found'); } $shipment->payment_status = 'completed'; $shipment->save(); \DB::commit(); return $this->setStatusCode(200)->respond(['success' => true]); } } throw new PaymentNotValidException('PayPal Payment not valid.'); } catch (PaymentNotValidException $e) { return $this->setStatusCode(402)->respondWithError($e->getMessage()); } catch (\Exception $e) { return $this->setStatusCode(400)->respondWithError($e->getMessage()); } }