Laravel: Receive data from ChargeBee - laravel

I managed to create a Customer in ChargeBee but I don't know how to access each of its values.
To receive the information I use the following code, it works perfect for me ...
ChargeBee_Environment::configure("mysite-test","test_afhjsfslfsfslk438493sshgeh");
$result = ChargeBee_Customer::retrieve("16HHGDGJslfksds");
$customer = $result->customer();
$card = $result->card();
By doing a dd($result) I get the following:
ChargeBee_Result {#356 ▼
-_response: array:1 [▼
"customer" => array:22 [▼
"id" => "16HHGDGJslfksds"
"first_name" => "User"
"last_name" => "Test"
"email" => "user#mail.com"
"phone" => "555667788"
"auto_collection" => "on"
"net_term_days" => 0
"allow_direct_debit" => false
"created_at" => 1603420880
"taxability" => "taxable"
"updated_at" => 1603420880
"pii_cleared" => "active"
"resource_version" => 1603420880216
"deleted" => false
"object" => "customer"
"billing_address" => array:6 [▶]
"card_status" => "no_card"
"promotional_credits" => 0
"refundable_credits" => 0
"excess_payments" => 0
"unbilled_charges" => 0
"preferred_currency_code" => "USD"
]
]
-_responseObj: array:1 [▼
"customer" => ChargeBee_Customer {#358 ▶}
]
}
How can I get for example the id of this Customer??? Which in this case is 16HHGDGJslfksds

You can use this line:
$customerId = $result->customer()->id

Related

How to search value in collection and merge model in laravel

I have 2 arrays and each array has deeply nested array below is the array
$invoices = $request->user()->invoices();
0 => array:74 [▼
"id" => "in_1MPwwCGBXoaOj5Wmk7lPY9tJ
"lines" => array:5 [▼
"object" => "list"
"data" => array:2 [▼
0 => array:24 [▶]
1 => array:24 [▶]
]
"has_more" => false
"total_count" => 2
"url" => "/v1/invoices/"
]
1 => array:74 [▼
"id" => "in_1MPwwCGBXoaO22vWmk7lPY9tJ
"lines" => array:5 [▼
"object" => "list"
"data" => array:2 [▼
0 => array:24 [▶]
1 => array:24 [▶]
]
"has_more" => false
"total_count" => 2
"url" => "/v1/invoices/"
]
i am trying to get the id from the deep nested array like this
$productnames = $invoices->first()->lines->first()->id;
and then search for the id in the modal like this
$plans = Plan::where('stripe_id', $productnames)->first();
it only display one id, and not all the id from the array,
also, when i merge both the collection it shows result like the below
$invoicescollection = new Collection($invoices);
$planscollection = new Collection($plans);
$products = $invoicescollection->merge($plans)->toArray();
array:10 [▼ // app/Http/Controllers/Billing/BillingController.php:26
0 => array:74 [▶]
1 => array:74 [▶]
"id" => 5
"title" => "All"
"slug" => "all"
"stripe_id" => "price_1MO55aOGBXoaOjvWmuQCJ14b3"
"active" => "0"
"created_at" => null
"updated_at" => null
]
i wanted to show the merge data in each array, not like the above.
thank you for the help.

laravel model object changed unexpectedly while doing map operation on it

This is some wierd situation in which I am there right now like I have one object
$this->remitter;
if I dump this I get this
App\Models\IpayAppBc\Remitter {#1960
#connection: "ipay_app_bc__write"
+table: "remitter"
+timestamps: false
#primaryKey: "id"
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:15 [
"id" => 15016478
"name" => "ASKHSYA"
"name_last" => ""
"dob" => null
"mobile" => "1234567"
"limit_expiry" => "{"PB":"202212"}"
"address" => "PANCHKULA PANCHKULA HARYANA"
"city" => "PANCHKULA"
"pincode" => "134117"
"state" => "HARYANA"
"two_factor" => 0
"status" => 0
"otp_status" => 2
"paytm_key" => "h00097ea42dc-a6ed-42bc-b2cf-3b6a876f878b"
"ab_kyc" => 0
]
#original: array:15 [
"id" => 15016478
"name" => "ASKHSYA"
"name_last" => ""
"dob" => null
"mobile" => "9946617986"
"limit_expiry" => "{"PB":"202212"}"
"address" => "PANCHKULA PANCHKULA HARYANA"
"city" => "PANCHKULA"
"pincode" => "134117"
"state" => "HARYANA"
"two_factor" => 0
"status" => 0
"otp_status" => 2
"paytm_key" => "h00097ea42dc-a6ed-42bc-b2cf-3b6a876f878b"
"ab_kyc" => 0
]
Now I did this
$remitterDetails = $this->remitter;
$remitterDetails->beneficiaries = $remitterDetails->beneficiaries->take(50)->map(function ($beneficiary) {
return [
'id' => $beneficiary->bene_id,
'name' => $beneficiary->name,
'account' => $beneficiary->account,
'ifsc' => $beneficiary->ifsc,
'bank' => $beneficiary->bank,
'verificationDt' => $beneficiary->last_success_dt,
];
});
Now the confusing part is this mapping changed my $this->remitter too
after dumping $this->remitter again i get this
App\Models\IpayAppBc\Remitter {#1960
#connection: "ipay_app_bc__write"
+table: "remitter"
#attributes: array:16 [
"id" => 15016478
"name" => "ASKHSYA"
"name_last" => ""
"dob" => null
"mobile" => "1234567"
"limit_expiry" => "{"PB":"202212"}"
"address" => "PANCHKULA PANCHKULA HARYANA"
"city" => "PANCHKULA"
"pincode" => "134117"
"state" => "HARYANA"
"two_factor" => 0
"status" => 0
"otp_status" => 2
"paytm_key" => "h00097ea42dc-a6ed-42bc-b2cf-3b6a876f878b"
"ab_kyc" => 0
"beneficiaries" => Illuminate\Support\Collection {#1956
#items: array:2 [
0 => array:6 [
"id" => "898ddaeff5a54bf2f05a3ce00f5cd13a"
"name" => "DUMMY"
"account" => "37893810436"
"ifsc" => "SBIN0000001"
"bank" => "STATE BANK OF INDIA"
"verificationDt" => "2022-12-01 16:58:57"
]
1 => array:6 [
"id" => "c647579b5881ffc39cb2ffb1fc600634"
"name" => "MRAN DW"
"account" => "3789381043"
"ifsc" => "SBIN0000001"
"bank" => "STATE BANK OF INDIA"
"verificationDt" => null
]
]
#escapeWhenCastingToString: false
}
I am just assuming why this happened is there any referencing happening because as per my knowledge this only happens when I use referencing to any variable ?
any information on this please
Thanks

How to get getClientOriginalName() from Image of array on Laravel?

I Have An Array from request Like This.... I have order_details table, and i want to update column category, remark and foto data where have orderid and skuid that i get from request,...
array:7 [▼
"orderid" => "191121120129KUR"
"_token" => "1ov7aCP8JzzokWNfMpwntTH0Dv084HqwIqMUfLd1"
"orderdet" => array:5 [▼
0 => array:4 [▼
"skuid" => "10006"
"category" => array:2 [▼
0 => "1"
1 => "2"
]
"remark" => "remarks 1"
"foto" => array:2 [▼
0 => Illuminate\Http\UploadedFile {#348 ▶}
1 => Illuminate\Http\UploadedFile {#349 ▶}
]
]
1 => array:4 [▼
"skuid" => "10007"
"category" => array:1 [▼
0 => "1"
]
"remark" => "remarks 2"
"foto" => array:4 [▼
0 => Illuminate\Http\UploadedFile {#350 ▶}
1 => Illuminate\Http\UploadedFile {#351 ▶}
2 => Illuminate\Http\UploadedFile {#352 ▶}
3 => Illuminate\Http\UploadedFile {#353 ▶}
]
]
2 => array:2 [▼
"skuid" => "10008"
"remark" => null
]
3 => array:2 [▼
"skuid" => "10138"
"remark" => null
]
4 => array:2 [▼
"skuid" => "10078"
"remark" => null
]
]
"actioncomplaint" => "1"
"salahsiapa" => "3"
"remarkcust" => null
"remarkfresh" => "Dani"
]
I want to store name of image using getClientOriginalName(),...
How to get that name of images , this is my controller
foreach($request->orderdet as $row)
{
$data = OrderDetail::where('data_order_id', $request->orderid)->where('skuid', $row['skuid'])->first();
if(isset($row['category'])) {
$data->remarkscomplaint = $row['remark'];
$data->categorycomplaint = implode(',', $row['category']);
$data->fotocomplaint = $row['foto'];
$data->save();
}
}
Try something like:
$fotos = $request->only(['foto']);
$fotoNames = []
foreach($fotos as $foto) {
$fotoNames[] = $foto->getClientOriginalName();
}
$implodedFotos = implode(',' $fotoNames);
then
foreach($request->orderdet as $row) {
$data = OrderDetail::where('data_order_id', $request->orderid)->where('skuid', $row['skuid'])->first();
if(isset($row['category'])) {
dd($row['foto']); //Array of Image
$data->remarkscomplaint = $row['remark'];
$data->categorycomplaint = implode(',', $row['category']);
$data->fotocomplaint = $implodedFotos; // <- set your attribute with the variable we create with the names
$data->save();
}
}
I guess you could also do something like
$fotos = collect($request->orderset['foto'])
->map(function ($foto) {
return $foto->getClientOriginalName();
})
->implode(',');
$fotos->all(); // access the result
and then in order to save your images this will help you.
$foto->storeAs('path', 'filename');

Laravel Collection - Returning a total with sum() on nested arrays

I am trying to return a total using sum() by fetching the "duration" from each user within a $collection
The structure is
USER > CONTRACT_RESULTS > DAYS > DATE > ACTIVITIES > ACTIVITY
I want to query the "type" of activity and pull back the duration that is associated with that activity.
I had it working when it was much less nested using:
$collection->whereIn('activity',['off'])->sum('duration');
But I'm at a loss now since they are so nested and the names of the arrays are dynamic
Here is an example of the structure for 1 user in the $collection:
array:13 [▼
"user_id" => 52
"contract_results" => array:2 [▼
"2019-10-21 - 2019-10-31" => array:10 [▼
"hours_a_week" => "20"
"days" => array:1 [▼
"2019-10-21" => array:2 [▼
"bank_holiday" => "no"
"activities" => array:2 [▼
"10:00 - 22:00" => array:5 [▼
"type" => "driving"
"from" => "10:00"
"to" => "22:00"
"duration" => 720
"night_minutes" => 60
]
"22:00 - 00:00" => array:5 [▼
"type" => "driving"
"from" => "22:00"
"to" => "00:00"
"duration" => 120
"night_minutes" => 120
]
]
]
]
]
"2019-11-01 - 2019-11-10" => array:10 [▼
"hours_a_week" => "10"
"days" => array:1 [▼
"2019-11-01" => array:2 [▼
"bank_holiday" => "yes"
"activities" => array:1 [▼
"10:00 - 22:00" => array:5 [▼
"type" => "availability"
"from" => "10:00"
"to" => "22:00"
"duration" => 720
"night_minutes" => 60
]
]
]
]
]
]
]
I'm new to this so, thank you very much for your input!
Based on your unclear information, this is all that I can do:
$durationTotal = collect($data->get('contract_results'))
->transform(function ($contractResult) {
return array_merge((array) $contractResult, [
'days' => collect(data_get($contractResult, 'days'))
->transform(function ($day) {
return array_merge((array) $day, [
'activities' => collect(data_get($day, 'activities'))
->whereIn('type', ['driving'])
->toArray()
]);
})
->toArray()
]);
})
->sum(function ($item) {
return array_sum(data_get($item, 'days.*.activities.*.duration', []));
});

values are not being stored correctly in the array $items

I want to get the quantity of each registration type with the code below:
$registrationTypeDetails = Registration::with('participants:id,registration_type_id,registration_id')->find($regID);
$type_counts = [];
foreach ($registrationTypeDetails->participants as $p) {
$name = $p->registration_type->name;
if (!isset($type_counts[$name])) {
$type_counts[$name] = 0;
}
$type_counts[$name]++;
}
The dd($type_counts), if the conference has 2 registration types available (general and plus) and the user is doing a registration with 2 participants in the registration type "general" and 0 participants in the registration type "plus" shows:
array:2 [▼
"general" => 2
]
Then I need to make a post request to an API where is necessary to send in the request body the quantity of each registration type, in this case there is only 1 registration type "general" and the value for quantity should be "2".
So I have this code below the above code to create the array:
foreach ($registrationTypeDetails->participants as $registrationType) {
$items['invoice']['items'][] = [
'name' => $registrationType->registration_type->name,
'unit_price' => $registrationType->registration_type->price,
'quantity' => $type_counts[$registrationType->registration_type->name],
];
}
$create = $client->request('POST', 'https://...', [
'query' => ['api_key' => '...'], 'json' => $items,
]);
But then the output array is:
array:1 [▼
"invoice" => array:4 [▼
"client" => array:7 [▶]
"items" => array:2 [▼
0 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 2
]
1 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 2
]
]
]
]
Instead of only 1 item:
array:1 [▼
"invoice" => array:4 [▼
"client" => array:7 [▶]
"items" => array:2 [▼
0 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 2
]
]
]
]
$items shows:
array:1 [▼
"invoice" => array:4 [▼
"items" => array:2 [▼
1 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 2
]
2 => array:5 [▼
"name" => "plus"
"unit_price" => 0
"quantity" => 2
]
]
]
]
You would need to key the results of the items according to the registration type name:
foreach ($registrationTypeDetails->participants as $registrationType) {
$items['invoice']['items'][$registrationType->registration_type->name] = [
'name' => $registrationType->registration_type->name,
'unit_price' => $registrationType->registration_type->price,
'quantity' => $type_counts[$registrationType->registration_type->name],
];
}
Better is to use the id of the registration_type if there is one:
foreach ($registrationTypeDetails->participants as $registrationType) {
$items['invoice']['items'][$registrationType->registration_type->id] = [
'name' => $registrationType->registration_type->name,
'unit_price' => $registrationType->registration_type->price,
'quantity' => $type_counts[$registrationType->registration_type->name],
];
}
Explanation
As you loop over the participants linked to a registration type multiple participants could have the same registration type. So in your case you need to group the results according to registration type. Just adding to items[] appends to the items array. You want to group the results by registration type.
Fixing the 422 validation error
Your items is a valid array, but because it is translated to json the 1 and 1 would be keyed as an object. To force the key adjustment you can do the following:
// It is hacky but it re-keys the items array numerically.
$items['invoice']['items'] = array_values($list['invoice']['items']);

Resources