I am trying to Create a Shopify Order using Api this is my code :
$arrOrder= array(
"email"=> "foo#example.com",
"fulfillment_status"=> "fulfilled",
"send_receipt"=> true,
"send_fulfillment_receipt"=> true,
"line_items"=> array(
array(
"product_id"=>875744960642,
"variant_id"=> 3558448932592,
"quantity"=> 1
)
),
"customer"=> array(
"id"=> 458297751235
),
"financial_status"=> "pending"
); echo json_encode($arrOrder);
echo "<br />";
$url = "https://AkiKey:Password#Store.myshopify.com/admin/api/2021-01/orders.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($arrOrder));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
echo "<pre>";
print_r($response);
and the response is :
{"errors":{"order":"Required parameter missing or invalid"}}
I think there is some data mismatch that is sent using API Call, according to the documentation this the format to create an order.
So I think your request is something like this one demo code
$arrOrder= [
"order" =>[
"email" => "foo#example.com",
"fulfillment_status" => "fulfilled",
"send_receipt" => true,
"send_fulfillment_receipt" => true,
"line_items" => [
[
"product_id" => 875744960642,
"variant_id" => 3558448932592,
"quantity" => 1
]
],
"customer" => [
"id"=> 458297751235
],
"financial_status"=> "pending"
]
];
Related
if(isset($_POST['add_shopify']))
{
$title = $_POST['title'];
$body = $_POST['body_html'];
$vendor = $_POST['vendor'];
$type = $_POST['product_type'];
$price = $_POST['price'];
$images = $_POST['images'];
$product = array(
'title'=> $title,
'body_html' => $body,
'vendor'=> $vendor,
'product_type'=> $type,
"variants"=>[[
"price"=> $price
]],
"images" => [
[
"src"=> $images
]
]
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL," https://9e54fc.myshopify.com/admin/api/2022-10/products.json?access_token=*********************" );
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HTTPHEADER,array(
'Content-Type' => 'application/json',
));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($product));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
if (curl_errno($curl)) {
$error_msg = curl_error($curl);
}
curl_close($curl);
if (isset($error_msg)) {
echo $error_msg;
}
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
print_r(json_decode($resp));
when i run this code it's return true. but when i am checking admin panel there are no product showing of that name. this code is working properly in postman api. please tell me about it what's wrong with it?
You are missing the "product" key in the $product array, please check the below code that there is a "product" key, and inside that, all the product details are there. You have to pass the data in JSON format, but you are using http_build_query, which Shopify doesn't accept with application/json header, so please use the below code to create a product via PHP curl.
<?php
$productData = [
"product" => [
"title" => "Burton Custom Freestyle 151",
"body_html" => "<strong>Good snowboard!</strong>",
"vendor" => "Burton",
"product_type" => "Snowboard",
"variants" => [
[
"option1" => "Blue",
"option2" => "155"
],
[
"option1" => "Black",
"option2" => "159"
]
],
"options" => [
[
"name" => "Color",
"values" => [
"Blue",
"Black"
]
],
[
"name" => "Size",
"values" => [
"155",
"159"
]
]
]
]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://your-development-store.myshopify.com/admin/api/2022-10/products.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'X-Shopify-Access-Token' => '{access_token}',
'Content-Type' => 'application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($productData));
$response = curl_exec($ch);
curl_close($ch);
I'm integrating the Instamojo payment gateway into a website using core PHP.
I wrote some script that is working well enough on the test.instamojo.com (Which is a test version of their portal) but When I changed the credentials for the live testing (means I have removed test level credentials and put all real-time credentials like their live URL, Private key, Auth Key of our account) It's started showing error in the variable where I want to receive the long URL to redirect for the payment.
The Instamojo response is
{
"success": true,
"payment_request": {
"id": "47a321d9*****************bbb55f64",
"phone": "+9170******55",
"email": "a*******#gmail.com",
"buyer_name": "Aman",
"amount": "100.00",
"purpose": "FIFA",
"expires_at": null,
"status": "Pending",
"send_sms": true,
"send_email": true,
"sms_status": "Pending",
"email_status": "Pending",
"shorturl": null,
"longurl": "https://www.instamojo.com/#EXAMPLE/47a321d95c5c4d7f8e0e7742bbb55f64",
"redirect_url": "http://www.example.com/thankyou.php/",
"webhook": null,
"allow_repeated_payments": false,
"created_at": "2022-09-10T09:16:12.104302Z",
"modified_at": "2022-09-10T09:16:12.104336Z"
}
}
This is my Php Code
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.instamojo.com/api/1.1/payment-requests/');
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("X-Api-Key:*****************************************",
"X-Auth-Token:**************************************"));
$payload = Array(
'purpose' => 'FIFA',
'amount' => '100',
'phone' => '70*****55',
'buyer_name' => 'Aman',
'redirect_url' => 'http://www.example.com/thankyou.php/',
'send_email' => true,
'send_sms' => true,
'email' => 'a******#gmail.com',
'allow_repeated_payments' => false
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
$response = curl_exec($ch);
curl_close($ch);
header('location:'.$response->payment_request->longurl);
?>
I'm getting an error (red line within $ response) at
header('location:'.$response->payment_request->longurl);
The use of json_decode() and traverse to data you want
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.instamojo.com/api/1.1/payment-requests/');
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("X-Api-Key:*****************************************",
"X-Auth-Token:**************************************"));
$payload = Array(
'purpose' => 'FIFA',
'amount' => '100',
'phone' => '70*****55',
'buyer_name' => 'Aman',
'redirect_url' => 'http://www.example.com/thankyou.php/',
'send_email' => true,
'send_sms' => true,
'email' => 'a******#gmail.com',
'allow_repeated_payments' => false
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
$response = curl_exec($ch);
curl_close($ch);
header('location:'.json_decode($response, true)['payment_request']['longurl']);
?>
I want to create product in shopify through api.I tried with below code,but its not working.
<?php
$products_array = array(
"product" => array(
"title" => "Test Product",
"body_html" => "<strong>Description!</strong>",
"vendor" => "DC",
"product_type" => "Test",
"published" => true ,
"variants" => array(
array(
"sku" => "t_009",
"price" => 20.00,
"grams" => 200,
"taxable" => false,
)
)
)
);
$SHOPIFY_API = "https://apikey:password#domainname.myshopify.com/admin/products.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $SHOPIFY_API);
$headers = array( "Authorization: Basic ".base64_encode("apikey:password"),
"Content-Type: application/json",
"charset: utf-8");
curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($products_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec ($curl);
curl_close ($curl);
echo "<pre>";
print_r($response);
echo "</pre>";
?>
It gives response as '{"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}'.any idea?
Please check if there is write permission is set or not for products in your private app.
I have found the issue, it's not about permission, is about the URL of the API you are posting it wrong URL. Here is the right one:
https://{apikey}:{password}#{hostname}/admin/api/{version}/{resource}.json
And here is the code:
<?php
$products_array = array(
"product" => array(
"title" => "New Test Product",
"body_html" => "<strong>Description!</strong>",
"vendor" => "DC",
"product_type" => "Test",
"published" => true ,
"variants" => array(
array(
"sku" => "t_009",
"price" => 20.00,
"grams" => 200,
"taxable" => false,
)
)
)
);
$API_KEY = 'apikey';
$PASSWORD = 'password';
$SHOP_URL = 'domainname.myshopify.com';
$SHOPIFY_API = "https://$API_KEY:$PASSWORD#$SHOP_URL/admin/api/2020-04/products.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $SHOPIFY_API);
$headers = array(
"Authorization: Basic ".base64_encode("$API_KEY:$PASSWORD"),
"Content-Type: application/json",
"charset: utf-8"
);
curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($products_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec ($curl);
curl_close ($curl);
echo "<pre>";
print_r($response);
echo "</pre>";
?>
try "published" => false ,
im trying to create a customer in paymaya using curl in php.
im following this documentation http://developers.paymaya.com.payment-vault.s3-website-ap-southeast-1.amazonaws.com/#card-vault-customers-post
but its not returning the right response(it returns nothing)
<?php
require_once(DIR_VENDOR . 'PayMaya-PHP-SDK-master/sample/autoload.php');
Class Paymaya {
public function paymayaInit(){
PayMayaSDK::getInstance()->initCheckout("pk-nRO7clSfJrojuRmShqRbihKPLdGeCnb9wiIWF8meJE9", "sk-jZK0i8yZ30ph8xQSWlNsF9AMWfGOd3BaxJjQ2CDCCZb", "SANDBOX");
}
public function createCustomer(){
$this->paymayaInit();
// $ch = curl_init("https://pg-sandbox.paymaya.com/payments/v1/customers");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://pg-sandbox.paymaya.com/payments/v1/customers");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Basic c2stOWxSbUZUVjhCSWR4b1hXbTVsaURBbEtGMHlMNGdaendtRFFBbW52eFdPRjo="));
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
$body = array(
"firstName" => "Ysa",
"middleName" => "Cruz",
"lastName" => "Santos",
"birthday" => "1987-10-10",
"sex" => "F",
"contact" => array(
"phone" => "+63(2)1234567890",
"email" => "ysadcsantos#gmail.com"
),
"billingAddress" => array(
"line1" => "9F Robinsons Cybergate 3",
"line2" => "Pioneer Street",
"city" => "Mandaluyong City",
"state" => "Metro Manila",
"zipCode" => "12345",
"countryCode" => "PH"
),
"metadata" => array()
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
$response = curl_exec($ch);
curl_close($ch);
// die(print_r($response));
return json_decode($response);
}
}
here is my class.
does anyone already tried integrating paymaya in php?
also i have to comment the namespace in PayMayaSDK.php to be able to use PayMayaSDK class
Thanks for your credentials posted in question I made few requests and found what was wrong.
First of all I used curl_error($ch) to find out what error was.
It's The requested URL returned error: 400 Bad Request.
Problem is that you set Content-Type to be json, but sending URL encoded query.
Change http_build_query to json_encode
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://pg-sandbox.paymaya.com/payments/v1/customers");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Basic c2stOWxSbUZUVjhCSWR4b1hXbTVsaURBbEtGMHlMNGdaendtRFFBbW52eFdPRjo="));
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
$body = array(
"firstName" => "Ysa",
"middleName" => "Cruz",
"lastName" => "Santos",
"birthday" => "1987-10-10",
"sex" => "F",
"contact" => array(
"phone" => "+63(2)1234567890",
"email" => "ysadcsantos#gmail.com"
),
"billingAddress" => array(
"line1" => "9F Robinsons Cybergate 3",
"line2" => "Pioneer Street",
"city" => "Mandaluyong City",
"state" => "Metro Manila",
"zipCode" => "12345",
"countryCode" => "PH"
),
"metadata" => array()
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
$response = curl_exec($ch);
echo __FILE__."#".__LINE__."<pre>";
var_dump($response, curl_error($ch));
echo "</pre>";
curl_close($ch);
I'm having trouble just sending my first API call to PayPal.
I'm hoping its just simple syntax that I got wrong.
The error I'm getting:
"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($ch);
if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
$sAccessToken = $json->access_token;
$sTokenType = $json->token_type;
$sAppID = $json->app_id;
print_r($json);
}
curl_close($ch);
$ch2 = curl_init();
//curl -v https://api.sandbox.paypal.com/v1/payments/payment
curl_setopt($ch2, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
//-H "Content-Type:application/json" \
//-H "Authorization: Bearer <Access-Token>" \
curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
"Content-Type:application/json",
"Authorization: Bearer ".$sAccessToken,
));
$payer = array(
'payment_method' => 'credit_card',
'funding_instruments' => array(
'credit_card' => array(
'number' => "4417119669820331",
"type" => "visa",
"expire_month" => "11",
"expire_year" => "2018",
"cvv2" => "874",
"first_name" => "Betsy",
"last_name" => "Buyer",
"billing_address" => array(
"line1" => "111 First Street",
"city" => "Saratoga",
"state" => "CA",
"postal_code" => "95070",
"country_code" => "US"
),
),
),
);
$params = array(
'intent' => 'sale',
'payer' => $payer,
'transactions' => array(array(
"amount" =>"7.47",
"currency" =>"USD",
"details" => array(
"subtotal"=>"7.41",
"tax"=>"0.03",
"shipping"=>"0.03"
),
"description" => "This is the payment transaction description."
)
));
curl_setopt($ch2, CURLOPT_POST, true );
curl_setopt($ch2, CURLOPT_POSTFIELDS, json_encode( $params ) );
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true );
$response2 = curl_exec( $ch2 );
$errrors2 = curl_error($ch2);
print_r($response2." / ");
print_r($errrors2);
curl_close($ch2);