Pass Json object to other class method - php

I'm trying to pass json decoded object to method of a class. But when I try to access properties of that object, it gives notice: Trying to get property of non-object php. But when I write that object variable to a file it shows all data.
I'm trying to update hubspot data using its api when a webhook event is sent by memberpress plugin.
Here is what i'm doing:
File: hubspotsync.php
require_once 'class-synchronize.php';
class HubspotSync{
public function init()
{
$encoded_data = file_get_contents('php://input');
if( $encoded_data != '' ){
$data = json_decode($encoded_data);
$this->perform_action($data);
}
}
private function perform_action($data)
{
$synchronize = new Synchronize();
$synchronize->perform_sync($data);
unset($synchronize);
}
}
$sync = new HubspotSync();
$sync->init();
unset($sync);
File : class-syncronize.php
class Synchronize{
public function perform_sync($eventobj)
{
// If I fwrite this $eventObj to a file, it looks like as below. but gives an notice on next code. why???
$event_type = $eventobj->event; // This line gives the notice.
}
}
$eventobj looks like this:
stdClass Object
(
[event] => subscription-created
[type] => subscription
[data] => stdClass Object
(
[coupon] =>
[membership] => stdClass Object
(
[id] => 16
[title] => Monthly Subscription
[content] =>
[excerpt] =>
[date] => 2016-09-16 19:22:07
[status] => publish
[author] => 4
[date_gmt] => 2016-09-16 19:22:07
[modified] => 2017-09-27 15:19:04
[modified_gmt] => 2017-09-27 20:19:04
[group] => 0
[price] => 28.00
[period] => 1
[period_type] => months
[signup_button_text] => Sign Up
[limit_cycles] =>
[limit_cycles_num] => 2
[limit_cycles_action] => expire
[trial] =>
[trial_days] => 0
[trial_amount] => 0.00
[trial_once] => 1
[group_order] => 0
[is_highlighted] =>
[pricing_title] => Monthly Subscription
[pricing_show_price] => 1
[pricing_display] => auto
[custom_price] =>
[pricing_heading_txt] =>
[pricing_footer_txt] =>
[pricing_button_txt] => Sign Up
[pricing_button_position] => footer
[pricing_benefits] => Array
(
[0] =>
)
[register_price_action] => default
[register_price] =>
[thank_you_page_enabled] => 1
[thank_you_message] => Thank you for signing up for a monthly subscription to The Prudent Speculator. Please contact info#theprudentspeculator.com with any questions regarding your subscription. Your login credentials should be e-mailed to you shortly.
[custom_login_urls_enabled] =>
[custom_login_urls_default] =>
[custom_login_urls] => Array
(
)
[expire_type] => delay
[expire_after] => 1
[expire_unit] => days
[expire_fixed] => 2017-09-27
[tax_exempt] =>
[allow_renewal] =>
[access_url] =>
[disable_address_fields] =>
[simultaneous_subscriptions] =>
[use_custom_template] =>
[custom_template] => custom/page-archive.php
[customize_payment_methods] => 1
[custom_payment_methods] => Array
(
[0] => owyg62-7fu
)
[customize_profile_fields] => 1
[custom_profile_fields] => Array
(
[0] => mepr_phone
[1] => mepr_username
)
[cannot_purchase_message] => You don't have access to purchase this item.
)
[member] => stdClass Object
(
[id] => 25
[email] => craig.andrews#allies4me.com
[username] => craigandrews
[nicename] => craigandrews
[url] =>
[message] =>
[registered_at] => 2017-10-16 20:56:16
[ip] => 24.27.9.104
[first_name] => Craig
[last_name] => Andrews
[display_name] => craigandrews
)
[id] => 7
[subscr_id] => mp-sub-59e7cdb3d47e0
[response] =>
[gateway] => stdClass Object
(
[name] => PayPal Payments Pro
[label] => Paypal
[use_label] => 1
[icon] => https://prudent2.wpengine.com/wp-content/plugins/memberpress/images/checkout/cards.png
[use_icon] => 1
[desc] => Pay with your credit card via PayPal
[use_desc] => 1
[id] => owyg62-7fu
[settings] => stdClass Object
(
[gateway] => MeprPayPalProGateway
[id] => owyg62-7fu
[label] => Paypal
[use_label] => 1
[icon] => https://prudent2.wpengine.com/wp-content/plugins/memberpress/images/checkout/cards.png
[use_icon] => 1
[desc] => Pay with your credit card via PayPal
[use_desc] => 1
[api_username] => kbroughton_api1.afamcapital.com
[api_password] => 48UDEBHJBB6RXNVY
[signature] => AQUF7rVduRDHYM-3AIBZaDFk-4ZYAZQgmcG7B9LdUmyA7fRkA9Mk1gPj
[sandbox] => on
[force_ssl] => on
[debug] =>
[saved] => 1
[url] => https://www.sandbox.paypal.com/cgi-bin/webscr
[api_url] => https://api-3t.sandbox.paypal.com/nvp
[api_version] => 69
)
[capabilities] => Array
(
[0] => process-payments
[1] => process-refunds
[2] => create-subscriptions
[3] => cancel-subscriptions
[4] => update-subscriptions
[5] => suspend-subscriptions
[6] => resume-subscriptions
[7] => subscription-trial-payment
)
)
[ip_addr] => 70.112.70.86
[price] => 28.00
[period] => 1
[period_type] => months
[limit_cycles] => 0
[limit_cycles_num] => 2
[limit_cycles_action] => expire
[prorated_trial] => 0
[trial] => 0
[trial_days] => 0
[trial_amount] => 0.00
[status] => pending
[created_at] => 2017-10-18 21:54:59
[total] => 28.00
[tax_rate] => 0.000
[tax_amount] => 0.00
[tax_desc] =>
[tax_class] => standard
[cc_last4] =>
[cc_exp_month] =>
[cc_exp_year] =>
[token] =>
[tax_compound] => 0
[tax_shipping] => 1
)
)

var_dump( $eventObj ) and make sure that the event property actually exists on that object. It might be $eventObj['event'] or something weird like that. The error is telling you that event doesnt exist on your $eventObj

Related

How to get status value string from razorpay array

Razorpay\Api\Payment Object ( [attributes:protected] => Array ( [id] => pay_IOx1H64JMSEFAv [entity] => payment [amount] => 100 [currency] => INR [status] => captured [order_id] => order_IOx0xic60K6TZi [invoice_id] => [international] => [method] => wallet [amount_refunded] => 0 [refund_status] => [captured] => 1 [description] => [card_id] => [bank] => [wallet] => payzapp [vpa] => [email] => sudiptobain8#gmail.com [contact] => +919134322935 [notes] => Razorpay\Api\Payment Object ( [attributes:protected] => Array ( [shopping_order_id] => 3456 ) ) [fee] => 2 [tax] => 0 [error_code] => [error_description] => [error_source] => [error_step] => [error_reason] => [acquirer_data] => Razorpay\Api\Payment Object ( [attributes:protected] => Array ( [transaction_id] => ) ) [created_at] => 1637649968 ) )
help to get [status] => captured From This in php
You need to capture objects using object (arrow) notation
<?php
echo $result->status;
?>

How to get values from array object in PHP

I have the following array and when I do print("<pre>".print_r($term_arr,true)."</pre>");, I get:
Array
(
[0] => Array
(
[status] => 1
[message] => Charge attempted
[data] => Array
(
[amount] => 1000000
[currency] => NGN
[transaction_date] => 2021-05-04T03:18:03.000Z
[status] => success
[reference] => tbhlnqs9elbcoth
[domain] => test
[metadata] =>
[gateway_response] => Approved
[message] =>
[channel] => card
[ip_address] =>
[log] =>
[fees] => 25000
[authorization] => Array
(
[authorization_code] => AUTH_z260f8cskt
[bin] => 408408
[last4] => 4081
[exp_month] => 12
[exp_year] => 2030
[channel] => card
[card_type] => visa
[bank] => TEST BANK
[country_code] => NG
[brand] => visa
[reusable] => 1
[signature] => SIG_TyzxLul2N9M3RSX5MJIY
[account_name] =>
)
[customer] => Array
(
[id] => 43591782
[first_name] =>
[last_name] =>
[email] => ele48#gmail.com
[customer_code] => CUS_rs2vvoeo7pe6f1b
[phone] =>
[metadata] =>
[risk_action] => default
[international_format_phone] =>
)
[plan] =>
[id] => 1112036335
)
)
[1] => Array
(
[status] => 1
[message] => Charge attempted
[data] => Array
(
[amount] => 100000
[currency] => NGN
[transaction_date] => 2021-05-04T03:18:03.000Z
[status] => success
[reference] => fxi85hsbg2u8jwi
[domain] => test
[metadata] =>
[gateway_response] => Approved
[message] =>
[channel] => card
[ip_address] =>
[log] =>
[fees] => 1500
[authorization] => Array
(
[authorization_code] => AUTH_bn008ru8rq
[bin] => 408408
[last4] => 4081
[exp_month] => 12
[exp_year] => 2030
[channel] => card
[card_type] => visa
[bank] => TEST BANK
[country_code] => NG
[brand] => visa
[reusable] => 1
[signature] => SIG_TyzxLul2N9M3RSX5MJIY
[account_name] =>
)
[customer] => Array
(
[id] => 43746063
[first_name] =>
[last_name] =>
[email] => gabwebby#gmail.com
[customer_code] => CUS_d4r1mko8twyc6vg
[phone] =>
[metadata] =>
[risk_action] => default
[international_format_phone] =>
)
[plan] =>
[id] => 1112036343
)
)
I am trying to get the value of customer, so I did the following:
foreach($term_arr as $value){
echo 'Title: ' . $value->customer->customer_code .'<br/>';
}
But I have this error:
PHP Notice: Trying to get property 'customer' of non-object in PHP
Notice: Trying to get property 'customer_code' of non-object in
How can I get the values of array object?
Firstly your inner array is associatve array so if we want values to get we need to traverse using key.
Solution you tried to get customer code where hierarchy is not proper
When you iterate over you parent index array hierarchy is data->customer->customer code
so your customer resides under data array thats why you are getting notice that customer is non object of value.
So solution or you can iterate like will give you customer code,
foreach($term_arr as $value){
print_r($value["data"]["customer"]["customer_code"]."\n");
}

Getting just one value from an array (php) [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 3 years ago.
When I print print_r($event_json); it gives me the array below. I need to get just one value from it [type] as in [type] => charge.succeeded (see the top of the array). I tried echo $event_json[0]['type']; but I am getting nothing.
stdClass Object
(
[created] => 1326853478
[livemode] =>
[id] => charge.succeeded_00000000000000
[type] => charge.succeeded
[object] => event
[request] =>
[pending_webhooks] => 1
[api_version] => 2018-10-31
[data] => stdClass Object
(
[object] => stdClass Object
(
[id] => ch_00000000000000
[object] => charge
[amount] => 500
[amount_refunded] => 0
[application] =>
[application_fee] =>
[application_fee_amount] =>
[balance_transaction] => txn_00000000000000
[billing_details] => stdClass Object
(
[address] => stdClass Object
(
[city] =>
[country] =>
[line1] =>
[line2] =>
[postal_code] =>
[state] =>
)
[email] =>
[name] => someemail#yahoo.com
[phone] =>
)
[captured] =>
[created] => 1553691107
[currency] => usd
[customer] => cus_00000000000000
[description] =>
[destination] =>
[dispute] =>
[failure_code] => card_declined
[failure_message] => Your card was declined.
[fraud_details] => stdClass Object
(
[stripe_report] => fraudulent
)
[invoice] =>
[livemode] =>
[metadata] => stdClass Object
(
)
[on_behalf_of] =>
[order] =>
[outcome] => stdClass Object
(
[network_status] => not_sent_to_network
[reason] => merchant_blacklist
[risk_level] => highest
[risk_score] => 98
[seller_message] => Stripe blocked this payment.
[type] => blocked
)
[paid] => 1
[payment_intent] =>
[payment_method_details] => stdClass Object
(
[card] => stdClass Object
(
[brand] => visa
[checks] => stdClass Object
(
[address_line1_check] =>
[address_postal_code_check] =>
[cvc_check] => unavailable
)
[country] => US
[exp_month] => 1
[exp_year] => 2022
[fingerprint] => Gawa768Trrk4fEmb
[funding] => credit
[last4] => 0019
[three_d_secure] =>
[wallet] =>
)
[type] => card
)
[receipt_email] =>
[receipt_number] =>
[receipt_url] => https://pay.stripe.com/receipts/acct_1DU4J1IcP22cq9aQ/ch_1EIbENIcP22cq9aQk7r9xN3u/rcpt_Em9Xy3Ambv1b8QZISOsIHdbxCnLdgup
[refunded] =>
[refunds] => stdClass Object
(
[object] => list
[data] => Array
(
)
[has_more] =>
[total_count] => 0
[url] => /v1/charges/ch_1EIbENIcP22cq9aQk7r9xN3u/refunds
)
[review] =>
[shipping] =>
[source] => stdClass Object
(
[id] => card_00000000000000
[object] => card
[address_city] =>
[address_country] =>
[address_line1] =>
[address_line1_check] =>
[address_line2] =>
[address_state] =>
[address_zip] =>
[address_zip_check] =>
[brand] => Visa
[country] => US
[customer] => cus_00000000000000
[cvc_check] => unavailable
[dynamic_last4] =>
[exp_month] => 1
[exp_year] => 2022
[fingerprint] => Gawa768Trrk4fEmb
[funding] => credit
[last4] => 0019
[metadata] => stdClass Object
(
)
[name] => someemail#yahoo.com
[tokenization_method] =>
)
[source_transfer] =>
[statement_descriptor] =>
[status] => failed
[transfer_data] =>
[transfer_group] =>
)
)
)
What you are seeing is an stdClass Object and not an array. Do like below:
<?php
echo $event_json->type;

How to retrieve the values from Stripe for webhook

I am using webhook of stripe to receive some information after successful payment
but it give me error
<?php
include 'dbConfig.php'; // For Connecting the database to Store the retrieve value in SQL database
include 'init.php';
Stripe::setApiKey("sk_test_*****************");
// retrieve the request's body and parse it as JSON
$input = file_get_contents("php://input");
$event = json_decode($input);
if ($event_json->type == "charge.succeeded") {
print_r($event_json);
}
its give me this response
stdClass Object
(
[created] => 1326853478
[livemode] =>
[id] => evt_00000000000000
[type] => charge.succeeded
[object] => event
[request] =>
[pending_webhooks] => 1
[api_version] => 2017-02-14
[data] => stdClass Object
(
[object] => stdClass Object
(
[id] => ch_00000000000000
[object] => charge
[amount] => 249
[amount_refunded] => 0
[application] =>
[application_fee] =>
[balance_transaction] => txn_00000000000000
[captured] => 1
[created] => 1489056204
[currency] => usd
[customer] =>
[description] => Movie Title
[destination] =>
[dispute] =>
[failure_code] =>
[failure_message] =>
[fraud_details] => stdClass Object
(
)
[invoice] =>
[livemode] =>
[metadata] => stdClass Object
(
[order_id] => 6735
)
[on_behalf_of] =>
[order] =>
[outcome] => stdClass Object
(
[network_status] => approved_by_network
[reason] =>
[risk_level] => normal
[seller_message] => Payment complete.
[type] => authorized
)
[paid] => 1
[receipt_email] =>
[receipt_number] =>
[refunded] =>
[refunds] => stdClass Object
(
[object] => list
[data] => Array
(
)
[has_more] =>
[total_count] => 0
[url] => /v1/charges/ch_19vOjwKGQPjNjAif9fuYkcsj/refunds
)
[review] =>
[shipping] =>
[source] => stdClass Object
(
[id] => card_00000000000000
[object] => card
[address_city] => ludhiana
[address_country] => AL
[address_line1] => hn 88 st.no5
[address_line1_check] => pass
[address_line2] =>
[address_state] => skdjflsdf
[address_zip] => skfnsd
[address_zip_check] => pass
[brand] => Visa
[country] => US
[customer] =>
[cvc_check] => pass
[dynamic_last4] =>
[exp_month] => 1
[exp_year] => 2018
[funding] => credit
[last4] => 4242
[metadata] => stdClass Object
(
)
[name] => jagtarnice
[tokenization_method] =>
)
[source_transfer] =>
[statement_descriptor] =>
[status] => succeeded
[transfer_group] =>
)
)
)
and i want to store value from this response
like status, paymentdate and description
$customer_id = $event_json->data->object->customer;
$customer = Stripe_Customer::retrieve($customer_id);
$charge_id = $event_json->data->object->id;
$charge = Stripe_Charge::retrieve($charge_id);
This thing is not works

Get Value from an array with foreach

i've an format array like this
Array (
[0] => stdClass Object ( [subscriptionContractId] => 20878 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-02 03:27:39.000 [endDate] => 2015-07-04 03:27:39.000 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 2 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => 2015-07-03 03:27:39.000 [lastPaymentDate] => 2015-07-02 03:27:39.000 [subscriptionContractHistory] =>Array ( [0] => stdClass Object ( [subscriptionContractId] => 20878 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-07-01 03:27:53.290 ) ) [subscriptionContractLogs] => )
[1] => stdClass Object ( [subscriptionContractId] => 20861 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:31:47.000 [endDate] => 2015-06-30 11:32:05.150 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:31:47.000 [subscriptionContractHistory] => Array ( [0] =>
stdClass Object ( [subscriptionContractId] => 20861 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:31:55.530 ) [1] => stdClass Object ( [subscriptionContractId] => 20861 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:32:05.977 ) ) [subscriptionContractLogs] => )
[2] => stdClass Object ( [subscriptionContractId] => 20860 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:29:37.000 [endDate] => 2015-06-30 11:30:19.887 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:29:37.000 [subscriptionContractHistory] => Array ( [0] => stdClass Object ( [subscriptionContractId] => 20860 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:30:10.267 ) [1] => stdClass Object ( [subscriptionContractId] => 20860 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:30:20.687 ) ) [subscriptionContractLogs] => )
[3] => stdClass Object ( [subscriptionContractId] => 20859 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:27:33.000 [endDate] => 2015-06-30 11:27:57.683 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:27:33.000 [subscriptionContractHistory] => Array ( [0] => stdClass Object ( [subscriptionContractId] => 20859 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:27:42.173 ) [1] => stdClass Object ( [subscriptionContractId] => 20859 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:27:58.467 ) ) [subscriptionContractLogs] => )
i want to take this value [subscriptionContractId] => 20859
PFB my code
foreach($getContract as $key =>$value) {
$cancel = $getContract[$key]->subscriptionContractId;
$this->data['res'] = $cancel;
}
with that above code only showing single value of record, meanwhile record is around 27 records.
and another code, i try to rectify with initialize array, PFB.
$cancel = array()
foreach($getContract as $key =>$value) {
$cancel[] = $getContract[$key]->subscriptionContractId;
$this->data['res'] = $cancel;
}
i need only the value, of the array such as 20879, etc. eventhough i use double loop to extract particular array it only show the last record on 10663.
Please help
You can modify your code as follows (see php documenation about foreach for more background):
$cancel = array();
foreach($getContract as $value) {
$cancel[] = $value->subscriptionContractId;
...
}
This way, in every iteration $value is assigned one value from your getContract array, with it's key just being omitted. Then you can store the value's subscriptionContractId in your $cancel array to do whatever you are planning to do.
Note: briosheje is right about the line:
$this->data['res'] = $cancel;
$this->data['res'] is overwritten in every iteration. As you are overwriting it with the your $cancel array, your code should still work, but your doing unnecessary work here, as your array might be subjected to further change in the next iteration.
My advice would be to move this line after your loop brackets ended, so that your result is saved just once:
...
foreach($getContract as $value) {
...
}
$this->data['res'] = $cancel;

Categories