Store this as an object - php

SOLUTION:
Each of the keys ending :private had a __get() and toJSON() method which is needed to get the data from them as _propMap is private.
I'm using PayPal's PHP API to take payments from PayPal, and the data I get after a payment has been completed, comes back as this
Array
(
[PayPal\Common\PPModel_propMap] => Array
(
[id] => PAY-THEID
[create_time] => 2013-12-03T15:47:15Z
[update_time] => 2013-12-03T15:47:34Z
[state] => approved
[intent] => sale
[payer] => PayPal\Api\Payer Object
(
[_propMap:PayPal\Common\PPModel:private] => Array
(
[payment_method] => paypal
[payer_info] => PayPal\Api\PayerInfo Object
(
[_propMap:PayPal\Common\PPModel:private] => Array
(
[email] => my#email.com
[first_name] => Tom
[last_name] => Hart
[payer_id] => thePayerId
[shipping_address] => PayPal\Api\Address Object
(
[_propMap:PayPal\Common\PPModel:private] => Array
(
[line1] => 1 Main Terrace
[city] => Wolverhampton
[state] => West Midlands
[postal_code] => W12 4LQ
[country_code] => GB
)
)
)
)
)
)
[transactions] => Array
(
[0] => PayPal\Api\Transaction Object
(
[_propMap:PayPal\Common\PPModel:private] => Array
(
[amount] => PayPal\Api\Amount Object
(
[_propMap:PayPal\Common\PPModel:private] => Array
(
[total] => 0.33
[currency] => GBP
[details] => PayPal\Api\Details Object
(
[_propMap:PayPal\Common\PPModel:private] => Array
(
[subtotal] => 0.11
[tax] => 0.11
[shipping] => 0.11
)
)
)
)
[description] => Payment description
[item_list] => PayPal\Api\ItemList Object
(
[_propMap:PayPal\Common\PPModel:private] => Array
(
[items] => Array
(
[0] => PayPal\Api\Item Object
(
[_propMap:PayPal\Common\PPModel:private] => Array
(
[name] => Flowers
[price] => 0.11
[currency] => GBP
[quantity] => 1
)
)
)
)
)
[related_resources] => Array
(
[0] => PayPal\Api\RelatedResources Object
(
[_propMap:PayPal\Common\PPModel:private] => Array
(
[sale] => PayPal\Api\Sale Object
(
[_propMap:PayPal\Common\PPModel:private] => Array
(
[id] => 9L998852HN614082X
[create_time] => 2013-12-03T15:47:15Z
[update_time] => 2013-12-03T15:47:34Z
[state] => completed
[amount] => PayPal\Api\Amount Object
(
[_propMap:PayPal\Common\PPModel:private] => Array
(
[total] => 0.33
[currency] => GBP
)
)
[parent_payment] => PAY-4S184757A49956741KKO72AY
[links] => Array
(
[0] => PayPal\Api\Links Object
(
[_propMap:PayPal\Common\PPModel:private] => Array
(
[href] => https://api.sandbox.paypal.com/v1/payments/sale/9L998852HN614082X
[rel] => self
[method] => GET
)
)
[1] => PayPal\Api\Links Object
(
[_propMap:PayPal\Common\PPModel:private] => Array
(
[href] => https://api.sandbox.paypal.com/v1/payments/sale/abc/refund
[rel] => refund
[method] => POST
)
)
[2] => PayPal\Api\Links Object
(
[_propMap:PayPal\Common\PPModel:private] => Array
(
[href] => https://api.sandbox.paypal.com/v1/payments/payment/PAY-abc
[rel] => parent_payment
[method] => GET
)
)
)
)
)
)
)
)
)
)
)
[links] => Array
(
[0] => PayPal\Api\Links Object
(
[_propMap:PayPal\Common\PPModel:private] => Array
(
[href] => https://api.sandbox.paypal.com/v1/payments/payment/abc
[rel] => self
[method] => GET
)
)
)
)
)
I want to convert parts of that (shipping_address being one of them) to a json object (using json_encode/decode(), and store it in my database so in the admin section I can view the address so I know where to send things, however, I can't convert that to a json object, it just returns {}. How can I store these details in my database to view them later?
EDIT:
Json_encode code
$db['address'] = json_encode((array) $dets->payer->payer_info->shipping_address);
var_dump(json_last_error());
$db['payerId'] = $dets->payer->payer_info->payer_id;
$db['prices'] = json_encode((array) $dets->transations[0]->amount);
var_dump(json_last_error());
$db['description'] = $dets->transations[0]->description;
$db['items'] = json_encode((array) $dets->transations[0]->item_list->items);
var_dump(json_last_error());
$db['links'] = json_encode((array) $dets->related_resources[0]->sale->links);
var_dump(json_last_error());
The output of the $db array is
Array
(
[userId] => 10
[paymentId] => PAY-4VC71851RJ180032AKKPAB3Y
[state] => approved
[address] => {"\u0000PayPal\\Common\\PPModel\u0000_propMap":{"line1":"1 Main Terrace","city":"Wolverhampton","state":"West Midlands","postal_code":"W12 4LQ","country_code":"GB"}}
[payerId] => P77LD9M7MUQN2
[prices] => []
[description] =>
[items] => []
[links] => []
)
So some are getting encoded, some arnt.

This was also bothering me for a while and I struggled to find a straightforward answer. I'm using the current PHP REST STK and in the sample ExecutePayment.php file saw something like this:
// Execute the payment
// (See bootstrap.php for more on `ApiContext`)
$result = $payment->execute($execution, $apiContext);
echo "<html><body><pre>";
var_dump($result);
echo "</pre><a href='../index.html'>Back</a></body></html>";
As noted, the $result object is not very helpful to navigate. You can't directly encode the object as PayPal is doing some crazy stuff on their own in the SDK. As noted, the solution is to use the toJSON() method from the PPModal class to convert that object into JSON directly:
$result = $payment->execute($execution, $apiContext);
$array = $result->toJSON();
echo '<pre>';
print_r($array);
echo '</pre>';
I hope that's a little clearer to somebody.

I think the problem you are having with some values "not being encoded" is that some of your variable references are wrong.
For example, you are trying to encode:
$dets->transations[0]->amount
Whereas, I think you need to get that value from:
$dets->transactions[0]['_propMap:PayPal\Common\PPModel:private']['amount']
You have similar issue for most of your variable references.

Related

php parse array in object detail

I got stuck on parsing array in my php object
stdClass Object
(
[result] => stdClass Object
(
[details] => Array
(
[0] => stdClass Object
(
[account] =>
[address] => add1
[category] => receive
[amount] => 1100
[label] =>
[vout] => 1
)
[1] => stdClass Object
(
[account] =>
[address] => add2
[category] => receive
[amount] => 11600
[label] =>
[vout] => 2
)
[2] => stdClass Object
(
[account] =>
[address] => add3
[category] => receive
[amount] => 1000
[label] =>
[vout] => 4
)
)
)
)
So how I can fetch all details indexes 0,1,2 etc
So, you could just iterate over details as below:
foreach($your_variable->result->details as $current_detail){
echo $current_detail->account;
// other code here
}
Your data is object if you converto to array u can simply do it
$array=json_decode(json_encode($data),true);
print_r($array);
If just one details key
$array=json_decode(json_encode($data),true)
print_r($array['result']['details']);

Paypal PLUS 'Direct Debit' does not work

I have the following array for paypal plus. This works great for (iframe) 'Paypal' and 'Credit Card' payments. But does not work for 'Direct Debit'.
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[intent] => sale
[experience_profile_id] => TXP-2UP57256T6655231G
[payer] => PayPal\Api\Payer Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[payment_method] => paypal
)
)
[redirect_urls] => PayPal\Api\RedirectUrls Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[return_url] => http://test.localhost/paypal/payment.php?success=true
[cancel_url] => http://test.localhost/paypal/payment.php?success=false
)
)
[transactions] => Array
(
[0] => PayPal\Api\Transaction Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[amount] => PayPal\Api\Amount Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[total] => 10.50
[currency] => USD
[details] => PayPal\Api\Details Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[subtotal] => 7.50
[tax] => 1.50
[shipping] => 1.50
)
)
)
)
[description] => Payment description
[invoice_number] => 5936611845485
[item_list] => PayPal\Api\ItemList Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[items] => Array
(
[0] => PayPal\Api\Item Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[name] => Ground Coffee 40 oz
[description] => Ground Coffee 40 oz
[price] => 7.50
[currency] => USD
[quantity] => 1
)
)
)
)
)
[related_resources] => Array
(
)
)
)
)
[id] => PAY-9X212986B0791962DLE3GCGI
[state] => created
[create_time] => 2017-06-06T08:00:25Z
[links] => Array
(
[0] => PayPal\Api\Links Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[href] => https://api.sandbox.paypal.com/v1/payments/payment/PAY-9X212986B0791962DLE3GCGI
[rel] => self
[method] => GET
)
)
[1] => PayPal\Api\Links Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[href] => https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-41B117362N517473Y
[rel] => approval_url
[method] => REDIRECT
)
)
[2] => PayPal\Api\Links Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[href] => https://api.sandbox.paypal.com/v1/payments/payment/PAY-9X212986B0791962DLE3GCGI/execute
[rel] => execute
[method] => POST
)
)
)
)
Once i put all of my dummy bank details it says: We can not complete your purchase at the time
Please go back to the dealer and choose another payment method.
Here is the link (I am testing it in sandbox):
Can any one help?

php read from array inside class

when i use print_r for the response from API URL i got the following result:
stdClass Object(
[Messages] => Array
(
[0] => stdClass Object
(
[MessageID] => 990950058
[Recipient] => 966000000000
[Status] => Queued
)
[1] => stdClass Object
(
[MessageID] => 990950059
[Recipient] => 966500000000
[Status] => Queued
)
)
[NumberOfUnits] => 1
[Cost] => 0.00000
[Balance] => 2.89050
[TimeCreated] => 2016-11-10 14:03:49
[CurrencyCode] => SAR
)
the problem is i want to make a loop to read the values for MessageID
how i can do it?
so simple use foreach is used to loop through each key/value pair
foreach($your_variable->Messages as $row)
{
echo $row->MessageID;
}

parsing xml array response in PHP objects

i am working on a UPS tracking API, i have called the API to track the package, i am getting the right response, but in array format, i am new to json decoding, please tell me how to parse this in PHP, do i have to create multiple objects in PHP? i am attaching the output as well as the PHP code.
PHP Code
if (isset($_POST['af0'])) {
if (preg_match('/^[a-z\d_]{4,80}$/i', $_POST['trackingNumber'])) {
$cleanTrackingNumber = $_POST['trackingNumber'];
$someArray = upsTrack("$cleanTrackingNumber");
echo '<pre>'; print_r($someArray); echo '</pre>';
} else {
echo 'Invalid tracking number... sigh...';
}
}
Output
Array
(
[TRACKRESPONSE] => Array
(
[RESPONSE] => Array
(
[TRANSACTIONREFERENCE] => Array
(
[XPCIVERSION] => 1.0
)
[RESPONSESTATUSCODE] => 1
[RESPONSESTATUSDESCRIPTION] => Success
)
[SHIPMENT] => Array
(
[SHIPPER] => Array
(
[SHIPPERNUMBER] => A6161A
[ADDRESS] => Array
(
[ADDRESSLINE1] => 132 E 43RD ST
[CITY] => NEW YORK
[STATEPROVINCECODE] => NY
[POSTALCODE] => 10017 4019
[COUNTRYCODE] => US
)
)
[SHIPTO] => Array
(
[ADDRESS] => Array
(
[CITY] => TORONTO
[STATEPROVINCECODE] => ON
[POSTALCODE] => M5V3X1
[COUNTRYCODE] => CA
)
)
[SHIPMENTWEIGHT] => Array
(
[UNITOFMEASUREMENT] => Array
(
[CODE] => LBS
)
[WEIGHT] => 3.20
)
[SERVICE] => Array
(
[CODE] => 011
[DESCRIPTION] => STANDARD
)
[REFERENCENUMBER] => Array
(
[CODE] => 13
[VALUE] => A6161AD9HPK
)
[SHIPMENTIDENTIFICATIONNUMBER] => 1ZA6161A6832763249
[PICKUPDATE] => 20140519
[SCHEDULEDDELIVERYDATE] => 20140521
[PACKAGE] => Array
(
[TRACKINGNUMBER] => 1ZA6161A6832763249
[ACTIVITY] => Array
(
[ACTIVITYLOCATION] => Array
(
[ADDRESS] => Array
(
[CITY] => SECAUCUS
[STATEPROVINCECODE] => NJ
[COUNTRYCODE] => US
)
)
[STATUS] => Array
(
[STATUSTYPE] => Array
(
[CODE] => I
[DESCRIPTION] => DEPARTURE SCAN
)
[STATUSCODE] => Array
(
[CODE] => DP
)
)
[DATE] => 20140520
[TIME] => 053000
)
[MESSAGE] => Array
(
[CODE] => 01
[DESCRIPTION] => On Time
)
[PACKAGEWEIGHT] => Array
(
[UNITOFMEASUREMENT] => Array
(
[CODE] => LBS
)
[WEIGHT] => 3.20
)
[REFERENCENUMBER] => Array
(
[CODE] => 19
[VALUE] => MMTD71EUY061A
)
)
)
)
)
If you already have the output in Array format then you don't have anything more to do with json decoding.
You can use $someArray to get any data from the array.
It's up to you if you want to just display it or save it, for example in a database.
What is it you trying to accomplish?

Freshbooks API show details

I am using the php library from here and I have a small problem.
To get some info for a invoice 118868, I can do this
<?php
$invoice_id=118868;
$invoice=$freshbooks->invoiceGet($invoice_id);
print_r($invoice);
?>
This is the output for print_r
Class Object ( [#attributes] => stdClass Object ( [status] => ok ) [invoice] => stdClass Object ( [invoice_id] => 00000219023 [estimate_id] => stdClass Object ( ) [number] => 8822 [client_id] => 83 [contacts] => stdClass Object ( [contact] => stdClass Object ( [contact_id] => 0 ) ) [recurring_id] => stdClass Object ( ) [organization] => Jimmy Thwart [first_name] => stdClass Object ( ) [last_name] => stdClass Object ( ) [p_street1] => stdClass Object ( ) [p_street2] => stdClass Object ( ) [p_city] => stdClass Object ( ) [p_state] => stdClass Object ( ) [p_country] => stdClass Object ( ) [p_code] => stdClass Object ( ) [po_number] => 10002 [status] => sent [amount] => 16.90 [amount_outstanding] => 16.90 [paid] => 0.00 [date] => 2013-01-31 00:00:00 [notes] => stdClass Object ( ) [terms] => Your slot can only be secured upon payment. Any reservation made before payment will only be guaranteed for 2 days. [discount] => 0 [url] => https://example.freshbooks.com/view/vgPb2TNGCR7n8JV [auth_url] => https://example.freshbooks.com/invoices/219023 [links] => stdClass Object ( [client_view] => https://example.freshbooks.com/view/vgPb2TNGCR7n8JV [view] => https://example.freshbooks.com/invoices/219023 [edit] => https://example.freshbooks.com/invoices/219023/edit ) [return_uri] => stdClass Object ( ) [updated] => 2013-01-31 02:25:13 [currency_code] => SGD [language] => en [vat_name] => stdClass Object ( ) [vat_number] => stdClass Object ( ) [folder] => active [staff_id] => 1 [lines] => stdClass Object ( [line] => stdClass Object ( [line_id] => 1 [name] => Lady 1pax [description] => Services (1 pax) [unit_cost] => 16.90 [quantity] => 1 [amount] => 16.90 [tax1_name] => stdClass Object ( ) [tax2_name] => stdClass Object ( ) [tax1_percent] => 0 [tax2_percent] => 0 [compound_tax] => 0 [type] => Item ) ) [gateways] => stdClass Object ( [gateway] => stdClass Object ( [name] => PayPal ) ) ) )
I hope the output can only be the URL and not this whole chunk of code.
Output wanted:
https://example.freshbooks.com/view/vgPb2TNGCR7n8JV
However, it lists all information of the invoice. How do I get only the invoice URL?
Well, using the output of your code, you can see what the elements of $invoice are. Use the one that you need.
Invoice is an instane of a class. You should treat it like all instances.
To get URL value you can do the next:
<?php
$invoice_id=118868;
$invoice=$freshbooks->invoiceGet($invoice_id);
print $invoice->url;
?>
or there's another possible value you're looking for:
<?php
$invoice_id=118868;
$invoice=$freshbooks->invoiceGet($invoice_id);
print $invoice->links->client_view;
?>
According to official API doc <url> is not supporting any more, so you should use <links> "element to provide your customers with direct links to the invoice for editing, viewing by the client and viewing by an administrator."

Categories