fix double payments - double purchases - php

i'm implementing an online payment platform similar to paypal, the problem is that when they click on the buy button 2 times or more quickly and this causes the payment to register twice instead of once
When you click buy execute this action:
public function invoke(Request $request) {
$payment_id = $request->get('payment_id');
$credenciales = config('services.mercadopago.token');
$request->get('user_id'));
$response = Http::get("https://api.mercadopago.com/v1/payments/$payment_id" . "?access_token=$credenciales");
$response = json_decode($response);
$request->session()->put('order', $response->order->id);
$request->session()->put('ingreso', $response->transaction_details->net_received_amount);
$request->session()->put('monto', $response->transaction_details->total_paid_amount);
$request->session()->put('method', $response->payment_type_id);
$status = $response->status;
If the answer is approved run this:
if($status == 'approved') {
Income::insert([
'user_id' => Session::get('user_id'),
'evento_id' => Session::get('variableName'),
//Guardar el personal seleccionado
'mp_id' => Session::get('order'),
'metodo' => Session::get('metodo'),
'monto' => Session::get('monto'),
'rrpp_id' => Session::get('rrpp'),
'ingreso' => Session::get('ingreso'),
]);
OrderNew::insert([
'user_id' => Session::get('user_id'),
'dia_id' => Session::get('variableName'),
//Guardar el personal seleccionado
'whatsapp' => Session::get('telefono'),
'cantidad' => Session::get('cantidad'),
'anticipada' => Session::get('anticipada'),
'horario' => Session::get('horario'),
'rrpp' => Session::get('rrpp'),
'pagado' => '1',
'tipo' => 'vip',
'codigo' => rand(1580, 4005),
]);
in the first model I register the incoming money and in the second model I register the customer's order
and there is the problem, if they click on Buy several times, the records are duplicated and they get free products
How can I limit or solve this problem?

Instead of Insert use updateOrCreate or maybe firstOrCreate that accepts 2 parameters, first one an array to check the values against database records and second to insert/find data. If the records match, it will be updated/retrieved, otherwise a new one will be created. Please check Laravel docs for updateOrCreate or firstOrCreate.
In your case, for example:
Income::firstOrCreate([
'user_id' => Session::get('user_id'),
'evento_id' => Session::get('variableName'),
//Guardar el personal seleccionado
'mp_id' => Session::get('order'),
'metodo' => Session::get('metodo'),
'monto' => Session::get('monto'),
'rrpp_id' => Session::get('rrpp'),
'ingreso' => Session::get('ingreso'),
]);
This will check the exact records and if it finds a match it retrieves the data, otherwise creates one which avoids duplicated entries.
However, since your second query has a random number generated, it will not find a match if we pass only one arguement, therefore, as a first arguement we need to pass the array to be checked against the database (all data, except for the rand() part).
OrderNew::firstOrCreate(
[
'user_id' => Session::get('user_id'),
'dia_id' => Session::get('variableName'),
//Guardar el personal seleccionado
'whatsapp' => Session::get('telefono'),
'cantidad' => Session::get('cantidad'),
'anticipada' => Session::get('anticipada'),
'horario' => Session::get('horario'),
'rrpp' => Session::get('rrpp'),
'pagado' => '1',
'tipo' => 'vip',
],
[
'user_id' => Session::get('user_id'),
'dia_id' => Session::get('variableName'),
//Guardar el personal seleccionado
'whatsapp' => Session::get('telefono'),
'cantidad' => Session::get('cantidad'),
'anticipada' => Session::get('anticipada'),
'horario' => Session::get('horario'),
'rrpp' => Session::get('rrpp'),
'pagado' => '1',
'tipo' => 'vip',
'codigo' => rand(1580, 4005),
]);
According to your data, you can change the array of data to be checked against the database.

Specify that the route should not allow concurrent requests from the same session.
Route::get('/deposit',ExampleController::class)->block(10);

Related

Laravel Stripe Setup

okay, this is my first time to ask a question here so please give grace if it's not very clear. Anyway, I have this code in Laravel Billing.php.
Is this correct? Whenever a new customer is created, it doesn't have it's user email address but instead this unknown#domain.com was assigned to the user.
This was set by my previous developer. But ever since we hired him for just simple fix, we've had numerous issues with the site.
$stripeCustomer = StripeCustomer::create([
'email' => $currentCustomer->email ? $currentCustomer->email : 'unknown#domain.com',
'description' => $company->name,
'metadata' => [
'company_id' => $company->id,
'card_owner_email' => $currentCustomer->email ? $currentCustomer->email : false,
'company_name' => $company->name,
],
]);
You can remove customer email from the StripeCustomer when creating since stripe API said that email field of customer is optional. Here is the reference link
Here what you should fix:
$customerObject = [
'description' => $company->name,
'metadata' => [
'company_id' => $company->id,
'company_name' => $company->name,
],
];
if ($currentCustomer->email) {
$customerMetadata["metadata"]["card_owner_email"] = $currentCustomer->email;
$customerObject["email"] = $currentCustomer->email;
}
$stripeCustomer = StripeCustomer::create($customerObject);

How to get a specific value from sql into a php file which (the value) is passed through angular js

$data = [
'merchantId' => 'M100003360', // To be replace by integration merchant ID
'qrCode' => '968566222112615382',
'curType' => 'RM',
'notifyURL' => 'https://google.com/',
'merOrderNo' => uniqid(),
'goodsName' => ,
'orderAmt' => 'This is where I want to pull the value from sql',
'remark' => '',
'transactionType' => '1',
];
$data = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
But I would only want to get a specific value so is there a way to filter out?? Without using WHERE clause as I want it to be dynamic.

mongoDB, PHP update specific value not all the values

I am having a problem in updating values i get from web service ..
$collection = $modb->$table;
$collection->update(array("id" => (int)$row['id']),
array('$set' => array(
"user_id" => (int)$post_data_array['user_id'],
"story" => (int)$post_data_array['story'],
"surprize_sub1" => (int)$post_data_array['surprize_sub1'],
"surprize_sub2" => (int)$post_data_array['surprize_sub2'],
"surprize_sub3" => (int)$post_data_array['surprize_sub3'],
"exr_solve" => (int)$post_data_array['exr_solve'],
"exr_assessmnt" => (int)$post_data_array['exr_assessmnt'],
"exr_refresh" => (int)$post_data_array['exr_refresh'],
"sound_control" => (int)$post_data_array['sound_control'],
"clock_control" => (int)$post_data_array['clock_control'],
"switch_user" => (int)$post_data_array['switch_user'],
"exr_print" => (int)$post_data_array['exr_print'],
"write_on_wall" => (int)$post_data_array['write_on_wall'],
"switch_letter" => (int)$post_data_array['switch_letter'],
"view_controls" => (int)$post_data_array['view_controls'],
)));
I get these values from end users.. i want the specific field sent to be updated without loosing all the rest of data ..
in this code only sent data is set while removing the rest .. i want to change only sent ones by keeping the rest as they are, please advice
you need to use updateOne instead of update .
updateOne
Use the MongoDB\Collection::updateOne() method to update a single document matching a filter.
$collection = $modb->$table;
$collection->updateOne(array("id" => (int)$row['id']),
array('$set' => array(
// .... array elements
)));

Updating 1 group through MailChimp API v2.0 while multiple is created

I have list which consists of multiple groups, with batchSubscribe I want to update only single group in list, is that possible or every update should contain information about other groups also?
replace_interest must be set to true (default)
this is my code for batchSubscribe:
foreach ($company->users as $user) {
if ($this->isValidAdmin($user)) {
$subscribers[] = ['email' => ['email' => "$user->email"],
'email_type' => 'html',
'merge_vars' => ['fname' => $user->first_name,
'lname' => $user->last_name,
'mc_language' => $user->lang,
'groupings' => [
['id' => Config::get('mailchimp.companies.status'),
'groups' => [$company->status]]]]
];
}
$subResult = $this->mailchimp->lists->batchSubscribe($adminList, $subscribers, false, true);
Or it is not possible, and I need to include info about all groups with all API requests I make?
In v2, you will need to provide all group data, there's no way to do partial updates. In v3 of the API this is possible, though.

Saving one-to-many relationship to DB (laravel)

I'm trying to save a relationship the way I saw on the docs, but isn't working.
On my Contato (Contact) model I've:
public function filhoContato()
{
return $this->hasMany('FilhoContato', 'id_contato');
}
Along with my fillables to enable mass-assignment
My FilhoContato (ContactChildren) model:
public function contato()
{
return $this->belongsTo('Contato');
}
And on my controller:
$contato = Contato::create(array(
'nome' => Input::get('nome'),
'nascimento' => $data,
'cpf' => Input::get('cpf'),
'tel_principal' => Input::get('telefone'),
'idade' => Input::get('idade'),
'email' => Input::get('email'),
'tipo_end' => Input::get('tipo'),
'cep' => Input::get('cep'),
'estado' => Input::get('estado'),
'cidade' => Input::get('cidade'),
'bairro' => Input::get('bairro'),
'rua' => Input::get('rua'),
'numero' => Input::get('numero'),
'logradouro' => Input::get('logradouro'),
'genero' => Input::get('genero'),
'estadoCivil' => Input::get('estadoCivil'),
'mae' => Input::get('mae'),
'pai' => Input::get('pai'),
'filhos' => Input::get('filhos'),
'grupo' => Input::get('grupo'),
'caminho' => $filename . $extension,
'ativo' => Input::get('ativo'),
'exaluno' => Input::get('exaluno')
));
$filhocontato = new FilhoContato(array('nome' => Input::get('name')));
$contato = Contato::find(1);
$filhocontato = $contato->filhoContato()->save($filhocontato);
However only the Contato (contact) table data gets inserted.
How can I save both tables at same time? I need a loop to get all value from Input::get('name') too.
Just to clarify, if my Contato(contact) has a children then he will insert their names in a dynamically generated form text field and when he clicks submit all of his data will go to Contato(contact) table and his children (if he has) will go to filhocontato table which structure is (id, nome, id_contato). That's what I'm aiming for at least :/

Categories