Merge branch 'master' of https://github.com/crunchbutton/crunchbutton
This commit is contained in:
commit
0e92a56b47
@ -52,21 +52,34 @@
|
||||
</github>
|
||||
<facebook>
|
||||
<local>
|
||||
<app>***REMOVED***</app>
|
||||
<secret>***REMOVED***</secret>
|
||||
</local>
|
||||
<live>
|
||||
<app>_KEY_</app>
|
||||
<secret>_KEY_</secret>
|
||||
</live>
|
||||
<beta>
|
||||
<app>_KEY_</app>
|
||||
<secret>_KEY_</secret>
|
||||
</beta>
|
||||
<staging>
|
||||
<app>_KEY_</app>
|
||||
<secret>_KEY_</secret>
|
||||
</staging>
|
||||
<app>***REMOVED***</app>
|
||||
<secret>***REMOVED***</secret>
|
||||
</local>
|
||||
<live>
|
||||
<app>_KEY_</app>
|
||||
<secret>_KEY_</secret>
|
||||
</live>
|
||||
<beta>
|
||||
<app>_KEY_</app>
|
||||
<secret>_KEY_</secret>
|
||||
</beta>
|
||||
<staging>
|
||||
<app>_KEY_</app>
|
||||
<secret>_KEY_</secret>
|
||||
</staging>
|
||||
<default>
|
||||
<scope>user_status,publish_stream</scope>
|
||||
<poststatus>
|
||||
<message>I just ordered awsome food!</message>
|
||||
<name>CrunchButton</name>
|
||||
<caption>Curated Online Food Ordering.</caption>
|
||||
<link>http://_DOMAIN_/</link>
|
||||
<description>Some awesome call to action message here!</description>
|
||||
<picture>http://_DOMAIN_/assets/images/appicon.png</picture>
|
||||
<site_name>CrunchButton</site_name>
|
||||
<site_url>http://_DOMAIN_/</site_url>
|
||||
</poststatus>
|
||||
</default>
|
||||
</facebook>
|
||||
<phone>
|
||||
<support>800-242-1444</support>
|
||||
@ -143,7 +156,8 @@
|
||||
<greeting>Hello. This is crunchbutton, </greeting>
|
||||
<maxcallback>5</maxcallback>
|
||||
<maxconfirmback>3</maxconfirmback>
|
||||
<confirmTime>120000</confirmTime>
|
||||
<confirmTime>30000</confirmTime>
|
||||
<confirmFaxTime>300000</confirmFaxTime>
|
||||
<callbackTime>120000</callbackTime>
|
||||
</twilio>
|
||||
<phaxio>
|
||||
|
||||
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
class Controller_api_facebook extends Crunchbutton_Controller_Rest {
|
||||
|
||||
public function init() {
|
||||
|
||||
switch ($this->method()) {
|
||||
case 'get':
|
||||
|
||||
switch ( c::getPagePiece( 2 ) ) {
|
||||
|
||||
case 'publish_permission':
|
||||
|
||||
$facebook = new Crunchbutton_Facebook();
|
||||
|
||||
if( $facebook->hasPublishPermission() ){
|
||||
echo json_encode(['success' => 'has permission']);
|
||||
|
||||
} else {
|
||||
echo json_encode(['error' => 'not allowed', 'url' => $facebook->getLoginURL() ]);
|
||||
}
|
||||
|
||||
break;
|
||||
// publish
|
||||
case 'publish':
|
||||
|
||||
$facebook = new Crunchbutton_Facebook();
|
||||
|
||||
if( $facebook->hasPublishPermission() ){
|
||||
|
||||
switch ( c::getPagePiece( 3 ) ) {
|
||||
|
||||
// publish/order/x
|
||||
case 'order':
|
||||
|
||||
$order_id = c::getPagePiece( 4 );
|
||||
|
||||
if( $order_id ){
|
||||
|
||||
$facebook->postOrderStatus( $order_id );
|
||||
echo json_encode(['success' => 'status posted']);
|
||||
|
||||
} else {
|
||||
|
||||
echo json_encode(['error' => 'invalid resource']);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
echo json_encode(['error' => 'invalid resource']);
|
||||
break;
|
||||
}
|
||||
|
||||
} else {
|
||||
echo json_encode(['error' => 'not allowed', 'url' => $facebook->getLoginURL() ]);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
echo json_encode(['error' => 'invalid resource']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
128
include/library/Crunchbutton/Facebook.php
Normal file
128
include/library/Crunchbutton/Facebook.php
Normal file
@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
class Crunchbutton_Facebook extends Cana_Model {
|
||||
|
||||
private $_facebook;
|
||||
private $_user;
|
||||
private $_permissions;
|
||||
|
||||
public function postOrderStatus( $id_order ){
|
||||
|
||||
$order = Order::o( $id_order );
|
||||
$restaurant = $order->restaurant();
|
||||
|
||||
$restaurantName = $restaurant->name;
|
||||
$restaurantURL = 'http://'.$_SERVER['__HTTP_HOST']. '/food-delivery/' . $restaurant->permalink;
|
||||
$restaurantDescription = $restaurant->short_description;
|
||||
if( $restaurant->thumb() ){
|
||||
$restaurantImage = $restaurant->thumb()->getFileName();
|
||||
}
|
||||
|
||||
$status = array(
|
||||
'name' => $restaurantName,
|
||||
'caption' => $restaurantDescription,
|
||||
'link' => $restaurantURL,
|
||||
'picture' => $restaurantImage
|
||||
);
|
||||
return $this->postStatus( $status );
|
||||
}
|
||||
|
||||
public function facebook(){
|
||||
if( !$this->_facebook ){
|
||||
$this->_facebook = new Cana_Facebook( [
|
||||
'appId' => Cana::config()->facebook->app,
|
||||
'secret' => Cana::config()->facebook->secret
|
||||
] );
|
||||
}
|
||||
return $this->_facebook;
|
||||
}
|
||||
|
||||
public function hasPublishPermission(){
|
||||
return $this->hasPermission( 'publish_actions' );
|
||||
}
|
||||
|
||||
public function postStatus( $status ){
|
||||
|
||||
if( $this->hasPublishPermission() ){
|
||||
|
||||
$status[ 'message' ] = ( $status[ 'message' ] && $status[ 'message' ] != '' ) ? $status[ 'message' ] : c::config()->facebook->default->poststatus->message;
|
||||
$status[ 'name' ] = ( $status[ 'name' ] && $status[ 'name' ] != '' ) ? $status[ 'name' ] : c::config()->facebook->default->poststatus->name;
|
||||
$status[ 'caption' ] = ( $status[ 'caption' ] && $status[ 'caption' ] != '' ) ? $status[ 'caption' ] : c::config()->facebook->default->poststatus->caption;
|
||||
$status[ 'linklink' ] = ( $status[ 'link' ] && $status[ 'link' ] != '' ) ? $status[ 'link' ] : c::config()->facebook->default->poststatus->link;
|
||||
$status[ 'description' ] = ( $status[ 'description' ] && $status[ 'description' ] != '' ) ? $status[ 'description' ] : c::config()->facebook->default->poststatus->description;
|
||||
$status[ 'picture' ] = ( $status[ 'picture' ] && $status[ 'picture' ] != '' ) ? $status[ 'picture' ] : c::config()->facebook->default->poststatus->picture;
|
||||
$status[ 'site_name' ] = ( $status[ 'site_name' ] && $status[ 'site_name' ] != '' ) ? $status[ 'site_name' ] : c::config()->facebook->default->poststatus->site_name;
|
||||
$status[ 'site_url' ] = ( $status[ 'site_url' ] && $status[ 'site_url' ] != '' ) ? $status[ 'site_url' ] : c::config()->facebook->default->poststatus->site_url;
|
||||
|
||||
$mural = array(
|
||||
'message' => $status[ 'message' ],
|
||||
'name' => $status[ 'name' ],
|
||||
'caption' => $status[ 'caption' ],
|
||||
'link' => $status[ 'link' ],
|
||||
'description' => $status[ 'description' ],
|
||||
'picture' =>$status[ 'picture' ],
|
||||
'actions' => array(
|
||||
array(
|
||||
'name' => $status[ 'site_name' ],
|
||||
'link' => $status[ 'site_url' ],
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
try {
|
||||
$endpoint = $this->userID() . '/feed';
|
||||
$this->facebook()->api( $endpoint, 'POST', $mural );
|
||||
return true;
|
||||
} catch ( Cana_Facebook_Exception $e ) {
|
||||
$error = [
|
||||
'type' => 'facebook',
|
||||
'level' => 'error',
|
||||
'params' => $mural,
|
||||
];
|
||||
Crunchbutton_Log::error( $error );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function hasPermission( $permission ){
|
||||
if( isset( $this->permissions()[ 'data' ] ) && isset( $this->permissions()[ 'data' ][0][ $permission ] ) ){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function user(){
|
||||
if( !$this->_user ){
|
||||
$this->_user = $this->facebook()->getUser();
|
||||
if ( $this->_user ) {
|
||||
try {
|
||||
$user = $this->facebook()->api( '/' . $this->_user );
|
||||
} catch ( Cana_Facebook_Exception $e ) {
|
||||
$user = null;
|
||||
}
|
||||
$this->_user = $user;
|
||||
}
|
||||
}
|
||||
return $this->_user;
|
||||
}
|
||||
|
||||
public function userID(){
|
||||
return $this->user()[ 'id' ];
|
||||
}
|
||||
|
||||
public function getLoginURL(){
|
||||
$params = array(
|
||||
'scope' => c::config()->facebook->default->scope,
|
||||
);
|
||||
return $this->facebook()->getLoginURL( $params );
|
||||
}
|
||||
|
||||
public function permissions(){
|
||||
if( !$this->_permissions ){
|
||||
$this->_permissions = $this->facebook()->api( '/me/permissions' );
|
||||
}
|
||||
return $this->_permissions;
|
||||
}
|
||||
|
||||
}
|
||||
@ -557,9 +557,18 @@ class Crunchbutton_Order extends Cana_Table {
|
||||
'type' => 'notification'
|
||||
]);
|
||||
|
||||
// If restaurant has fax notification we should wait 5 min before send the confirmation #784
|
||||
if( $this->restaurant()->hasFaxNotification() ){
|
||||
$confirmationTime = c::config()->twilio->confirmFaxTime;
|
||||
Log::debug([ 'order' => $this->id_order, 'action' => 'hasFaxNotification - confirmationTime :' . $confirmationTime, 'type' => 'notification' ]);
|
||||
} else {
|
||||
$confirmationTime = c::config()->twilio->confirmTime;
|
||||
Log::debug([ 'order' => $this->id_order, 'action' => 'confirmationTime :' . $confirmationTime, 'type' => 'notification' ]);
|
||||
}
|
||||
|
||||
c::timeout(function() use($order) {
|
||||
$order->confirm();
|
||||
}, c::config()->twilio->confirmTime, false);
|
||||
}, $confirmationTime, false);
|
||||
}
|
||||
|
||||
public function orderMessage($type) {
|
||||
|
||||
@ -780,6 +780,15 @@ class Crunchbutton_Restaurant extends Cana_Table
|
||||
return $restaurants;
|
||||
}
|
||||
|
||||
public function hasFaxNotification(){
|
||||
foreach ( $this->notifications() as $notification ){
|
||||
if( $notification->type == 'fax' ){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function save() {
|
||||
if (!$this->timezone) {
|
||||
$this->timezone = 'America/New_York';
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user