I need a little help with implementing payment getaway in Laravel shop.
Payment I use is https://gourl.io/ and I can't understand how to take needed information. So I have set the files database table, database connection and all.. Now I'm trying to redirect user to payment.php page after order form is submitted. This is my CartController.php orderSubmit function
public function orderSubmit() {
$cart = Session::get(self::CART_SESSION_KEY, array());
if (count($cart) < 1) {
return Redirect::to('/');
}
$validatorRules = array(
'captcha' => 'required|captcha',
'shipping_address' => 'required|min:10',
'shipping_method' => 'required|in:' . implode(',', [Settings::SETTINGS_SHIPPING_NORMAL, Settings::SETTINGS_SHIPPING_EXPRESS])
);
Input::merge(array_map('trim', Input::all()));
$validator = Validator::make(Input::all(), $validatorRules);
if ($validator->fails()) {
return Redirect::to('/cart/order?_token=' . csrf_token())->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}
$shipping = array(
'quantity' => 1,
'image' => '/img/noimage.png',
'description' => '',
'title' => 'FIX ME', // this should never occur,
'price' => 100000 // this should never occur
);
switch (Input::get('shipping_method')) {
case Settings::SETTINGS_SHIPPING_NORMAL:
$shipping['title'] = 'Normal Delivery';
$shipping['price'] = 0;
break;
case Settings::SETTINGS_SHIPPING_EXPRESS:
$shipping['title'] = sprintf('Express Delivery - $%.2f', Settings::getOption('express_shipping_cost'));
$shipping['price'] = doubleval(Settings::getOption('express_shipping_cost'));
break;
}
$cart['shipping'] = $shipping;
$order = new Order();
$order->user_id = self::$user->user_id;
$order->data = json_encode($cart);
$order->address = Input::get('shipping_address');
$order->pgp_key = Input::get('gpgkey');
$order->info = Input::get('additional_info');
$order->save();
Session::put(self::CART_SESSION_KEY, array());
return Redirect::to('payment.php')->with('message_success', 'Order created! We will contact you shortly to confirm your order and payment details.');
}
and this is payment.php
require_once( "../cryptobox.class.php" );
/**** CONFIGURATION VARIABLES ****/
$userID = ""; // place your registered userID or md5(userID) here (user1, user7, uo43DC, etc).
// you don't need to use userID for unregistered website visitors
// if userID is empty, system will autogenerate userID and save in cookies
$userFormat = ""; // save userID in cookies (or you can use IPADDRESS, SESSION)
$orderID = "";
$amountUSD = 20;
$period = "NOEXPIRY";
$def_language = "en";
$public_key = "mypublickey";
$private_key = "myprivatekey";
/** PAYMENT BOX **/
$options = array(
"public_key" => $public_key, // your public key from gourl.io
"private_key" => $private_key, // your private key from gourl.io
"webdev_key" => "", // optional, gourl affiliate key
"orderID" => $orderID, // order id or product name
"userID" => $userID, // unique identifier for every user
"userFormat" => $userFormat, // save userID in COOKIE, IPADDRESS or SESSION
"amount" => 0, // product price in coins OR in USD below
"amountUSD" => $amountUSD, // we use product price in USD
"period" => $period, // payment valid period
"language" => $def_language // text on EN - english, FR - french, etc
);
// Initialise Payment Class
$box = new Cryptobox ($options);
// coin name
$coinName = $box->coin_name();
// Successful Cryptocoin Payment received
if ($box->is_paid())
{
if (!$box->is_confirmed()) {
$message = "Thank you for payment (payment #".$box->payment_id()."). Awaiting transaction/payment confirmation";
}
else
{ // payment confirmed (6+ confirmations)
// one time action
if (!$box->is_processed())
{
// One time action after payment has been made/confirmed
$message = "Thank you for order (order #".$orderID.", payment #".$box->payment_id()."). We will send soon";
// Set Payment Status to Processed
$box->set_status_processed();
}
else $message = "Thank you. Your order is in process"; // General message
}
}
else $message = "This invoice has not been paid yet";
$languages_list = display_language_box($def_language);
My question is how to take the correct info in the payment.php? How to take userID, userFormat, orderID and so on?
First of all, I would suggest you use Laravel as the framework it is intended for. In Laravel you define controllers to handle your http-requests. Make a new PaymentController and put the code from payment.php into this controller. Then make a route to that controller-method.
Also put your configuration settings in Laravels config-folder.
And the require_once( "../cryptobox.class.php" ); can be replaced by a dependency injection in your controllers constructor.
Now back to your question.
$userID is where you put your registered Laravel user ID. (If you dont have any registered users, leave it blank). Why you should put your user's id in this variable? -It helps to keep track of which users have done which payments. You can later save this information in your database if you want to keep track of payment history.
$orderID This is where you put your internal order id. Why should you use an internal order id? -Again its to keep track of which purchases of which products have been done by which users. You can store your order-id in your database together with user-id and product-id to get a purchase history log.
$userFormat This is how you wish to store your user information, session, cookie, etc. Because when the purchase is executed, the payment gateway needs a way to access this information, and therefor it must be stored in the session or in a cookie.
I would use $_SESSION['$value'] if you use session for your users!
Related
I am trying to have an email send when a user is added to an ACF user field, I don't want existing users that are in the field already to be notified. Below are a few options I have tried, but am not having luck with. Any assistance would be greatly appreciated.
Option 1:
add_action( 'acf/save_post', 'my_acf_save_hook_email'); function my_acf_save_hook_email( $post_id ) {
// // Get the list of user ids that are currently in the ACF field
$current_pds_project_managers = get_field('pds_project_manager');
// Get the list of user ids that were just added to the ACF field
$added_pds_project_managers = $_POST['pds_project_manager'];
// Get the list of user ids that are newly added, and not already in the ACF field
$newly_added_pds_project_managers = array_diff($added_pds_project_managers, $current_pds_project_managers);
// Send an email to each newly added user
foreach ($newly_added_pds_project_managers as $user) {
$user_email = $user['user_email'];
wp_mail( $user_email, 'You have been added to a project', 'You can now access the project from your dashboard.' );
}
}
Option 2: (this ends up sending 2 emails to the existing users in the field and one email to the new user, I then started with option one to see if I could only get the new users to no avail)
add_action( 'acf/save_post', 'my_acf_save_hook_email', 5); function my_acf_save_hook_email( $post_id ) {
// bail early if no pds_project_manager
if( empty('pds_project_manager') ) {
return; }
// Get the value of the ACF field
$project_managers = get_field('pds_project_manager', '');
// Create an empty array to store the emails
$emails_to_send = array();
// Loop through the project managers
foreach ($project_managers as $project_manager) {
// Get the user data
$user_email = $project_manager['user_email'];
$user_id = $project_manager['ID'];
// Check if the user already exists in the array
if (!in_array($user_id, $project_managers)) {
// Add the user to the emails to send
$emails_to_send[] = $user_email; } }
// Send emails to the users
foreach ($emails_to_send as $email_to_send) {
// Send the email
wp_mail($email_to_send, 'You Have Been Added To A Project', 'You have been added to a project. Please check the project page for more information.'); } }
I'm trying to use Laravel transactions for an operation that requires a change in customer's balance.
In this specific situation, when an already approved (status = 1) process is edited, the update operation must subtract the current process amount and add the new process amount to the customer's wallet.
This is the current code:
$process = Process::findOrFail($id);
// CHECKS THE CURRENT PROCESS SUBSIDY
$old_subsidy = $process->subsidy;
try {
DB::beginTransaction();
$process->update($request->all());
// SUMS NEW PROCESS SUBSIDY ONLY WHEN PROCESS IS ALREADY APPROVED
if($process->status == 1) {
$customer = Customer::findOrFail($process->customer_id);
$customer->balance = $request->subsidy - $old_subsidy;
$customer->save();
}
DB::commit();
$notification = array(
'title' => __('Success'),
'message' => __('Process updated with success'),
'class' => 'bg-success'
);
return redirect()->route('processes.show',$process->id)->with($notification);
} catch (\Exception $e) {
DB::rollback();
$notification = array(
'title' => __('Erro'),
'message' => __('Ocorreu um erro'),
'class' => 'bg-danger'
);
return redirect()->back()->with($notification);
}
To test, i'm forcing an error, on this line, requesting a customer id that doesn't exist, like: $customer = Customer::findOrFail(99999). The error message is shown but the changes in the process are still being saved instead of roled back.
Am i missing something here?
Thanks in advance!
I want to delete the admin user by just make him password and email as empty Because I need him rest Information in the database
so when i need to delete him I want to change password and email as empty and the same time I want to log him out of the system immediately
every thing works fine but without log him out
here is delete function
public function DeleteAdminRole($id){
$adminimg = Admin::findOrFail($id);
$img = $adminimg->profile_photo_path;
if($img){
unlink($img);
}
Auth::logout($adminimg);
Admin::findOrFail($id)->update([
'type' =>3,
'email'=>"",
'password'=>"",
]);
$notification = array(
'message' => 'Admin User Deleted Successfully',
'alert-type' => 'info'
);
return redirect()->back()->with($notification);
} // end method
I am using Moodle 2.7 and in the Quiz activity there is the overview page for all attempts of the learners.
The table is under mymoodle/mod/quiz/report.php?id=50&mode=overview
Right now only admin users or users with the capability 'mod/quiz:viewreports' can see the table.
How to add users, without using any capability, who will be able to see this report?
Right now every user, without the capability gets the error from report.php:
$reportlist = quiz_report_list($context);
if (empty($reportlist) !totara_is_manager($userid)) {
print_error('erroraccessingreport', 'quiz');
}
// Validate the requested report name.
if ($mode == '') {
// Default to first accessible report and redirect.
$url->param('mode', reset($reportlist));
redirect($url);
} else if (!in_array($mode, $reportlist)) {
print_error('erroraccessingreport', 'quiz');
}
if (!is_readable("report/$mode/report.php")) {
print_error('reportnotfound', 'quiz', '', $mode);
}
The table function is under reportlib.php:
function quiz_report_list($context) {
global $DB;
static $reportlist = null;
if (!is_null($reportlist)) {
return $reportlist;
}
$reports = $DB->get_records('quiz_reports', null, 'displayorder DESC', 'name, capability');
$reportdirs = core_component::get_plugin_list('quiz');
// Order the reports tab in descending order of displayorder.
$reportcaps = array();
foreach ($reports as $key => $report) {
if (array_key_exists($report->name, $reportdirs)) {
$reportcaps[$report->name] = $report->capability;
}
}
// Add any other reports, which are on disc but not in the DB, on the end.
foreach ($reportdirs as $reportname => $notused) {
if (!isset($reportcaps[$reportname])) {
$reportcaps[$reportname] = null;
}
}
$reportlist = array();
foreach ($reportcaps as $name => $capability) {
if (empty($capability)) {
$capability = 'mod/quiz:viewreports';
}
if (has_capability($capability, $context)) {
$reportlist[] = $name;
}
}
return $reportlist;
}
I want to add designated people by their id, who will act as managers.
If you want to completely bypass the capabilities' mechanism for viewing reports, then you could always comment the array values in access.php corresponding to the key 'mod/quiz:viewreports'. In other words, you can go to /mod/quiz/db/access.php and substitute
// View the quiz reports.
'mod/quiz:viewreports' => array(
'riskbitmask' => RISK_PERSONAL,
'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),
with
// View the quiz reports.
'mod/quiz:viewreports' => array(
// 'riskbitmask' => RISK_PERSONAL,
// 'captype' => 'read',
// 'contextlevel' => CONTEXT_MODULE,
// 'archetypes' => array(
// 'teacher' => CAP_ALLOW,
// 'editingteacher' => CAP_ALLOW,
// 'manager' => CAP_ALLOW
)
),
or, alternatively, you can tune or turn on the entries according to your necessities. For more information see:
https://docs.moodle.org/dev/Access_API
Then you can
check the ID of the current user ($USER->id) and
write some custom function to decide if this user can or cannot see the report.
Note: I would not bypass the capabilities mechanism, though, because it is reliable and safe. You could however tune it in order to allow only user groups defined by you.
When user place his order it redirects him to another page which check if he made his payment or no. Now The controller is ready and I need to initialize the box which make this and I need to took some variables.. I don't know how to implement this into payment.blade.php. This is what I tried and doesn't seems to work.
This is the controller
public function paymentView( $orderId, $userId ) {
$order = Session::all();
$order = Order::where('order_id', $orderId)->first();
if (!$order) {
App::abort(404);
}
$userID = $order['user_id'];
$orderID = $order['order_id'];
$options = array(
...
);
// Initialise Payment Class
$box = new Cryptobox ($options);
// coin name
$coinName = $box->coin_name();
// Successful Cryptocoin Payment received
if ($box->is_paid())
{
if (!$box->is_confirmed()) {
$message = "Thank you for payment (payment #".$box->payment_id()."). Awaiting transaction/payment confirmation";
}
else
{
if (!$box->is_processed())
{
$message = "Thank you for order (order #".$orderID.", payment #".$box->payment_id()."). We will send soon";
$box->set_status_processed();
}
else $message = "Thank you. Your order is in process"; // General message
}
}
else $message = "This invoice has not been paid yet";
$languages_list = display_language_box($def_language);
return View::make('site.cart.payment', [
'order' => $order,
]);
}
Now I'm trying to put this in my view
#section('content')
#foreach($order->getOrderData(true) as $productId => $item)
<?php if (!$box->is_paid()) echo "<h2>Pay Invoice Now - </h2>"; else echo "<br><br>"; ?>
<?php echo $box->display_cryptobox(true, 580, 230); ?>
#endforeach
#endsection
When I run the page I've got error that box isn't defined.. How can I take this info in the view?
In the same way you've passed $order to the view. Replace the return at the bottom of you method with this return.
return View::make('site.cart.payment', [
'order' => $order,
'box' => $box
]);
The second argument passed to View::make() is an array of data that should be made available to the view.