diff --git a/.env.example b/.env.example index 333bfc0..990696d 100644 --- a/.env.example +++ b/.env.example @@ -32,9 +32,12 @@ PUSHER_APP_ID= PUSHER_APP_KEY= PUSHER_APP_SECRET= - INVISIBLE_RECAPTCHA_SITEKEY=6LfdQSMUAAAAAOP31dkmdZJeNvDghlBfqJsHT5M- INVISIBLE_RECAPTCHA_SECRETKEY=6LfdQSMUAAAAAAT5cKXa7lI3koiFy4c5BP7BX3m8 NOCAPTCHA_SECRET=6LfdQSMUAAAAAAT5cKXa7lI3koiFy4c5BP7BX3m8 NOCAPTCHA_SITEKEY=6LfdQSMUAAAAAOP31dkmdZJeNvDghlBfqJsHT5M- + +FB_APP_ID=424974257871315 +FB_APP_SECRET=94e6ed7204eb94082bc87feeb7bea7ca +FB_PAGE_TOKEN=EAAGCgwet2dMBAJJsZBgVTZCb4jdgTVnJD8TjvsOQ5JKyxEYWc7d6xSeosfZBqjITY95qC0jQ66RsBRZBlprHCbE5YYQl6cwaKHZAR4MXLZA977jtCaOP3Q5ZCXY7UrBJCV6IOcIAbmsBPFNoyecLgn6qVMecdxQrspbd31AoxvZBz4bZBZCOPSC6Cb diff --git a/README.md b/README.md index 75392c2..88e12ca 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,29 @@ chmod -R 777 storage/ chmod 777 bootstrap/cache ``` +#### Facebook intergration + +Facebook intergration automatically post need requests to a facebook page. A facebook app and administrative rights to a page is required. Using them an access token to page should be generated to put in .env file. The app does not need to be approved if the page admin has has administrative rights to the app. + +![](http://d.pr/i/R3WrWj+) + +![](http://d.pr/i/othvIj+) +Click "Get Access Token" + +![](http://d.pr/i/JL3vnV+) + +The access token is short lived one (2 hours), now we need to convert it to long-lived. + +Visit below url and it will give you the access token + +`https://graph.facebook.com/oauth/access_token? + client_id=APP_ID& + client_secret=APP_SECRET& + grant_type=fb_exchange_token& + fb_exchange_token=EXISTING_ACCESS_TOKEN ` + +Refer to .env.sample to place the credentials. + ### Pull requests Send all the PRs to `dev` branch. We keep `master` and `prod` branches only for final releases and all the development works on the `dev`. diff --git a/app/Http/Controllers/NeedsController.php b/app/Http/Controllers/NeedsController.php index d442e6c..16362ee 100644 --- a/app/Http/Controllers/NeedsController.php +++ b/app/Http/Controllers/NeedsController.php @@ -6,18 +6,24 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Response; use Validator; +use App\Services\FacebookNotification; +use DomainException; class NeedsController extends Controller { private $need; + private $fbNotifier; + /** * NeedsController constructor. * @param NeedsRepository $needsRepository + * @param FacebookNotification $fbNotifier */ - public function __construct(NeedsRepository $needsRepository) + public function __construct(NeedsRepository $needsRepository, FacebookNotification $fbNotifier) { $this->need = $needsRepository; + $this->fbNotifier = $fbNotifier; } /** @@ -68,17 +74,27 @@ public function save(Request $request) ], $messages); if ($validator->fails()) { + return redirect('/needs/add') ->with('isSuccess', false) ->with('errors', $validator->errors()->all()) ->withInput(); } else { - $response = $this->need->addNeed($request->all()); - if ($response) { + + try { + + $need = $this->need->addNeed($request->all()); + + //need is being created first and if fb posting fail rest of the logic + //is executed + $fbId = $this->fbNotifier->publishNeed($need); + $this->need->updateNeed($need->id, ['fb_post_id' => $fbId]); + return redirect('/needs') ->with('isSuccess', true) ->with('message', 'සාර්ථකව ඇතුලත්කරන ලදී.'); - } else { + + } catch (DomainException $e) { return redirect('/needs/add') ->with('isSuccess', false) ->with('errors', ['ඇතුලත්කිරීම දෝෂ සහිතය.']) diff --git a/app/Need.php b/app/Need.php index 4e626ad..6f09fb2 100644 --- a/app/Need.php +++ b/app/Need.php @@ -12,6 +12,6 @@ class Need extends Model * @var array */ protected $fillable = [ - 'name', 'telephone', 'address', 'city', 'needs', 'heads', 'source' + 'name', 'telephone', 'address', 'city', 'needs', 'heads', 'source', 'fb_post_id' ]; } diff --git a/app/Repositories/NeedsRepository.php b/app/Repositories/NeedsRepository.php index 0186d6f..40cdd11 100644 --- a/app/Repositories/NeedsRepository.php +++ b/app/Repositories/NeedsRepository.php @@ -11,6 +11,7 @@ use App\Need; use App\Repositories\Contracts\NeedsInterface; use Illuminate\Support\Facades\Log; +use DomainException; class NeedsRepository implements NeedsInterface { @@ -23,10 +24,20 @@ class NeedsRepository implements NeedsInterface public function addNeed($input) { try { - Need::create($input); - return true; + return Need::create($input); } catch (\Exception $e) { - Log::error($e->getMessage()); + throw new DomainException('Unable to create need.'); + } + } + + public function updateNeed($id, array $input) + { + $need = $this->findNeed($id); + + if ($need) { + $need->fill($input); + return $need->save(); + } else { return false; } } diff --git a/app/Services/FacebookNotification.php b/app/Services/FacebookNotification.php new file mode 100644 index 0000000..a61a168 --- /dev/null +++ b/app/Services/FacebookNotification.php @@ -0,0 +1,51 @@ +fb = new Facebook([ + 'app_id' => Config::get('services.facebook.app_id'), + 'app_secret' => Config::get('services.facebook.app_secret'), + 'default_graph_version' => 'v2.9', + 'default_access_token' => Config::get('services.facebook.page_token') + ]); + } + + public function publishNeed(Need $need) + { + $content = []; + $content[] = $need->needs; + $content[] = $need->address; + $content[] = $need->city; + $content[] = "People - ". $need->heads; + $content[] = $need->name.' - '.$need->telephone; + + try { + + $response = $this->fb->post('/me/feed', ['message' => implode($content, "\n")]); + $response->decodeBody(); + return array_get($response->getDecodedBody(), 'id'); + + } catch(FacebookResponseException $e) { + Log::error($e); + return false; + } catch(FacebookSDKException $e) { + Log::error($e); + return false; + } + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index 103d83b..7b4e934 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,7 @@ "type": "project", "require": { "php": ">=5.6.4", + "facebook/graph-sdk": "^5.5", "laravel/framework": "5.4.*", "laravel/tinker": "~1.0", "albertcht/invisible-recaptcha": "^1.3" diff --git a/config/services.php b/config/services.php index 4460f0e..8efb882 100644 --- a/config/services.php +++ b/config/services.php @@ -35,4 +35,10 @@ 'secret' => env('STRIPE_SECRET'), ], + 'facebook' => [ + 'app_id' => env('FB_APP_ID'), + 'app_secret' => env('FB_APP_SECRET'), + 'page_token' => env('FB_PAGE_TOKEN') + ] + ]; diff --git a/database/migrations/2017_05_29_003241_add_fb_post_id_to_needs.php b/database/migrations/2017_05_29_003241_add_fb_post_id_to_needs.php new file mode 100644 index 0000000..a0dfc8c --- /dev/null +++ b/database/migrations/2017_05_29_003241_add_fb_post_id_to_needs.php @@ -0,0 +1,32 @@ +string("fb_post_id", 100)->after("heads")->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('needs', function (Blueprint $table) { + $table->dropColumn("needs"); + }); + } +}