<?php
/*
File: AppController.php
Author: Ivan Kähm
Date: 15.01.2020 13:45
Project: friendscore.web.a22.localhost
*/
namespace App\Controller;
use Pimcore\Controller\FrontendController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Osiset\BasicShopifyAPI;
class AppController extends FrontendController
{
/**
* @Template()
* @param Request $request
*/
public function pageAction(Request $request)
{
}
public function shopifyAction(Request $request)
{
$shop = $request->get("shop");
if ($shop) {
// install app
$api_key = "1ec6b93f670b55f43dce7a7a430edb9f";
$scopes = "read_content,write_content,read_themes,write_themes,read_products,write_products,read_product_listings";
$scopes .= ",read_customers, write_customers,read_orders, write_orders, read_draft_orders, write_draft_orders,read_inventory, write_inventory,read_locations,read_script_tags, write_script_tags";
$scopes .= ",read_checkouts, write_checkouts, read_price_rules, write_price_rules,read_translations, write_translations,read_locales, write_locales";
$redirect_uri = "https://friendscore.xxii.de/de/shopify/gentoken";
// Build install/approval URL to redirect to
$install_url = "https://" . $shop . "/admin/oauth/authorize?client_id=" . $api_key . "&scope=" . $scopes . "&redirect_uri=" . urlencode($redirect_uri);
// Redirect
header("Location: " . $install_url);
die();
} else {
$token = "b3824b7bd256d475724240574739230a";
$apiKey = "1ec6b93f670b55f43dce7a7a430edb9f";
$pw = "74e7020f910b7b12d7940dd7c32d9466";
$sharedSrt = "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";
$credential = new \Slince\Shopify\PrivateAppCredential('daed83883b565e4e3de21559ab68c513', '74e7020f910b7b12d7940dd7c32d9466', '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');
$client = new \Slince\Shopify\Client($credential, 'coreshopxxii.myshopify.com', [
'metaCacheDir' => '/var/www/vhosts/xxii.de/https://friendscore.xxii.de/web/var/tmp' // Metadata cache dir, required
]);
//$data = $client->getLastResponse();
$api = new BasicShopifyAPI();
$api->setVersion('2020-03'); // "YYYY-MM" or "unstable"
$api->setShop("coreshopxxii.myshopify.com");
$api->setAccessToken("b3824b7bd256d475724240574739230a");
$data = $api->rest("GET", "/admin/api/2020-01/products.json", $params = null, $headers = [], $sync = true);
/*
$order = array (
"email" => "foo@example.com",
"fulfillment_status" => "unfulfilled",
"line_items" => [
[
"variant_id" => 32907957567628,
"quantity" => 2
]
]
);
$client->getOrderManager()->create($order);
$orders = $client->getOrderManager()->getClient();
*/
echo "<pre>";
var_dump($data->body);
}
}
public function shopifyGenTokenAction(Request $request)
{
// Set variables for our request
$api_key = "1ec6b93f670b55f43dce7a7a430edb9f";
$shared_secret = "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";
$params = $_GET; // Retrieve all request parameters
$hmac = $_GET['hmac']; // Retrieve HMAC request parameter
$params = array_diff_key($params, array('hmac' => '')); // Remove hmac from params
ksort($params); // Sort params lexographically
$computed_hmac = hash_hmac('sha256', http_build_query($params), $shared_secret);
// Use hmac data to check that the response is from Shopify or not
if (hash_equals($hmac, $computed_hmac)) {
// Set variables for our request
$query = array(
"client_id" => $api_key, // Your API key
"client_secret" => $shared_secret, // Your app credentials (secret key)
"code" => $params['code'] // Grab the access key from the URL
);
// Generate access token URL
$access_token_url = "https://" . $params['shop'] . "/admin/oauth/access_token";
// Configure curl client and execute request
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $access_token_url);
curl_setopt($ch, CURLOPT_POST, count($query));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));
$result = curl_exec($ch);
curl_close($ch);
// Store the access token
$result = json_decode($result, true);
$access_token = $result['access_token'];
// Show the access token (don't do this in production!)
return new Response($access_token);
//return $this->json(array('key' => 'value'));
} else {
// Someone is trying to be shady!
die('This request is NOT from Shopify!');
}
}
/**
* @Template
* @param Request $request
* @return array
*/
public function navAction(Request $request)
{
return [
'siteconfig' => $request->get('siteconfig'),
'documentNav' => $request->get('documentNav')
];
}
/**
* @Template
* @param Request $request
* @return array
*/
public function footerAction(Request $request)
{
return [
'websiteData' => $request->get('websiteData'),
'documentFooter' => $request->get('documentFooter')
];
}
/**
* @Template()
* @param Request $request
*/
public function emailAction(Request $request)
{
return [
'gender' => $request->attributes->get('gender'),
'name' => $request->attributes->get('name'),
'lastname' => $request->attributes->get('lastname'),
'email' => $request->attributes->get('email'),
'company' => $request->attributes->get('company'),
'address' => $request->attributes->get('address'),
'zip' => $request->attributes->get('zip'),
'city' => $request->attributes->get('city'),
'state' => $request->attributes->get('state'),
'message' => $request->attributes->get('message')
];
}
}