Last active
January 11, 2023 17:18
-
-
Save snez/fe4d44ec4729b9f7d6870e50fd8e52b2 to your computer and use it in GitHub Desktop.
How to use Magento 2 REST API using the official Stripe module
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
<?php | |
function getAdminToken() | |
{ | |
$url = 'https://example.com/rest/default/V1/integration/admin/token'; | |
$data = [ | |
"username" => "user", | |
"password" => "user_password" | |
]; | |
$data_string = json_encode($data); | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($data_string) | |
)); | |
$response = curl_exec($curl); | |
if ($response) | |
{ | |
curl_close($curl); | |
echo "Got token $response\n"; | |
return $response; | |
} | |
else | |
{ | |
echo 'Curl error: ' . curl_error($curl); | |
return false; | |
} | |
} | |
function createCustomer($token) | |
{ | |
$url = 'https://example.com/rest/default/V1/customers'; | |
$data = [ | |
"customer" => [ | |
"email" => "[email protected]", | |
"firstname" => "Jane", | |
"lastname" => "Doe", | |
"addresses" => [[ | |
"defaultShipping" => true, | |
"defaultBilling" => true, | |
"firstname" => "Jane", | |
"lastname" => "Doe", | |
"region" => [ | |
"regionCode" => "NY", | |
"region" => "New York", | |
"regionId" => 43 | |
], | |
"postcode" => "10755", | |
"street" => ["123 Oak Ave"], | |
"city" => "Purchase", | |
"telephone" => "512-555-1111", | |
"countryId" => "US" | |
]] | |
], | |
"password" => "Password1" | |
]; | |
$data_string = json_encode($data); | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Authorization Bearer: ' . $token | |
)); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
echo "Got customer $response\n"; | |
return json_decode($response, true); | |
} | |
function getCustomerToken() | |
{ | |
$url = 'https://example.com/rest/default/V1/integration/customer/token'; | |
$data = [ | |
"username" => "[email protected]", | |
"password" => "Password1" | |
]; | |
$data_string = json_encode($data); | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
)); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
echo "Got customer token $response\n"; | |
return json_decode($response, true); | |
} | |
function createQuote($customerToken) | |
{ | |
$url = 'https://example.com/rest/default/V1/carts/mine'; | |
$data = ''; | |
$data_string = json_encode($data); | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, ''); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Authorization: Bearer ' . $customerToken | |
)); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
echo "Got quote $response\n"; | |
return json_decode($response, true); | |
} | |
function addToCart($customerToken, $quoteId) | |
{ | |
$url = 'https://example.com/rest/default/V1/carts/mine/items'; | |
$data = [ | |
"cartItem" => [ | |
"sku" => "24-WB04", | |
"qty" => 1, | |
"quote_id" => "$quoteId" | |
] | |
]; | |
$data_string = json_encode($data); | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Authorization: Bearer ' . $customerToken | |
)); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
echo "Added to cart $response\n"; | |
return json_decode($response, true); | |
} | |
function estimateShipping($customerToken) | |
{ | |
$url = 'https://example.com/rest/default/V1/carts/mine/estimate-shipping-methods'; | |
$data = [ | |
"address" => [ | |
"region" => "New York", | |
"region_id" => 43, | |
"region_code" => "NY", | |
"country_id" => "US", | |
"street" => [ | |
"123 Oak Ave" | |
], | |
"postcode" => "10577", | |
"city" => "Purchase", | |
"firstname" => "Jane", | |
"lastname" => "Doe", | |
"customer_id" => 4, | |
"email" => "[email protected]", | |
"telephone" => "(512) 555-1111", | |
"same_as_billing" => 1 | |
] | |
]; | |
$data_string = json_encode($data); | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Authorization: Bearer ' . $customerToken | |
)); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
echo "Estimated shipping $response\n"; | |
return json_decode($response, true); | |
} | |
function setShippingAndBilling($customerToken) | |
{ | |
$url = 'https://example.com/rest/default/V1/carts/mine/shipping-information'; | |
$data = [ | |
"addressInformation" => [ | |
"shipping_address" => [ | |
"region" => "New York", | |
"region_id" => 43, | |
"region_code" => "NY", | |
"country_id" => "US", | |
"street" => [ | |
"123 Oak Ave" | |
], | |
"postcode" => "10577", | |
"city" => "Purchase", | |
"firstname" => "Jane", | |
"lastname" => "Doe", | |
"email" => "[email protected]", | |
"telephone" => "512-555-1111" | |
], | |
"billing_address" => [ | |
"region" => "New York", | |
"region_id" => 43, | |
"region_code" => "NY", | |
"country_id" => "US", | |
"street" => [ | |
"123 Oak Ave" | |
], | |
"postcode" => "10577", | |
"city" => "Purchase", | |
"firstname" => "Jane", | |
"lastname" => "Doe", | |
"email" => "[email protected]", | |
"telephone" => "512-555-1111" | |
], | |
"shipping_carrier_code" => "tablerate", | |
"shipping_method_code" => "bestway" | |
] | |
]; | |
$data_string = json_encode($data); | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Authorization: Bearer ' . $customerToken | |
)); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
echo "Set billing and shipping $response\n"; | |
return json_decode($response, true); | |
} | |
function sendPaymentInformation($customerToken) | |
{ | |
$url = 'https://example.com/rest/default/V1/carts/mine/payment-information'; | |
$data = [ | |
"paymentMethod" => [ | |
"method" => "stripe_payments", | |
"additional_data" => [ | |
"cc_stripejs_token" => "pm_card_visa" // Use pm_card_threeDSecureRequired for 3DS authentication | |
] | |
], | |
"billing_address" => [ | |
"email" => "[email protected]", | |
"region" => "New York", | |
"region_id" => 43, | |
"region_code" => "NY", | |
"country_id" => "US", | |
"street" => ["123 Oak Ave"], | |
"postcode" => "10577", | |
"city" => "Purchase", | |
"telephone" => "512-555-1111", | |
"firstname" => "Jane", | |
"lastname" => "Doe" | |
] | |
]; | |
$data_string = json_encode($data); | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Authorization: Bearer ' . $customerToken | |
)); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
echo "Set billing and shipping $response\n"; | |
return json_decode($response, true); | |
} | |
$token = getAdminToken(); | |
if ($token === FALSE) { die('Error getting admin token'); } | |
$customer = createCustomer($token); | |
$customerToken = getCustomerToken(); | |
$quoteId = createQuote($customerToken); | |
$cart = addToCart($customerToken, $quoteId); | |
$shipping = estimateShipping($customerToken); | |
$estimate = setShippingAndBilling($customerToken); | |
$payment = sendPaymentInformation($customerToken); | |
print_r($payment); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment