Created
August 23, 2017 18:31
-
-
Save ShaunaGordon/24da74ac66d0d2d8259fe52df0f57ef3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function verifyRequest(Request $request, $secret) { | |
// Per the Shopify docs: | |
// Everything except hmac and signature... | |
$signature = $request->except(['hmac', 'signature']); | |
// Sorted lexilogically... | |
ksort($signature); | |
// Special characters replaced... | |
foreach ($signature as $k => $val) { | |
$k = str_replace('%', '%25', $k); | |
$k = str_replace('&', '%26', $k); | |
$k = str_replace('=', '%3D', $k); | |
$val = str_replace('%', '%25', $val); | |
$val = str_replace('&', '%26', $val); | |
$signature[$k] = $val; | |
} | |
// Hashed per hmac standards, using sha256 and the shared secret | |
$test = hash_hmac('sha256', http_build_query($signature), $secret); | |
// Verified when equal | |
return $request->input('hmac') === $test; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function verifyRequest($request, $secret) { | |
// Per the Shopify docs: | |
// Everything except hmac and signature... | |
$hmac = $request['hmac']; | |
unset($request['hmac']); | |
unset($request['signature']); | |
// Sorted lexilogically... | |
ksort($request); | |
// Special characters replaced... | |
foreach ($request as $k => $val) { | |
$k = str_replace('%', '%25', $k); | |
$k = str_replace('&', '%26', $k); | |
$k = str_replace('=', '%3D', $k); | |
$val = str_replace('%', '%25', $val); | |
$val = str_replace('&', '%26', $val); | |
$request[$k] = $val; | |
} | |
// Hashed per hmac standards, using sha256 and the shared secret | |
$test = hash_hmac('sha256', http_build_query($request), $secret); | |
// Verified when equal | |
return $hmac === $test; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment