Paypal Single Payout - 'Json request malformed' - php

I am trying to proceed single payout with Paypal Sandbox. The error I am getting is:
'MALFORMED_REQUEST_ERROR' - 'Json request malformed.'
After receiving the token successfully, I setup the single payment details array and encode json, and then use curl to post it, but no luck. The code goes like:
if ($token) {
$ch = curl_init();
$data = [
'sender_batch_header' => [
'email_subject' => "You have a payment",
'sender_batch_id' => '184328423'
],
'items' => [
'recipient_type' => "EMAIL",
'amount' => [
'value' => 12.00,
'currency' => "USD"
],
'receiver' => 'test123test#hotmail.com',
'note' => 'Hello World',
'sender_item_id' => "123"
],
];
$headers = [
'Content-Type:application/json',
'Authorization:Bearer ' . $token,
];
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payouts?sync_mode=true");
// curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
print_r($json);
}
curl_close($ch);
}

It took some time for me to notice it. The answer:
$data = [
'sender_batch_header' => [
'email_subject' => "You have a payment",
'sender_batch_id' => $randId
],
'items' => [
[
'recipient_type' => "EMAIL",
'amount' => [
'value' => "12.00",
'currency' => "USD"
],
'receiver' => 'test123test#hotmail.com',
'note' => 'Hello World',
'sender_item_id' => "A123"
],
],
];

Related

Error when sending firebase notification to topics with curl and api v1

in my laravel app I'm using curl to send a notification to all users suscribed to a topic, however when I send the notification I get this error:
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"to\": Cannot find field."
This is how I send topic notif with curl:
public function sendTopic($topic,$title,$body, $data , $type, $image='')
{
$client = new Client();
$url = 'https://fcm.googleapis.com/v1/projects/wooloveapp-dda64/messages:send';
$fields =
[
'message' =>
[
"to" => $topic,
"notification" =>
[
"title" => $title,
"body" => $body,
],
"data" => [ "data" => json_encode($data) ],
"android" =>
[
"notification" =>
[
"sound" => "default",
"title" => $title,
"body" => $body,
'tag' => $topic,
"channel_id" => "500",
],
"priority" => "high",
"ttl" => "86400s"
//"badge" => 1
],
"apns" =>
[
"payload" =>
[
"aps" => [ "sound" => "default" ]
],
"headers" => [
"apns-priority" => "5",
"content_available" => "1"
],
],
"webpush"=>[
"headers"=>[
"Urgency"=> "high",
//"image" => "https://wooloveapp.com/img/misc/logo-02.jpg"
]
],
]
];
$headers =
[
'Authorization: Bearer ' .$this->getGoogleAccessToken(),
'Accept:application/json',
'Content-Length:'.strlen(json_encode($fields)),
'Content-Type:application/json',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
$result =
[
'result' => $result,
//'statusCode' => $statusCode
];
return $result;
}
You have to use topic instead of to if you're sending to a topic.

API request works with cURL but not with Guzzle?

I am trying to get this request working, it is my first time working with Guzzle and i dont know what i'm doing wrong here. The request works with cURL but it does not work with Guzzle:
Guzzle:
$res = $client->request('POST', 'https://api.bol.com/retailer/offers', [
'headers' => [
'Authorization' => 'Bearer ' . $jwt,
'Accept' => 'application/vnd.retailer.v5+json',
'content-type' => 'application/vnd.retailer.v5+json'
],
'body' => json_encode(array(
'ean' => (string)$item->ean_number,
'condition' => [
'name' => 'NEW',
],
'reference' => 'NS-'.(string)$item->ean_number,
'unknownProductTitle' => (string)$item->name,
'pricing' => [
'bundlePrices' =>[
'quantity' => 1,
'unitprice' => round($item->default_price->price * 1.21, 2)
],
],
'stock' => [
'amount' => (int)$item->stock,
'managedByRetailer' => false,
],
'fulfilment' => [
'method' => 'FBR',
'deliveryCode' => '2-3d',
]
))
]);
cURL:
$productjson[] = array(
'ean' => $data[4],
'condition' => array('name' => 'NEW', 'category' => 'NEW'),
'onHoldByRetailer' => 'false',
'reference' => "dy".$data[4],
'unknownProductTitle' => $data[1],
'pricing' => array('bundlePrices' => array('quantity' => 1, 'unitPrice' => $data[12])),
'stock'=> array('amount' => $data[13], 'managedByRetailer' => false),
'fulfilment' => array('method' => 'FBR', 'deliveryCode' => '2-3d')
);
$productjson = json_encode($productjson[0], JSON_FORCE_OBJECT);
$response = callAPI('POST', $bearer, 'https://api.bol.com/retailer/offers', $productjson);
function callAPI($method, $bearer, $url, $data, $accept = 'application/vnd.retailer.v4+json', $ignoreheaders = false){
$curl = curl_init();
if(!$ignoreheaders){
curl_setopt($curl, CURLOPT_HEADER, true);
}
switch ($method){
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "DELETE":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: '.$accept, 'Content-Type: '.$accept,
'Authorization: Bearer '.$bearer));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// EXECUTE:
$result = curl_exec($curl);
if(!$ignoreheaders){
if (strpos($result, 'JWT') !== false) {
$responses[] = 'Epired JWT:'.$result;
$bearer = callBearer('POST', 'https://login.bol.com/token?grant_type=client_credentials', NULL);
$result = curl_exec($curl);
}
if(strpos($result, '504') !== false){
$responses[] = '504 error:'.$result;
sleep(1.1);
$bearer = callBearer('POST', 'https://login.bol.com/token?grant_type=client_credentials', NULL);
$result = curl_exec($curl);
}
if(strpos($result, 'FAILURE') !== false){
$responses[] = 'FAILURE:'.$result;
sleep(1.1);
$bearer = callBearer('POST', 'https://login.bol.com/token?grant_type=client_credentials', NULL);
$result = curl_exec($curl);
}
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);
$result = json_decode($body);
return $result;
}
It's probably something really stupid, but im basically just asking: how do i execute the working cURL request with guzzle? Because my guzzle request returns a 400 bad request error.
Trying changing body to json and pass array instead of json encoded string, Guzzle will take care of converting this array to json internally
$res = $client->request('POST', 'https://api.bol.com/retailer/offers', [
'headers' => [
'Authorization' => 'Bearer ' . $jwt,
'Accept' => 'application/vnd.retailer.v5+json',
'content-type' => 'application/vnd.retailer.v5+json'
],
'json' => array(
'ean' => (string)$item->ean_number,
'condition' => [
'name' => 'NEW',
],
'reference' => 'NS-'.(string)$item->ean_number,
'unknownProductTitle' => (string)$item->name,
'pricing' => [
'bundlePrices' =>[
'quantity' => 1,
'unitprice' => round($item->default_price->price * 1.21, 2)
],
],
'stock' => [
'amount' => (int)$item->stock,
'managedByRetailer' => false,
],
'fulfilment' => [
'method' => 'FBR',
'deliveryCode' => '2-3d',
]
)
]);

JSON PHP Output Logic

I am using this script in PHP to capture google products through ZenSerp. I can get an output with all the terms.
Example;
Title
Description
Price
Image
URL Link
Is there a way to print
1.price
2.title
3.description
on another page?
thanks.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$data = [
"q" => "Tools",
"tbm" => "shop",
"device" => "desktop",
"location" => "Manhattan,New York,United States",
];
curl_setopt($ch, CURLOPT_URL, "https://app.zenserp.com/api/v2/search?" . http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"apikey: XXXX",
));
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response);
echo '<pre>' . var_export($json, true) . '</pre>';
?>
Output:
(object) array(
'title' => 'Craftsman 320-Piece Mechanic S Tool Set',
'link' => '/aclk?sa=L&ai=DChcSEwjn5sb54NDtAhXHfysKHWT9BIIYABBXGgJzZg&sig=AOD64_1kzP9frQqO7uULjK4LGcDJkpuuGA&ctype=46&q=&ved=0ahUKEwjP9cL54NDtAhUBjeYKHaqqBzgQ_-QECL8H&adurl=',
'description' => 'Mechanic ยท CRAFTSMAN',
'price' => '$149.99',
'source' => 'Sears',
'stars' => 4.5999999999999996447286321199499070644378662109375,
'reviews' => 1084,
'thumbnail' => 'data:image/jpeg;base64,/',
'extensions' =>
array (
),
'product_id' => '13276029442981215243',
'price_parsed' =>
(object) array(
'currency' => 'USD',
'value' => 149.990000000000009094947017729282379150390625,

Android Push Notification Through API

I am using one signal API For push notification. And I want to know how can I send button with value(Confirm = 1, Deny =0) so that I can post it from the notification screen. I am not able to send action button through API t the app. I want to post the value of the action button in the database. Please help
This is my code.
function sendMessage(){
$heading=array(
"en" => 'TEST'
);
$content = array(
"en" => 'TEST.'
);
$hashes_array = array();
array_push($hashes_array, array(
"id" => "1",
"text" => "Confirm",
"icon" => "http://i.imgur.com/N8SN8ZS.png",
"url" => "https://yoursite.com"
));
array_push($hashes_array, array(
"id" => "0",
"text" => "Deny",
"icon" => "http://i.imgur.com/N8SN8ZS.png",
"url" => "https://yoursite.com"
));
$fields = array(
'app_id' => "xxxxxxx-xxxxxxx-45b8-97d4-02982b59c14f",
'include_player_ids' => array($GLOBALS['one_signal_id']),
'data' => array("Status" => 200),
'small_icon' => 'https://lh3.googleusercontent.com/TzYz9McRVy8fD_WnpKiCK5anw20So6eyPR9ti-LwTd_QIer8BpAg8cMkRoO4sUv2xCDw=w300-rw',
'large_icon' => 'https://lh3.googleusercontent.com/TzYz9McRVy8fD_WnpKiCK5anw20So6eyPR9ti-LwTd_QIer8BpAg8cMkRoO4sUv2xCDw',
'headings' => $heading,
'contents' => $content,
'web_buttons' => $hashes_array
);
$fields = json_encode($fields);
// print("\nJSON sent:\n");
// print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic xxxxxxxxxxxxxxxxxxxxxx5NjktMjRkZWM0ZjFhMjZl'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage();

Accept a PayPal payment with invalid response

Alright, i have searched this and many more forums, no solution for me.
I have this code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $this->auth['access_token']
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
'intent' => 'sale',
'redirect_urls' => array(
'return_url' => 'http://andershagbard.wep.dk/goingpro/',
'cancel_url' => 'http://andershagbard.wep.dk/goingpro/'
),
'payer' => array (
'payment_method' => 'paypal'
),
'transactions' => array(
'amount' => array(
'total' => '5.00',
'currency' => 'USD'
),
'description' => 'This is the payment transaction description.'
)
)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
And it returns
(
[name] => MALFORMED_REQUEST
[message] => Incoming JSON request does not map to API request
[information_link] => https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST
[debug_id] => 27f7487f9c07f

Categories