/** * Execute the console command. * * @return mixed */ public function fire() { try { /** * This command will be run by crontab every night, * and check the all the drivers if the subscription ended. */ $drivers = EloquentDriverRepository::where('status', '=', 1)->get(); foreach ($drivers as $key => $driver) { $subscription = $driver->subscription; /** * Need charge commission fee. */ if ($subscription->needChargeCommission() && !$driver->paidCommission($subscription->current_period_end)) { $this->payCommission($subscription, $driver); } /** * Need change subscription. */ if ($subscription->needChargeSubscription()) { $plan = EloquentPlanRepository::findOrFail($subscription->plan_id); if ($subscriptionPayment = $this->paypal->subscriptionPayment($plan, $driver)) { $subscription->subscribe(EloquentPlanRepository::findOrFail($subscription->plan_id)); // Todo:: record all successful subscription payment. return; } // Todo:: record all failed subscription payment } } } catch (\Exception $e) { print_r($e->getMessage()); // Todo:: record the exception in subscription log. } }
protected function seedTestData() { $faker = Faker::create(); $store = Driver::create(array('email' => $faker->email, 'password' => \Hash::make($faker->name), 'photo' => $faker->imageUrl(250, 250), 'name' => $faker->name, 'seller_id' => $faker->firstName, 'address' => array('street' => $faker->streetAddress, 'city' => $faker->city, 'country' => $faker->country, 'postcode' => $faker->postcode), 'currency' => 'USD')); Category::create(array('name' => 'food', 'description' => $faker->text(), 'status' => 1)); Product::create(array('name' => $faker->name, 'driver_id' => $store->id, 'description' => $faker->text(), 'photos' => $faker->imageUrl(250, 250), 'tax_amount' => $faker->randomFloat(2, 0, 2), 'unit_price' => $faker->randomFloat(1, 2, 10), 'category_id' => 1, 'inventory' => 0, 'average_cost' => 0)); }
public function run() { DB::table('drivers')->truncate(); $faker = Faker::create(); for ($i = 0; $i < 10; $i++) { Driver::create(array('email' => $faker->email, 'password' => Hash::make('password'), 'photo' => $faker->imageUrl(250, 250), 'name' => $faker->name, 'seller_id' => $faker->firstName, 'address' => array('street' => $faker->streetAddress, 'city' => $faker->city, 'country' => $faker->country, 'postcode' => $faker->postcode), 'currency' => 'USD')); } }
/** * Handler to be executed after client subscription. * * @param ConnectionInterface $connection * @param Topic $topic * @return void */ public function subscribe(ConnectionInterface $connection, Topic $topic) { parent::subscribe($connection, $topic); $driver = EloquentDriverRepository::find($connection->userId); $to = Carbon::createFromTimestamp(Carbon::now()->setTimezone(\Config::get('app.timezone'))->format('U')); $from = Carbon::createFromTimestamp(Carbon::now()->setTimezone(\Config::get('app.timezone'))->format('U') - 60 * 60 * \Config::get('notification_hours')); $notifications = $driver->with_notifications($from, $to); $connection->event($topic->getId(), $notifications); }
/** * Handles a Request to convert it to a Response. * * When $catch is true, the implementation must catch all exceptions * and do its best to convert them to a Response instance. * * @param Request $request A Request instance * @param int $type The type of the request * (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST) * @param bool $catch Whether to catch exceptions or not * * @return Response A Response instance * * @throws \Exception When an Exception occurs during processing * * @api */ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true) { $this->app->boot(); if ($authorization = $request->headers->get('authorization')) { $token = $this->validateToken($authorization); // 1. Check owner_type if ($token['owner_type'] == 'user') { if ($driver = EloquentDriverRepository::findOrFail($token['owner_id'])) { $subscription = $driver->subscription; /** * Need charge commission fee. */ if ($subscription->needChargeCommission() && !$driver->paidCommission($subscription->current_period_end)) { $this->payCommission($subscription, $driver); } /** * Need charge subscription fee. */ if ($subscription->needChargeSubscription()) { if ($subscriptionPayment = $this->paypal->subscriptionPayment(EloquentPlanRepository::findOrFail($subscription->plan_id), $driver)) { $subscription->renewSubscription(EloquentPlanRepository::findOrFail($subscription->plan_id), $driver); } else { $subscription->expired(); // Todo:: record errors and send email to info user account expired. } } /** * * Canceled status. * * Don't charge subscription, change driver status to 0, and change driver subscription to past_due. * */ if ($subscription->status == 'canceled') { if (Carbon::now() >= $subscription->current_period_end) { $subscription->expired(); // Todo:: send email info user account expired. } } } } } $response = $this->app->handle($request, $type, $catch); return $response; }
/** * @param $commission * * @return \Illuminate\Http\JsonResponse */ public function billing($commission) { try { \DB::beginTransaction(); if ($driver = EloquentDriverRepository::find($commission->driver_id)) { $notification = []; if ($driver->notify_billing) { $notification['driver_id'] = $commission->driver_id; $notification['billing'] = $commission->commissions . ' ' . $commission->currency; if ($notification = NotificationRepository::create($notification)) { \DB::commit(); return $this->setStatusCode(201)->respondWithItem($notification); } } } return true; } catch (ValidationException $e) { return $this->errorWrongArgs($e->getErrors()); } }
/** * Email sticker pdf to driver * * @param $job * @param $data */ public function email($job, $data) { $view_data = !empty($data['data']) ? $data['data'] : []; // Process the send sticker pdf to user email event... try { \Mail::queue($data['template'], $view_data, function ($message) use($data) { $message->from($data['context']['from'], $data['context']['name'])->subject($data['context']['subject']); $message->to($data['to']); if (isset($data['attach'])) { $message->attach($data['attach'], array('as' => $data['as'], 'mime' => $data['mime'])); } }); if ($driver = Driver::findByEmail($data['to'])) { $driver->type = "emails"; \Event::fire('paxifi.notifications.emails', [$driver]); } $job->delete(); } catch (\Exception $e) { return \Response::json(["error" => true, "message" => ""], 500, []); } }
/** * Paypal subscription ipn handler. */ public function subscribe() { try { \DB::beginTransaction(); $listener = new Listener(); $verifier = new CurlVerifier(); $ipn = \Input::all(); $ipnMessage = new Message($ipn); $verifier->setIpnMessage($ipnMessage); $verifier->setEnvironment(\Config::get('paxifi.paypal.environment')); $listener->setVerifier($verifier); $listener->listen(function () use($listener, $ipn, &$response) { // on verified IPN (everything is good!) $resp = $listener->getVerifier()->getVerificationResponse(); // Find paid driver (custom is driver_id). if ($driver = EloquentDriverRepository::find(\Input::get('custom'))) { \Event::fire('paxifi.paypal.subscription.' . $ipn['txn_type'], [$driver, $ipn]); \DB::commit(); } }, function () use($listener) { // on invalid IPN (somethings not right!) $report = $listener->getReport(); $resp = $listener->getVerifier()->getVerificationResponse(); return $this->setStatusCode(400)->respondWithError('Subscription failed.'); }); } catch (\RuntimeException $e) { return $this->setStatusCode(400)->respondWithError($e->getMessage()); } catch (\Exception $e) { return $this->errorInternalError(); } }
/** * @param EloquentDriverRepository $driver * * @return bool|mixed */ public function buySticker(EloquentDriverRepository $driver) { $transaction = $this->getFuturePaymentTransaction($driver->getStickerPrice(), $driver->currency, "Sticker Payment"); $payment = $this->createPayment(null, $transaction, $driver); // Capture the payment if ($capturedPayment = $this->capturePayment(null, $payment, $driver)) { // Todo:: record capture purchase sticker payment success. return $capturedPayment; } else { // Todo:: record capture purchase sticker payment failed. return false; } }
/** * Remove the specified product from storage. * * @param \Paxifi\Store\Repository\Driver\EloquentDriverRepository $driver * @param int $productId * * @return \Illuminate\Http\JsonResponse */ public function destroy($driver, $productId) { try { $product = $driver->products()->findOrFail($productId); $product->delete(); \Event::fire('paxifi.product.deleted', [$product]); return $this->setStatusCode(204)->respond(array()); } catch (ModelNotFoundException $e) { return $this->errorNotFound($this->translator->trans('responses.product.not_found', array('id' => $productId))); } catch (\Exception $e) { return $this->errorInternalError(); } }
protected function seedTestData() { Driver::create(array('email' => $this->faker->email, 'password' => \Hash::make($this->faker->name), 'photo' => $this->faker->imageUrl(250, 250), 'name' => $this->faker->name, 'seller_id' => $this->faker->firstName, 'address' => array('street' => $this->faker->streetAddress, 'city' => $this->faker->city, 'country' => $this->faker->country, 'postcode' => $this->faker->postcode), 'currency' => 'USD')); Category::create(array('name' => 'food', 'description' => $this->faker->text(), 'status' => 1)); }