I´m trying to add a customField using the method subscribe
public function subscribe($userId, $leads, $listId)
{
$integration = $this->Integration->get($userId, self::platformName);
$config = json_decode($integration->config);
$batchData = [];
$this->addCustomFields($listId);
foreach ($leads as $lead) {
$name = explode(' ', $lead['name']);
$mergeFields = [
'FNAME' => array_key_exists(0, $name) ? $name[0] : $lead['name'],
'LNAME' => array_key_exists(1, $name) ? $name[1] : '',
'PHONE' => $lead['phone'],
];
foreach (self::customFieldsNames as $fieldName) {
$chosenCustomName = ($config['customFields'][$fieldName]['customName']);
$mergeFields[$chosenCustomName] = $lead[$fieldName];
}
array_push($batchData, [
'email_address' => \Carbon\Carbon::now()->timestamp.$lead['email'],
'status' => 'subscribed',
'email_type' => 'html',
'merge_fields' => $mergeFields,
]);
}
And it keeps giving me this error
"message": "Trying to access array offset on value of type null",
"exception": "ErrorException",
To be specific the error is on the line
$chosenCustomName = ($config['customFields'][$fieldName]['customName']);
But i don´t know how to solve this, the other methods to create and make a post in mailchimp with the new customField are already correct.
Related
I’m getting this error: Indirect modification of overloaded element of App\Models\ has no effect, change ->get(); to ->first->toArray(); another error Call to a member function toArray() on null
here the code
$penjualan = Penjualan::find(session('id_penjualan'));
$detail = PenjualanDetail::with('produk')->where('id_penjualan', session('id_penjualan'))->get();
$transaction = new Penjualan();
foreach ($detail as $item) {
$transaction_details = [
'order_id' => $penjualan->id_penjualan,
'gross_amount' => $penjualan->bayar,
];
$item_details = [
'id' => $penjualan->id_penjualan,
'price' => $item->harga_jual,
'quantity' => $item->jumlah,
'name' => $item->produk->nama_produk,
];
$customer_details = [
'first_name' => $request->get('uname'),
'last_name' => '',
'email' => $request->get('email'),
'phone' => $request->get('number'),
];
$transaction = [
'transaction_details' => $transaction_details,
'item_details' => $item_details,
'customer_details' => $customer_details,
];
}
$snapToken = Midtrans\Snap::getSnapToken($transaction);
$transaction->snap_token = $snapToken;
$transaction->save();
anyone could help me to fix this?
I have done Midtrans payment before.
The problem you've got with your current code are
you want to store snap_token inside your Penjualan model, but you use new Penjualan()
when multiple items are ordered, your code do not support it cause $item_details is a single array inside foreach loop
So here is an updated code with some notes and recommendations.
$id = session('id_penjualan');
$validated = $request->validate([
'uname' => 'required|string',
// 'lname' => 'nullable|string',
'email' => 'required|string',
'number' => 'required|string',
]);
// use single query to retrieve Penjualan and PenjualanDetail
// for product name, recommend to store it within PenjualanDetail, along with price and base_price
$penjualan = Penjualan::query()
->with('details')
->find($id);
if (!$penjualan) {
// you can redirect your customer to cart or any other page
return redirect('cart')->with('error', 'Can not find Penjualan.');
}
$item_details = [];
foreach ($penjualan->details as $item) {
$item_details[] = [
'id' => $item->id,
'price' => $item->harga_jual,
'quantity' => $item->jumlah,
// recommended product's name to be stored within PenjualanDetail
'name' => $item->nama_produk,
];
}
$transaction_details = [
'order_id' => $penjualan->id_penjualan,
// "bayar" must not contain any decimal, use cast Money
'gross_amount' => $penjualan->bayar,
];
// Optional
$customer_details = [
'first_name' => $validated['uname'],
'last_name' => '', // $validated['lname'],
'email' => $validated['email'],
'phone' => $validated['number'],
// 'billing_address' => '',
// 'shipping_address' => '',
];
// this is a simple array that will be sent to method getSnapToken()
$transaction = [
'transaction_details' => $transaction_details,
'customer_details' => $customer_details,
'item_details' => $item_details,
];
// this method only accept good old array
$snapToken = Midtrans\Snap::getSnapToken($transaction);
$penjualan->snap_token = $snapToken;
// recommend to store your customer detail here, either multiple fields or single array field storing $customer_details
// $penjualan->first_name = $customer_details['first_name'];
// $penjualan->last_name = $customer_details['last_name'];
// $penjualan->email = $customer_details['email'];
// $penjualan->phone = $customer_details['phone'];
// $penjualan->billing_address = $customer_details['billing_address'];
// $penjualan->shipping_address = $customer_details['shipping_address'];
$penjualan->save();
I commented out some parts that you might not use, where I did used them in the past.
Feel free to adjust them to suite your needs.
public function getSnapToken ()
{
$id = session('id_penjualan');
$member = Member::orderBy('nama')->get();
$penjualan = Penjualan::with(['penjualan_detail' => $detail = function ($query) {
$query->where('item', 'kode_produk, subtotal, jumlah, nama_produk');
}])->where('id_penjualan', ('id_penjualan'))
->orderBy('id_penjualan')
->get();
$transaction = array($penjualan);
foreach ( $detail as $item ) {
$transaction_details = [
'order_id' => $this->penjualan->id_penjualan,
'gross_amount' => $this->penjualan->bayar,
];
$item_details = [
'id' => $item->produk->kode_produk,
'price' => $item->jumlah * $item->subtotal,
'quantity' => $item->jumlah,
'name' => $item->produk->nama_produk,
];
$customer_details = [
'id' => $penjualan->member->kode_member,
'first_name' => $validated['uname'],
'last_name' => $validated['lname'],
'email' => $validated['email'],
'phone' => $validated['number'],
// 'billing_address' => '',
// 'shipping_address' => '',
];
$transaction = [
'transaction_details' => $transaction_details,
'item_details' => $item_details,
'customer_details' => $customer_details,
];
}
$snap_token = '';
try {
$snap_token = \Midtrans\Snap::getSnapToken($transaction);
}
catch (\Exception $e) {
echo $e->getMessage();
}
echo "snap_token = ".$snap_token;
}
public function payment(Penjualan $penjualan)
{
$snap_token = $penjualan->snap_token;
if (is_null($snap_token)) {
$midtrans = new CreateSnapTokenService($penjualan);
$snap_token = $midtrans->getSnapToken();
$snap_token = Penjualan::where('id_penjualan')->update(['snap_token' => $snap_token]);
}
return view('penjualan.payment', ['snap_token'=>$snap_token]);
}
Hi I am using RaveFlutterWave as my payment gateway. I want to store an orders whenever a customer finish making payment but I couldn't get pass that error. I don't know what am missing there.
Thanks for your help, here is my code.
public function callback(Request $request)
{
// $data = Rave::verifyTransaction(request()->txref);
$resp = $request->resp;
$body = json_decode($resp, true);
$txRef = $body['data']['data']['txRef'];
$data = Rave::verifyTransaction($txRef);
return redirect()->route('success');
}
Here is my route
Route::get('/success', 'RaveController#addToOrdersTables')->name('success');
and this is my method for saving the order
protected function addToOrdersTables($request, $error)
{
$order = Order::create([
'user_id' => auth()->user() ? auth()->user()->id : null,
'billing_email' => $request->email,
'billing_first_name' => $request->first_name,
'billing_last_name' => $request->last_name,
'billing_address' => $request->address,
'billing_city' => $request->city,
'billing_town' => $request->town,
'billing_postalcode' => $request->postalcode,
'billing_phone' => $request->phone,
'billing_total' => Cart::getTotal(),
'error' => $error,
]);
foreach (Cart::getContent() as $item)
{
OrderProduct::create([
'order_id' => $order->id,
'product_id' => $item->model->id,
'quantity' => $item->quantity,
]);
}
}
Thanks for concern.
You have to pass parameter through route as well:
Route::get('/success/{error}', 'RaveController#addToOrdersTables')->name('success');
And In addToOrdersTables method type hint the request using Request Like given below:
protected function addToOrdersTables(Request $request, $error)
This is the my back-end code. I wrote the test for this function as follows:
public function index(Request $request)
{
$fields = 'in:' . implode(',', Schema::getColumnListing('suppliers'));
$messages = [
'order_by.in' => 'The selected column name is invalid.',
];
$this->validate($request, [
'page' => ['numeric'],
'per_page' => ['numeric'],
'order_direction' => ['in:desc,asc,DESC,ASC'],
'order_by' => [$fields],
], $messages);
$perPage = 10;
$filter = '';
$orderBy = 'id';
$orderDirection = 'DESC';
try {
if ($request->has('per_page')) $perPage = (int)$request->per_page;
if ($request->has('filter')) $filter = $request->filter;
if ($request->has('order_by')) $orderBy = $request->order_by;
if ($request->has('order_direction')) $orderDirection = strtoupper($request->order_direction);
$suppliers = Supplier::select('id', 'firstname', 'lastname', 'phone', 'email', 'custom_field_1', 'custom_field_2')->where('store_id', Auth::user()->store);
if (!!$filter) {
$suppliers->where('id', 'LIKE', "%{$filter}%")
->orWhere('firstname', 'LIKE', "%{$filter}%")
->orWhere('lastname', 'LIKE', "%{$filter}%")
->orWhere('email', 'LIKE', "%{$filter}%")
->orWhere('phone', 'LIKE', "%{$filter}%");
}
$suppliers->orderBy($orderBy, $orderDirection);
$suppliers = $suppliers->paginate($perPage);
return response()->json([
'success' => true,
'data' => $suppliers->toArray(),
], Response::HTTP_OK);
} catch (Exception $e) {
report($e);
return serverExceptionMessage();
}
}
This is what I have tried and test. I'm creating a store because store_id is a foreign key in my supplier table and adding suppliers dynamically and filtering the firstname through the URL and getting the result from seeJson as an array:
public function testSupplierListViewPaginationFilterTest()
{
$user = factory('App\Models\User')->make();
$store = (factory('App\Models\Store')->make())->getAttributes();
$this->actingAs($user)
->post('/create-new-store', $store)
->seeStatusCode(Response::HTTP_OK);
$attributes = [
'id' => 1,
'firstname' => 'ashid',
'lastname' => 'mhd',
'phone' => 776358547,
'email' => 'ashid#email.com',
'custom_field_1' => 'test',
'custom_field_2' => 'test',
];
$user->store = Store::latest()->first()->id;
$attributes['store_id'] = Auth::user()->store;
$supplier = Supplier::create($attributes);
$this->actingAs($user)
->get('/suppliers?filter=' . $supplier['firstname'], $attributes)
->seeStatusCode(Response::HTTP_OK)->seeJson([
'success' => true,
"data" =>
[
'id' => 1,
'firstname' => 'ashid',
'lastname' => 'mhd',
'phone' => 776358547,
'email' => 'ashid#email.com',
'custom_field_1' => 'test',
'custom_field_2' => 'test',
]
]);
}
I'm getting following error:
1) SupplierTest::testSupplierListViewPaginationFilterTest
Unable to find JSON fragment
["data":"custom_field_1":"test","custom_field_2":"test","email":"ashid#email.com","firstname":"ashid","id":1,"lastname":"mhd","phone":776358547}] within [{"data":{"current_page":1,"data":[{"custom_field_1":"test","custom_field_2":"test","email":"ashid#email.com","firstname":"ashid","id":1,"lastname":"mhd","phone":"776358547"}],"first_page_url":"http:\/\/localhost\/suppliers?page=1","from":1,"last_page":1,"last_page_url":"http:\/\/localhost\/suppliers?page=1","next_page_url":null,"path":"http:\/\/localhost\/suppliers","per_page":10,"prev_page_url":null,"to":1,"total":1},"success":true}].
Failed asserting that false is true.
I need a best solution for this problem .
So, I have a function that creates me alerts based on changes made on a profile.And I need to have customizable alerts for every field, like name, city, email etc.Now, I need to make this working and I also need to show me an alert like ... User x changed his name to y, or User z changed his email to w.
Here is my controller
public function generalinfo(Request $request)
{
if ( $user = Sentinel::getUser() ) {
if ( $user->inRole('individuals') ) {
$data = $this->data;
$id = $user->id;
$user = User::findOrfail($id);
$generalinfo = User::firstOrNew(array('email' => $user->email ));
$dob = date("Y-m-d", strtotime($request->get('dob')));
$generalinfo->name = $request['name'];
$generalinfo->city = $request['city'];
$generalinfo->address = $request['address'];
$generalinfo->email = $request['email'];
$generalinfo->website1 = $request['website1'];
$generalinfo->website2 = $request['website2'];
$generalinfo->website3 = $request['website3'];
$generalinfo->save();
$feed = \App\Feed::create([
'user_id' => $id,
'feed_url' => $request['rss'],
'active' => 1
]);
$generalinfo = User::find($id);
$generalinfo->name = 'NewName';
// other $user changes
foreach ($generalinfo->getDirty() as $key => $attribute) {
$log = \App\Log::create([
'user_id' => $id,
'log' => " {$key} changed to {$user->user}.",
'date' => time(),
'type' => 'profile_updates',
'formatted_date' => date_format(date_create(date('Y-m-d')),"F dS, Y"),
'action_user_id' => $id,
'action_name' => $user->name,
'action_username' => $user->username,
'profile_picture' => $user->profile_picture,
]);
}
$generalinfo->save();
}
}
}
This is my problem : $generalinfo->name = 'NewName'; .I need to get the new value from database from $user->name. The problem is, I tried to use this : $generalinfo->name = $request['name']; but with no effect.
I using laravel 5.3 with PostgreSQL and i have code like this
public function store(array $request)
{
DB::transaction(function () use ($request) {
$phoneData = [
'phone_no' => $request['phone'],
'created_by' => $request['sdm_employee_id'],
'updated_by' => $request['sdm_employee_id'],
'created_stamp' => Carbon::now()->toDateString(),
'updated_stamp' => Carbon::now()->toDateString(),
];
$phone = DonorPhone::create($phoneData);
if (!$phone) {
throw new \Exception('Phone not created for account');
}
$data = [
'phone_id' => $phone->id,
'full_name' => $request['name'],
'address' => $request['address'],
'email' => array_key_exists('email', $request) ? $request['email'] : null,
'register_date' => Carbon::now()->toDateString(),
'marketer_id' => (int) $request['sdm_employee_id'],
'created_by' => (int) $request['sdm_employee_id'],
'updated_by' => (int) $request['sdm_employee_id'],
'company_id' => 2,
'user_status' => 1,
'note' => 'Add donor from sales track apps',
'phone_no_full' => $request['phone'],
'created_stamp' => Carbon::now()->toDateString(),
'updated_stamp' => Carbon::now()->toDateString(),
];
$contact = self::create($data);
if (!$contact) {
throw new \Exception('Contact not created');
}
$phone->user_id = (int)$contact->id;
$phone->save();
$target = new Target;
$target->updateAchievement($data['sdm_employee_id'], $this->tragetType, Carbon::now()->year, Carbon::now()->month);
throw new \Exception('Contact not created');
});
}
When create contact i got error
SQLSTATE[42883]: Undefined function: 7 ERROR: function php_donor_phone_get_all(integer) does not exist\nLINE 1: SELECT php_donor_phone_get_all(NEW.id)\n ^\nHINT: No function matches the given name and argument types. You might need to add explicit type casts.\nQUERY: SELECT php_donor_phone_get_all(NEW.id)\nCONTEXT: PL\/pgSQL function public.php_donor_tr_full_text() line 6 at assignment (SQL: insert into \"public\".\"php_donor_personal\" (\"full_name\", \"address\", \"email\", \"register_date\", \"marketer_id\", \"created_by\", \"updated_by\", \"company_id\", \"user_status\", \"phone_no_full\", \"created_stamp\", \"updated_stamp\") values . . .
I check for function php_donor_phone_get_all is exist and my database username have a grant to execute this function. This seems to relate to a specific data type when calling the function, passing not an integer, but I do not know how to ensure that data in passing is an integer. There is a hint for me? Please help.