Related
I try to implement paypal express checkout in a way that users are able to manipulate their order after they logged in with Paypal and when they are done, click "bux now" and the actual payment is capured.
To do so, I created the order with paypal before with intend "Authorize". The user logs into paypal and comes back to my shop to manage the order. When done, I want to use paypals updateOrder Call to apply the users changes. But It always bounces with error REFERENCE_ID_NOT_FOUND.
Here is the documentation:
https://developer.paypal.com/api/orders/v2/#orders_patch
Here is, what I send to Paypal.
[{
"op": "replace",
"path": "\/purchase_units\/#reference_id=='default'",
"value": [{
"reference_id": "2295037",
"description": "Your order at Shop",
"custom_id": "Shop Id 2295037",
"soft_descriptor": "Shopname",
"invoice_id": "2295037",
"amount": {
"currency_code": "EUR",
"value": 59.98,
"breakdown": {
"item_total": {
"currency_code": "EUR",
"value": 50.4
},
"shipping": {
"currency_code": "EUR",
"value": 0
},
"discount": {
"currency_code": "EUR",
"value": 0
},
"tax_total": {
"currency_code": "EUR",
"value": 9.58
}
}
},
"items": [{
"name": "Product 1",
"description": "Product Descr. 1",
"sku": "1019879",
"unit_amount": {
"currency_code": "EUR",
"value": 16.8
},
"tax": {
"currency_code": "EUR",
"value": 3.19
},
"quantity": "1",
"category": "PHYSICAL_GOODS"
}, {
"name": "Product 2",
"description": "Product Descr. 2",
"sku": "1024593",
"unit_amount": {
"currency_code": "EUR",
"value": 33.61
},
"tax": {
"currency_code": "EUR",
"value": 6.38
},
"quantity": "1",
"category": "PHYSICAL_GOODS"
}],
"shipping": {
"name": {
"full_name": "John Doe"
},
"address": {
"address_line_1": "Badensche Str. 24",
"address_line_2": "",
"admin_area_2": "Berlin",
"postal_code": "10715",
"country_code": "DE"
}
}
}]
}]
The answer from Paypal is "REFERENCE_ID_NOT_FOUND". Paypal itself suggests 'default' if there is only one purchase unit (which it is). I also tried to replace 'default' with the reference_id in our system (which was provided with the createOrder call) or paypal´s own transactionId (which is provided through the endpoint anyway). The error is always the same.
How can I change this call so that Paypal accepts it and updates the order in its system?
It turned out, that I had a mistake by sending the value as an array insterad of an object. With the shops own reference Id it worked after fixing this issue.
The Error code-Reposne by paypal was misleading:
"value": [{
->
"value": {
I have this json:
{"id":"***hidden***","event_version":"1.0","create_time":"2020-07-28T04:09:33.415Z","resource_type":"checkout-order","resource_version":"2.0","event_type":"CHECKOUT.ORDER.APPROVED","summary":"An order has been approved by buyer","resource":{"update_time":"2020-07-28T04:09:05Z","create_time":"2020-07-28T04:08:53Z","purchase_units":[{"reference_id":"default","amount":{"currency_code":"CAD","value":"50.00"},"payee":{"email_address":"***hidden**","merchant_id":"***hidden***"},"custom_id":"THISVALUEIWANT","shipping":{"name":{"full_name":"John Doe"},"address":{"address_line_1":"1 Maire-Victorin","admin_area_2":"Toronto","admin_area_1":"ON","postal_code":"M5A 1E1","country_code":"CA"}},"payments":{"captures":[{"id":"***hidden***","status":"COMPLETED","amount":{"currency_code":"CAD","value":"50.00"},"final_capture":true,"seller_protection":{"status":"ELIGIBLE","dispute_categories":["ITEM_NOT_RECEIVED","UNAUTHORIZED_TRANSACTION"]},"seller_receivable_breakdown":{"gross_amount":{"currency_code":"CAD","value":"50.00"},"paypal_fee":{"currency_code":"CAD","value":"1.75"},"net_amount":{"currency_code":"CAD","value":"48.25"}},"links":[{"href":"https://api.sandbox.paypal.com/v2/payments/captures/something","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v2/payments/captures/something/refund","rel":"refund","method":"POST"},{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/something","rel":"up","method":"GET"}],"create_time":"2020-07-28T04:09:05Z","update_time":"2020-07-28T04:09:05Z"}]}}],"links":[{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/something","rel":"self","method":"GET"}],"id":"something","intent":"CAPTURE","payer":{"name":{"given_name":"John","surname":"Doe"},"email_address":"sb-***hidden","payer_id":"something","address":{"country_code":"CA"}},"status":"COMPLETED"},"links":[{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/something","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/something/resend","rel":"resend","method":"POST"}]}';
If i breakdown the structure, it goes like this:
{
"id":"***hidden***",
"event_version":"1.0",
"create_time":"2020-07-28T04:09:33.415Z",
"resource_type":"checkout-order",
"resource_version":"2.0",
"event_type":"CHECKOUT.ORDER.APPROVED",
"summary":"An order has been approved by buyer",
"resource":{
"update_time":"2020-07-28T04:09:05Z",
"create_time":"2020-07-28T04:08:53Z",
"purchase_units":[{
"reference_id":"default",
"amount":{
"currency_code":"CAD",
"value":"50.00"
},
"payee":{
"email_address":"***hidden***",
"merchant_id":"***hidden***"},
"custom_id":"THISISTHEVALUEIWANT",
...
I'd like to extract and store the value of custom_id in a php variable. Problem is I struggle in finding the way to extract this specific item which is, to my understanding, an object, within an array, within an object...
I've tried multiple combinations, I tried foreach loops, nothing worked so far.
The only thing that actually kind of worked is this:
$json = json_decode($jsonobj, true); // json is stored in $jsonobj variable
foreach($json as $elem) {
foreach ($elem['purchase_units'] as $item ) {
echo $item['custom_id'];
}
}
Which lets me get the value I want, but at the same time it throws me a whole bunch of E_WARNING type 2 error:
Invalid argument supplied for foreach()...
Illegal string offset 'purchase_units'...
Can you tell my how I could reach this specific value without flooding my php error log?
Thank you very much!
The following should work for you. I have had to deal with a complexe object structure like this and managed to do it quite simply.
First of all, it's important to note that the original JSON you have provided does not match the formatted sample you have built based on the original. More specifically, the custom_id is not inside of payee. Instead, it's at the same level as payee. It goes like this ...
...
"purchase_units": [
{
"reference_id": "default",
"amount": {
"currency_code": "CAD",
"value": "50.00"
},
"payee": {
"email_address": "***hidden**",
"merchant_id": "***hidden***"
},
"custom_id": "THISVALUEIWANT", <--- This is what you need
"shipping": { ...
The solution I am providing will be based on the original JSON.
<?php
$json = <<<JSON
{
"id": "***hidden***",
"event_version": "1.0",
"create_time": "2020-07-28T04:09:33.415Z",
"resource_type": "checkout-order",
"resource_version": "2.0",
"event_type": "CHECKOUT.ORDER.APPROVED",
"summary": "An order has been approved by buyer",
"resource": {
"update_time": "2020-07-28T04:09:05Z",
"create_time": "2020-07-28T04:08:53Z",
"purchase_units": [
{
"reference_id": "default",
"amount": {
"currency_code": "CAD",
"value": "50.00"
},
"payee": {
"email_address": "***hidden**",
"merchant_id": "***hidden***"
},
"custom_id": "THISVALUEIWANT",
"shipping": {
"name": {
"full_name": "John Doe"
},
"address": {
"address_line_1": "1 Maire-Victorin",
"admin_area_2": "Toronto",
"admin_area_1": "ON",
"postal_code": "M5A 1E1",
"country_code": "CA"
}
},
"payments": {
"captures": [
{
"id": "***hidden***",
"status": "COMPLETED",
"amount": {
"currency_code": "CAD",
"value": "50.00"
},
"final_capture": true,
"seller_protection": {
"status": "ELIGIBLE",
"dispute_categories": [
"ITEM_NOT_RECEIVED",
"UNAUTHORIZED_TRANSACTION"
]
},
"seller_receivable_breakdown": {
"gross_amount": {
"currency_code": "CAD",
"value": "50.00"
},
"paypal_fee": {
"currency_code": "CAD",
"value": "1.75"
},
"net_amount": {
"currency_code": "CAD",
"value": "48.25"
}
},
"links": [
{
"href": "https://api.sandbox.paypal.com/v2/payments/captures/something",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v2/payments/captures/something/refund",
"rel": "refund",
"method": "POST"
},
{
"href": "https://api.sandbox.paypal.com/v2/checkout/orders/something",
"rel": "up",
"method": "GET"
}
],
"create_time": "2020-07-28T04:09:05Z",
"update_time": "2020-07-28T04:09:05Z"
}
]
}
}
],
"links": [
{
"href": "https://api.sandbox.paypal.com/v2/checkout/orders/something",
"rel": "self",
"method": "GET"
}
],
"id": "something",
"intent": "CAPTURE",
"payer": {
"name": {
"given_name": "John",
"surname": "Doe"
},
"email_address": "sb-***hidden",
"payer_id": "something",
"address": {
"country_code": "CA"
}
},
"status": "COMPLETED"
},
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/notifications/webhooks-events/something",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v1/notifications/webhooks-events/something/resend",
"rel": "resend",
"method": "POST"
}
]
}
JSON;
$object = json_decode($json);
$purchase_units = $object->resource->purchase_units ?? [];
$custom_ids = array_column($purchase_units, 'custom_id');
print_r($custom_ids);
The output will be:
Array
(
[0] => THISVALUEIWANT
)
Now a little explanation. After decoding the JSON object, I go straight for the property I need using $purchase_units = $object->resource->purchase_units ?? [];
The null coalesce operator will allow defaulting the $purchase_units variable to an empty array if anything in $object->resource->purchase_units is not present.
The array_column function allows extracting a specific column or property from all the items contained in an array. You can read more about it at https://www.php.net/manual/en/function.array-column.php
I seem to be having some trouble with JSON arrays. I was wondering if someone could please assist me.
I have the following JSON Array, and I want to get the figure in invoice_number
$json = '
{
"id": "PAY-7MP775806F4135612LLCHE4I",
"intent": "sale",
"state": "approved",
"cart": "9KA22662P3559221J",
"payer":
{
"payment_method": "paypal",
"status": "VERIFIED",
"payer_info":
{
"email": "accounts-buyer#traxprint.com",
"first_name": "test",
"last_name": "buyer",
"payer_id": "ZR6SRXGS252RG",
"shipping_address":
{
"recipient_name": "test buyer",
"line1": "1 Cheeseman Ave Brighton East",
"city": "Melbourne",
"state": "Victoria",
"postal_code": "3001",
"country_code": "AU"
},
"phone": "0364424947",
"country_code": "AU"
}
},
"transactions":
[
{
"amount":
{
"total": "3.00",
"currency": "USD",
"details":
{
"subtotal": "3.00"
}
},
"payee":
{
"merchant_id": "DSTEYCMCDUL3Y"
},
"description": "Payment description",
"invoice_number": "abc1231522823790",
"item_list":
{
"items":
[
{
"name": "Subscribe",
"sku": "sub1",
"price": "3.00",
"currency": "USD",
"tax": "0.00",
"quantity": 1
}
],
"shipping_address":
{
"recipient_name": "test buyer",
"line1": "1 Cheeseman Ave Brighton East",
"city": "Melbourne",
"state": "Victoria",
"postal_code": "3001",
"country_code": "AU"
}
},
"related_resources":
[
{
"sale":
{
"id": "41B66647LJ233225Y",
"state": "completed",
"amount":
{
"total": "3.00",
"currency": "USD",
"details":
{
"subtotal": "3.00"
}
},
"payment_mode": "INSTANT_TRANSFER",
"protection_eligibility": "ELIGIBLE",
"protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE",
"transaction_fee":
{
"value": "0.37",
"currency": "USD"
},
"parent_payment": "PAY-7MP775806F4135612LLCHE4I",
"create_time": "2018-04-04T06:36:59Z",
"update_time": "2018-04-04T06:36:59Z",
"links":
[
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/41B66647LJ233225Y",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/41B66647LJ233225Y/refund",
"rel": "refund",
"method": "POST"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-7MP775806F4135612LLCHE4I",
"rel": "parent_payment",
"method": "GET"
}
]
}
}
]
}
],
"create_time": "2018-04-04T06:36:33Z",
"links":
[
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-7MP775806F4135612LLCHE4I",
"rel": "self",
"method": "GET"
}
]
}';
The code I was trying to use is but it comes up with error Undefined property: stdClass::$invoice_number
$decoded = json_decode($json);
echo $Info = $decoded->transactions[0]->payee->invoice_number;
Could someone please tell me why this does not work?
Thanks
Robert
echo $Info = $decoded->transactions[0]->invoice_number;
I have an array which I was able to get some details from my results like this:
$result["transaction"];
when doing this the follow is displayed:
{ "id": "wt13LbKmJ2",
"location_id": "EYGMFA",
"created_at": "2017-07-01T07:32:57Z",
"tenders":
[ { "id": "C6xMF",
"location_id": "MFA",
"transaction_id": "NvDAOOA9",
"created_at": "2017-07-01T07:32:57Z",
"note": "Offering",
"amount_money": { "amount": 100, "currency": "USD" }, "processing_fee_money": { "amount": 33, "currency": "USD" },
"customer_id": "14QEJXXM5TX",
"type": "CARD",
"card_details": { "status": "CAPTURED",
"card": { "brand": "VI", "last_4": "0000" },
"entry_method": "ON_FILE"
} } ],
"product": "EXTERNAL_API" }
How can I get my array to only display "100" where it say "amount":100
If I use:
$result["transaction"]["id"];
it will display only the id but I can't get the amount to display.
Use the following to check ur data and use last line like echo $data->tenders[0]->amount_money->amount;
<?php
$string='{ "id": "wt13LbKmJ2", "location_id": "EYGMFA", "created_at": "2017-07-01T07:32:57Z", "tenders": [ { "id": "C6xMF", "location_id": "MFA", "transaction_id": "NvDAOOA9", "created_at": "2017-07-01T07:32:57Z", "note": "Offering", "amount_money": { "amount": 100, "currency": "USD" }, "processing_fee_money": { "amount": 33, "currency": "USD" }, "customer_id": "14QEJXXM5TX", "type": "CARD", "card_details": { "status": "CAPTURED", "card": { "brand": "VI", "last_4": "0000" }, "entry_method": "ON_FILE" } } ], "product": "EXTERNAL_API" }';
$data=json_decode($string);
echo '<pre>';
print_r(json_decode($string));
echo '</pre>';
echo $data->id.'<br>';
echo $data->tenders[0]->id;
echo $data->tenders[0]->amount_money->amount;
?>
strong text
$result['transaction']['tenders'][0]['amount_money']['amount']
First you need to change $result object to array then fetch the data
do like this
$result=json_decode(json_encode($result,true),true);
Then
$result['transaction']['tenders'][0]['amount_money']['amount']
I thing it will help you.
I'm using PayPal rest-api-sdk-php the latest version. Everything is working fine in terms of creating the payment and executing the payment and getting all the relevant data back but have decided I want the total amount paid via paypal. It returns a JSON object back, example below
"transactions": [
{
"amount": {
"total": "20.00",
"currency": "GBP",
"details": {
"subtotal": "17.50",
"tax": "1.30",
"shipping": "1.20"
}
},
"description": "Payment description",
"invoice_number": "55e30dbd55cea",
"item_list": {
"items": [
{
"name": "Ground Coffee 40 oz",
"price": "7.50",
"currency": "GBP",
"quantity": "1",
"description": "Ground Coffee 40 oz",
"tax": "0.30"
},
{
"name": "Granola bars",
"price": "2.00",
"currency": "USD",
"quantity": "5",
"description": "Granola Bars with Peanuts",
"tax": "0.20"
}
]
},
}
],
I have tried calling $payment->transactions->amount->total but get an error. Just wondered if someone can shed some light. Thanks
"transactions" is an array, so you need to iterate through it.
If you want the first total amount, try $payment->transactions[0]->amount->total;