PayPal Orders API schema - php

I am setting up a payment integration with PayPal Checkout Buttons and Paypal Orders API V2. Payments are going throw, but now I want to include items in my Order request, but I getting:
As PayPal docs say I have to pass array (contains the item object) Link to Orders API purchase_unit object Link to Orders API item object. But I'm still getting this error.
This is my request:
public function createOrder($value, $order, $products, $currency = 'EUR')
{
return $this->makeRequest(
'POST',
'/v2/checkout/orders',
[],
[
'intent' => 'CAPTURE',
'payer' => [
'name' => [
'given_name' => $order->billing_firstname,
'surname' => $order->billing_lastname
],
'email_address' => $order->email_address,
'address' => [
'address_line_1' => $order->billing_street_address,
'admin_area_2' => $order->billing_city,
'postal_code' => $order->billing_postcode,
'country_code' => $order->billing_country_code
],
],
'purchase_units' => [
0 => [
'amount' => [
'currency_code' => $currency,
'value' => $value,
],
'description' => 'Order: ' . $order->order_serial_number,
'items' => [
0 => [
'name' => 'Item1',
'unit_amount' => 100,
'quantity' => 1
],
],
],
],
'application_context' => [
'brand_name' => config('app.name'),
'shipping_preference' => 'NO_SHIPPING',
'user_action' => 'PAY_NOW',
'return_url' => route('approval.paypal', $order->id_order),
'cancel_url' => route('payment.cancelled', $order->id_order),
]
],
[],
$isJsonRequest = true
);
}
I'm using: Laravel and Guzzle/HTTP to perform payment requests.
As I say, payments are going throw, but when I try to add Items to Order, I'm getting this error.

You haven't posted the full (rest of the) error response that details the problem, so there may be another issue we can't see, but one thing that's missing is the amount.breakdown.item_total subtotal, which is required when passing an items array with amounts: https://developer.paypal.com/docs/api/orders/v2/#definition-amount_breakdown
A simple example in the API's JSON format, which you can adapt to PHP array syntax:
// based on example array from https://developer.paypal.com/docs/checkout/reference/server-integration/set-up-transaction/
"purchase_units": [{
"amount": {
"value": "17.49",
"currency_code": "USD",
"breakdown": {
"item_total": {
"currency_code": "USD",
"value": "17.49"
},
}
},
"items": [
{
"unit_amount": {
"currency_code": "USD",
"value": "17.49"
},
"quantity": "1",
"name": "item 1",
},
],
}
]

Related

Uploading attachment into deals in HubSpot using API

I am having some issues with uploading attachment into deals in HubSpot.
Here is my code:
$note_details = array(
"engagement" => array(
"active" => true,
"type" => "NOTE",
"timestamp" => 5456563646
),
"associations" => array(
"dealIds" => array( '4875586081' ),
"attachments" => array(
"id" => array ("44830500634")
),
),
"metadata" => array(
"body" => "TESTING-222",
)
);
This code creates an engagement or Notes but the attachment is not there!
and here is the JSON structure that HubSpot uses:
{
"engagement": {
"active": true,
"ownerId": 1,
"type": "NOTE",
"timestamp": 1409172644778
},
"associations": {
"contactIds": [2],
"companyIds": [ ],
"dealIds": [ ],
"ownerIds": [ ],
"ticketIds":[ ]
},
"attachments": [
{
"id": 4241968539
}
],
"metadata": {
"body": "note body"
}
}
Any idea what am I missing?

PHP ElasticSearch compound query with has_child

When I query Elasticsearch for products from a specific manufacturer, this works:
$params = ['index' => 'products',
'type' => 'product',
'body' => ['query' =>
['match' => ['manufacturers_id' => $query],],
],
];
But when I also want to add on a condition that the product comes in color Silver, which I have added as a child record to the product record, I get a syntax error:
$params = ['index' => 'products',
'type' => 'product',
'body' => ['query' =>
['match' => ['manufacturers_id' => $query],],
['query' =>
['has_child' =>
['type' => 'attributes',
['query' =>
['color' => 'Silver'],],
],
],
],
],
];
The error is
{
"error": {
"col": 49,
"line": 1,
"reason": "Unknown key for a START_OBJECT in [0].",
"root_cause": [
{
"col": 49,
"line": 1,
"reason": "Unknown key for a START_OBJECT in [0].",
"type": "parsing_exception"
}
],
"type": "parsing_exception"
},
"status": 400
}
Also tried
$params = ['index' => 'products',
'type' => 'product',
'body' => ["query"=> [
"match"=> [
"manufacturers_id"=> [11]
],
"has_child"=> [
"type"=> "attributes",
"query"=> [
"match"=> [
"color"=> "silver"
],
],
],
],
],
];
I get "Can't get text on a START_ARRAY at 1:39."
Try this:
"query"=> [
"match"=> [
"manufacturers_id"=> [1,2,3]
],
"has_child"=> [
"type"=> "attributes",
"query"=> [
"match"=> [
"color"=> "silver"
]
]
]
]
I also recommend Sense, it's a plugin for Chrome browser which helps writing ES queries.
See the screenshot
Finally got this to work. Big thanks to #pawle for his suggestion of Sense, which really helped.
$params = ['index' => 'products',
'type' => 'product',
'body' =>
[
"query" => [
"bool" => [
"must" => [[
"has_child" => [
"type" => "attributes",
"query" => [
"match" => [
"attributes_value" => "silver"
]
]
]
],
[
"match" => [
"manufacturers_id" => 4
]
]
]
]
]
],
];

Can't have a term and geo_distance_range in the same must filter

I have some data and I am trying to get all the results that have a certain month and are less than 1.6km from the target point. I am using the PHP client so my query looks like this.
$crimeSearch = [
'size' => 0,
'query' => [
'filtered' => [
'filter' => [
'bool' => [
'must' => [
'term' => [
'month' => $date,
],
'geo_distance_range' => [
'location' => [
'lat' => $lat,
'lon' => $lng,
],
'lt' => '1.6km',
],
],
],
],
],
],
'aggs' => [
'group_by_category' => [
'terms' => [
'field' => 'category',
],
],
],
];
I am currently seeing the following error:
query_parsing_exception: No query registered for [location]
My mapping looks like this:
"properties": {
"location": {
"type": "geo_point"
},
"category": {
"type": "string",
"index": "not_analyzed"
},
"month": {
"type": "string",
"index": "not_analyzed"
}
}
Now if I comment out either the term value or the geo_distance_range value from the must array then I get the correct results back. This error only occurs when they are both present.
Can anyone see what I wrong with my query?
I have tried moving the geo_distance_range into its own must block but this seems to bring back all results that match either of the the must filters and not them both.
If you need any more information please ask!
Thank you.
I do not know anything about PHP but If I try to convert equivalent ES json query then this might work. I guess you need to put every must clause in array like this
[
'size' => 0,
'query' => [
'filtered' => [
'filter' => [
'bool' => [
'must' => [
[
'term' => [
'month' => $date,
]
],
[
'geo_distance_range' => [
'location' => [
'lat' => $lat,
'lon' => $lng,
],
'lt' => '1.6km',
],
],
],
],
],
],
],
'aggs' => [
'group_by_category' => [
'terms' => [
'field' => 'category',
],
],
],
];
This is equivalent to
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"month": "June"
}
},
{
"geo_distance_range": {
"lt": "1.6km",
"location": {
"lat": 37.9174,
"lon": -122.305
}
}
}
]
}
}
}
}
}
Does this work?

Whats wrong with the Elastic Search PHP search query?

I've been trying to get the following JSON working in PHP Arrays but I don't seem to get any hits.
The JSON is as follows:
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "search"
}
}
}
},
"fields": [
"body",
"title",
"postDate",
"user",
"name"
],
"from": 0,
"size": 50,
"sort": {
"_score": {
"order": "asc"
}
},
"explain": true
}
And the PHP I managed to create is like this:
$docs = $client->search([
'index' => 'blog',
'type' => 'posts',
'body' => [
'query' => [
'filtered' => [
'query' => [
'query_string' => [
'query' => $search_query
]
]
]
],
'fields' => [
'body',
'title',
'postDate',
'user',
'name'
],
'from' => 0,
'size' => 50,
'sort' => [
'_score' => [
'order' => 'asc'
]
]
]
]);
It returns an response but no hits, even though it should (and it does in case of the JSON request)
What is going on here?
The post type wasn't required at all... I somehow thought it was. I used a tool called ElasticHQ to generate the JSON and i didn't realize it wasnt using Posts as a type.
Changed it to
$docs = $client->search([
'index' => 'blog',
'body' => [
'query' => [
'filtered' => [
'query' => [
'query_string' => [
'query' => $search_query
]
]
]
],
'fields' => [
'body',
'title',
'postDate',
'user',
'name'
],
'from' => 0,
'size' => 50,
'sort' => [
'_score' => [
'order' => 'asc'
]
]
]
]);

Array structure in datatables

How to obtain the following structure after the php json_encode.
It is possible?
{
"data": [
{
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800"
},
{
"name": "Garrett Winters",
"position": "Accountant",
"salary": "$170,750"
}
]
}
How must look arrays?
Although formally array keys can only be integer you could simple use:
array( 'data' =>
array(
array( 'name' => 'tiger nixon', 'position' => 'system architect', 'salary' => '$320,800' ),
array( 'name' => 'Garrett Winters', 'position' => 'Accountant', 'salary' => '$170,750' )
)
);

Categories