/
var
/
www
/
html
/
web
/
app
/
Http
/
Controllers
/
Upload File
HOME
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Osiset\ShopifyApp\Services\ChargeHelper; use Log; use App\Models\Plan; use App\Models\User; use Osiset\ShopifyApp\Objects\Values\ChargeReference; use GuzzleHttp\Client; class ShopifySettingController extends Controller { /** * * Show welcome screen * * After installation of APP * */ public function index(Request $request) { $shop = Auth::user(); $domain = $shop->getDomain()->toNative(); $shopApi = $shop->api()->rest('GET', '/admin/shop.json'); $shop_info_data=array(); $shop_info_data['shop']=(!empty($shopApi['body']['shop']->domain)) ? $shopApi['body']['shop'] : ''; $themesApi = $shop->api()->rest('GET', '/admin/themes.json'); if(!empty($themesApi['body']['themes'])){ foreach($themesApi['body']['themes'] as $single_theme){ if($single_theme['role']=='main'){ $shop_info_data['theme']=(!empty($single_theme['id'])) ? $single_theme : ''; } } } $shop_url=''; if(!empty($shop_info_data['shop']->domain) && !empty($shop_info_data['theme']->id)) $shop_url="https://".$shop_info_data['shop']->domain."/admin/themes/".$shop_info_data['theme']->id."/editor?context=apps"; if(!empty($this->shop_charges()['status'])) return view('welcome',['shop_info_data'=>$shop_info_data,'shop_url'=>$shop_url,'db_shop_data'=>$shop] ); else return view('buy-plan'); } /** * * Check Merchant purchase SubscriptionFlow App * * */ public function shop_charges(){ try { $response=array(); //Default message $response['status']=false; $response['data']='Something went wrong, please try again'; $chs = resolve(ChargeHelper::class); $shop = Auth::user(); if(!empty($shop->plan)){ // Get the charge entry from database, set it to the charge helper to use for API calls $charge = $chs->chargeForPlan($shop->plan->getId(), $shop); $chs->useCharge($charge->getReference()); // Get the charge data from Shopify API $chargeData = $chs->retrieve($shop); if($chargeData['status']=='active'){ $response['status']=true; $response['data']='The SubscriptionFlow Plan is now active'; } } } catch(Exception $e) { $response['status']=false; $response['data']='Something went wrong, please try again'; } return $response; } /** * * Step 1 * * Domain Authentication * */ public function sf_domain_authentication(Request $request){ $request->validate([ 'url' =>['required','regex:/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i'], ]); $shop = Auth::user(); $domain = $shop->getDomain()->toNative(); $store_name=''; if(!empty($domain)){ $domain_arr=explode('.',$domain); $store_name=(!empty($domain_arr[0])) ? $domain_arr[0] : ''; } try{ $client = new \GuzzleHttp\Client(['verify' => false]); $response = $client->request('POST',$request->url.'api/v1/integrations/shopify', [ 'form_params' => [ 'type' => 'install', 'store' => $store_name, 'api_key' => env('SHOPIFY_API_KEY'), 'token' => $shop->password, ] ]); $response = $response->getBody()->getContents(); if(!empty($response)) { $response_arr=json_decode($response,true); if(!empty($response_arr['status'])){ $shop->sf_domain=$request->url; $shop->is_domain_active=true; $shop->save(); return response()->json(array('status' => true, 'data'=>'Your store has been successfully connected!!' ), 200); } } } catch(\GuzzleHttp\Exception\RequestException $e){ $error=array(); if($e->hasResponse()){ $response_error=json_decode($e->getResponse()->getBody()->getContents(),true); /** * * Generic error * */ if(!empty($response_error['message'])){ $error['custom_errors'][] = array($response_error['message']); } /** * * Required field error * */ if(!empty($response_error['errors'])){ foreach($response_error['errors'] as $single_error){ if(!empty($single_error[0])) $error['custom_errors'][] = $single_error; } } return response()->json( $error, $e->getResponse()->getStatusCode()); } } catch(Exception $e){ } } /** * * Sync Product API Call * */ public function sf_import_products(Request $request){ $shop = Auth::user(); $domain = $shop->getDomain()->toNative(); try{ $client = new \GuzzleHttp\Client(['verify' => false]); $response = $client->request('POST',$shop->sf_domain.'api/v1/integrations/shopify', [ 'form_params' => [ 'type' => 'syncProducts', 'store' => $domain, // 'api_key' => env('SHOPIFY_API_KEY'), //'token' => $shop->password, ] ]); $response = $response->getBody()->getContents(); if(!empty($response)) { $response_arr=json_decode($response,true); if(!empty($response_arr['status'])){ $shop->is_product_sync=true; $shop->save(); return response()->json(array('status' => true, 'data'=>'The products import job has been queued. Please wait until all product are not synced.' ), 200); } } } catch(\GuzzleHttp\Exception\RequestException $e){ $error=array(); if($e->hasResponse()){ $response_error=json_decode($e->getResponse()->getBody()->getContents(),true); /** * * Generic error * */ if(!empty($response_error['message'])){ $error['custom_errors'][] = array($response_error['message']); } else{ $error['custom_errors'][] = 'Something went wrong, please try again!'; } return response()->json( $error, $e->getResponse()->getStatusCode()); } else{ $error['custom_errors'][] = 'Something went wrong, please try again!'; return response()->json( $error, 422); } } catch(Exception $e){ } } }