I have installed a package for Laravel 7, the package in question is this http://paypal.github.io/PayPal-PHP-SDK/ to manage payments with Paypal.
I created everything, controller, web route, everything.
But the moment I go to the page to test I can't find the class.
Target class [App\Http\Controllers\PaypalController] does not exist.
PaypalController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
// use to process billing agreements
use PayPal\Api\Agreement;
use PayPal\Api\Payer;
use PayPal\Api\Plan;
use PayPal\Api\ShippingAddress;
class PaypalController extends Controller
{
private $apiContext;
private $mode;
private $client_id;
private $secret;
private $plan_id;
// Create a new instance with our paypal credentials
public function __construct()
{
// Detect if we are running in live mode or sandbox
if(config('paypal.settings.mode') == 'live'){
$this->client_id = config('paypal.live_client_id');
$this->secret = config('paypal.live_secret');
$this->plan_id = env('PAYPAL_LIVE_PLAN_ID', '');
} else {
$this->client_id = config('paypal.sandbox_client_id');
$this->secret = config('paypal.sandbox_secret');
$this->plan_id = env('PAYPAL_SANDBOX_PLAN_ID', '');
}
// Set the Paypal API Context/Credentials
$this->apiContext = new ApiContext(new OAuthTokenCredential($this->client_id, $this->secret));
$this->apiContext->setConfig(config('paypal.settings'));
}
public function paypalRedirect(){
// Create new agreement
$agreement = new Agreement();
$agreement->setName('App Name Monthly Subscription Agreement')
->setDescription('Basic Subscription')
->setStartDate(\Carbon\Carbon::now()->addMinutes(5)->toIso8601String());
// Set plan id
$plan = new Plan();
$plan->setId($this->plan_id);
$agreement->setPlan($plan);
// Add payer type
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$agreement->setPayer($payer);
try {
// Create agreement
$agreement = $agreement->create($this->apiContext);
// Extract approval URL to redirect user
$approvalUrl = $agreement->getApprovalLink();
return redirect($approvalUrl);
} catch (PayPal\Exception\PayPalConnectionException $ex) {
echo $ex->getCode();
echo $ex->getData();
die($ex);
} catch (Exception $ex) {
die($ex);
}
}
public function paypalReturn(Request $request){
$token = $request->token;
$agreement = new \PayPal\Api\Agreement();
try {
// Execute agreement
$result = $agreement->execute($token, $this->apiContext);
$user = Auth::user();
$user->role = 'subscriber';
$user->paypal = 1;
if(isset($result->id)){
$user->paypal_agreement_id = $result->id;
}
$user->save();
echo 'New Subscriber Created and Billed';
} catch (\PayPal\Exception\PayPalConnectionException $ex) {
echo 'You have either cancelled the request or your session has expired';
}
}
}
Routes
Route::get('/subscribe/paypal', 'PaypalController#paypalRedirect');
Route::get('/subscribe/paypal/return', 'PaypalController#paypalReturn');
I can't understand what the problem is! Thank you all
Just run:
composer dump-autoload
to regenerate all classes, see: https://getcomposer.org/doc/03-cli.md
Mention the Laravel 7 route:
Route::post('create_paypal_plan','App\Http\Controllers\PaypallController#create_plan');
just like this.
Related
I'm trying to generate PayPal link to email, where user can pay for their order later. I'am using paypal/rest-api-sdk-php. For example using this route:
Route::get('/order/pay/{hash}', 'Frontend\PaymentController#orderPay')->name('order.pay');
My code for payment creation works (see code). When user cancel the payment or payment is unsuccessful, how can I return to the incomplete transaction and try to pay for it again? Should I create new payment everytime user goes to order pay route? Or can I simply identify the incomplete transaction in PayPal and redirect to some(?) PayPal link then?
use PayPal\Api\Payer;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\RedirectURLs;
use PayPal\Api\Payment;
/*
* #param \App\Order $order
* #return string
*/
public function createPayment($order)
{
$transaction = $this->getTransactionByOrderHash($order->hash);
if ($transaction) {
if ($transaction->is_refunded) {
return 'Paymant has already been refunded';
}
if ($transaction->is_payed) {
return 'Paymant has already been payed';
}
}
$price = $order->to_pay;
$currencyCode = $order->currency->iso_code;
try {
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$item = new Item();
$item
->setName(__('invoice.pfa_title'))
->setCurrency($currencyCode)
->setQuantity(1)
->setSku($order->vs)
->setPrice($price);
$itemList = new ItemList();
$itemList->setItems([$item]);
$details = new Details();
$details
->setShipping(0)
->setTax(0)
->setSubtotal($price);
$amount = new Amount();
$amount
->setCurrency($currencyCode)
->setTotal($price)
->setDetails($details);
$transaction = new Transaction();
$transaction
->setAmount($amount)
->setItemList($itemList)
->setInvoiceNumber(uniqid());
$redirectUrls = new RedirectUrls();
$redirectUrls
->setReturnUrl(route('paypal.success', $order->hash))
->setCancelUrl(route('paypal.cancel'));
$payment = new Payment();
$payment
->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions([$transaction]);
$payment->create($this->apiContext);
$approvalUrl = $this->getApprovalUrl($payment);
if ($approvalUrl) {
session([
'approval_url' => $approvalUrl,
'transaction_id' => $payment->getId(),
]);
return 'payment was successful';
}
} catch (PayPalConnectionException $ex) {
return json_decode($ex->getData());
} catch (Throwable $e) {
return $e->getMessage();
}
return 'payment was unsuccessful';
}
Why use the deprecated v1/payments PayPal-PHP-SDK, instead of the current v2/checkout/orders Checkout-PHP-SDK ?
In any case, yes you can create a new Payment/Order object everytime the customer attempts a checkout. Just set the invoice_id field to your own same but unique invoice/order number for the thing that customer is paying for, so that if the customer does happen to be trying to make a duplicate payment attempt for such a # that has already resulted in a successful transaction on your PayPal account before, it will be blocked by default (according to your PayPal account settings)
I try to integrate Paubox with Laravel 5 as custom mail provider.
How can I do it in best way?
I installed https://github.com/Paubox/paubox-php bot there is no description how to connect it with Laravel.
I solve it by creating new service provider and Transport class.
Change default mail service provider in config/app.php:
// Illuminate\Mail\MailServiceProvider::class,
App\Providers\PauboxServiceProvider::class,
Add credentials to config/mail.php:
'pauboxApiKey' => env('PAUBOX_API_KEY'),
'pauboxApiUser' => env('PAUBOX_API_USER')
Changes in .env
MAIL_DRIVER=paubox
PAUBOX_API_KEY=
PAUBOX_API_USER=
Create new service provider app/Providers/PauboxServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Mail\MailServiceProvider;
class PauboxServiceProvider extends MailServiceProvider {
protected function registerSwiftTransport() {
$this->app->singleton('swift.transport', function ($app) {
return new PauboxTransportManager($app);
});
}
}
Create transport manager app/Providers/PauboxTransportManager.php
<?php
namespace App\Providers;
use Illuminate\Mail\TransportManager;
class PauboxTransportManager extends TransportManager {
protected function createPauboxDriver() {
return new PauboxTransport;
}
}
Create transport app/Providers/PauboxTransport.php
<?php
namespace App\Providers;
use Illuminate\Mail\Transport\Transport;
use Swift_Mime_SimpleMessage;
use App\Vendors\Paubox as Paubox;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Illuminate\Support\Facades\Log;
class PauboxTransport extends Transport {
protected $config;
public function __construct() {
$this->config = config('mail');
}
public function send(Swift_Mime_SimpleMessage $msg, &$failedRecipients = null) {
$paubox = new Paubox\Paubox($this->config['pauboxApiUser'], $this->config['pauboxApiKey']);
$message = new Paubox\Mail\Message();
$content = new Paubox\Mail\Content();
$header = new Paubox\Mail\Header();
$content->setHtmlText($msg->getBody());
$header->setSubject($msg->getSubject());
$header->setFrom('"' . $this->config['from']['name'] . '" <' . $this->config['from']['address'] . '>');
$header->setReplyTo($this->config['from']['address']);
$recipients = [];
foreach ($msg->getTo() as $to => $val) {
recipients[] = $to;
}
$message->setHeader($header);
$message->setContent($content);
$message->setRecipients($recipients);
$sendMessageResponse = new Paubox\Mail\SendMessageResponse();
$sendMessageResponse = $paubox->sendMessage($message);
$errorMsg = '';
if (isset($sendMessageResponse->errors)) {
foreach ($sendMessageResponse->errors as $error) {
$errorMsg .= json_encode($error);
}
Log::error(PHP_EOL . "Paubox: " . $errorMsg . PHP_EOL);
throw new UnprocessableEntityHttpException('Error occurred while sending email');
}
return $sendMessageResponse;
}
}
I copied https://github.com/Paubox/paubox-php into App\Vendors\Paubox. I had to do it bacause paubox installed with composer didn't read my .env data. After copy I had to change namespaces in all files, and add Paubox constructor to pass api_key and api_user:
public function __construct($pauboxApiUser, $pauboxApiKey)
{
$this->pauboxApiUser = $pauboxApiUser;
$this->pauboxApiKey = $pauboxApiKey;
}
and below in code:
change \getenv('PAUBOX_API_USER');
into $this->pauboxApiUser;
and change \getenv('PAUBOX_API_KEY');
into $this->pauboxApiKey;
A had to also install composer require nategood/httpful
I have a UserProvider for the Lexik bundle and check if the user exists through the session but there is a problem when I make certain request the sessions lose the value someone knows because this happens.
Service
app.user_provider:
class: ApiBundle\Security\Userprovider
arguments: ["#session","#switchconnection","#doctrine.orm.entity_manager" , "#doctrine.dbal.default_connection" , "#doctrine"]
My provider
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use ApiBundle\Entity\Utilizador;
use Symfony\Component\HttpFoundation\Session\Session;
public function __construct(Session $session,$switchconnection ,\Doctrine\ORM\EntityManager $em , \Doctrine\DBAL\Connection $dbalConnection , \Doctrine\Bundle\DoctrineBundle\Registry $doctrine) {
$this->session = $session;
$this->switchconnection = $switchconnection;
$this->em = $em;
$this->connection = $dbalConnection;
$this->doctrine = $doctrine;
}
public function loadUserByUsername($username)
{
if($this->session->get("currentuser") == $username){
$this->switchconnection->switchDatabase($this->session->get("dbconnection"), $this->connection , $this->doctrine);
$conn = $this->em->getConnection();
$stmt = $conn->prepare(".....");
$stmt->bindParam(1, $username);
$stmt->execute();
$results = $stmt->fetchAll();
$count = count($results);
if($count != 0)
{
$user= new Utilizador();
$user->setUsername($results[0]['username']);
$user->setEmail($results[0]['email']);
return $user;
}
}
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username) );
}
I also checked that in the var/session/dev folder there is a session register with 2 Phpsession almost identical.
Update 1
Important information this only happens in webkit browsers
Please include session component in your ApiBundle\Security\Userprovider.
use Symfony\Component\HttpFoundation\Session\Session;
I think you forgot to add session component.
Update-1
User service_container by passing it in service. For me its working fine.
services.yml
app.user_provider:
class: ApiBundle\Security\Userprovider
arguments: ["#service_container", "#session","#switchconnection","#doctrine.orm.entity_manager" , "#doctrine.dbal.default_connection" , "#doctrine"]
Provider
protected $container;
protected $session;
public function __construct($container, Session $session,$switchconnection ,\Doctrine\ORM\EntityManager $em , \Doctrine\DBAL\Connection $dbalConnection , \Doctrine\Bundle\DoctrineBundle\Registry $doctrine) {
$this->container = $container;
$this->session = $container->get('session');
}
Alright , I have used this way to save the users info and It works perfect,
static public function memberSave($request) {
$signup = false;
$member = new Members();
$member->name = $request['name'];
$member->email = $request['email'];
$member->password = bcrypt($request['password']);
$member->save();
if (!empty($member->id)) {
$new_id = $member->id;
DB::insert("INSERT INTO roles VALUES ($new_id, 5613)");
$signup = true;
Session::flash('sm', 'Thank you! You have signed up successfully!');
}
return $signup;
}
but when making this for editing the profile(by user) It doesn't work
becuase I use new(); (making object)
I also didn't succeed to use find(); so I tried to use this
static public function saveProfile($id,$name,$email,$password) {
$sql = "UPDATE members SET name=?,email=?,password=? WHERE id=?";
$member = DB::select($sql, [$name,$email,$password,$id]);
but when I want to bcrypt the password in laravel doesnt work .
this is the code also in the second page
public function postProfile(ProfileValidation $request) {
if (Members::saveProfile($request['id'], $request['name'], $request['email'], $request['password'])) {
return redirect('');
}
}
I hope getting helped for editing the users profile by laravel , thanks.
Your Members class must extend Eloquent\Model for following this code to work.
class Members extends Model {
// optional
protected $table = 'members';
...
To find and update the member using email,
// find the single member
$member = Members::where('email', request['email'])->first();
// update the member
$member->name = $request['name'];
$member->password = $request['password'];
// now save the updated member
$member->save();
In order to to encrypt Password, Laravel provides Hash Facade,
// import this
use Hash;
...
// encrypt Password
$encrypted = Hash::make($request['password']);
...
if you want your user automatically hash the password at your model put:
public function setPasswordAttribute($value)
{
$this->attributes['password'] = Hash::make($value);
}
and you can directly check for the user if exist create new or update it:
public function saveMember($request)
{
$member = Member::findOrNew($request->email);
//All your input you want to save
$member->save();
}
In my current configuration, a user's email is stored on a remote server that I need to hit with a curl quest.
Luckily, I only need the email once a day when a certain process runs. However, when that process does run it will need to reference the email multiple times.
This is the current accessor I have set up for email. The problem is the curl request is being called every time I use $user->email. What's the best way to avoid this?
in UserModel:
public function getEmailAttribute(){
$curl = new Curl;
$responseJson = $curl->post('https://www.dailycred.com/admin/api/user.json',array(
'client_id'=>getenv('dailycredId')
,'client_secret'=>getenv('dailycredSecret')
,'user_id'=>$this->id
));
$response = json_decode($responseJson);
return $response->email;
}
private $cached_email = false;
public function getEmailAttribute(){
if ($this->cached_email){
// if set return cached value
return $this->cached_email;
}
// get the email
$curl = new Curl;
$responseJson = $curl->post('https://www.dailycred.com/admin/api/user.json',array(
'client_id'=>getenv('dailycredId')
,'client_secret'=>getenv('dailycredSecret')
,'user_id'=>$this->id
));
$response = json_decode($responseJson);
// cache the value
$this->cached_email = $response->email;
// and return
return $this->cached_email;
}
Depending on your use case make adjustments (ie. session, cache , static property...).
Extend a the Eloquent Model class
namespace App\Models\Utils;
use Illuminate\Database\Eloquent\Model as OldModel;
class MyModel extends OldModel
{
private $cachedAttributes = [];
public function getCachedAttribute(string $key, Callable $callable)
{
if (!array_key_exists($key, $this->cachedAttributes)) {
$this->setCachedAttribute($key, call_user_func($callable));
}
return $this->cachedAttributes[$key];
}
public function setCachedAttribute(string $key, $value)
{
return $this->cachedAttributes[$key] = $value;
}
public function refresh()
{
unset($this->cachedAttributes);
return parent::refresh();
}
}
make your class
class ElementWithEmail extends MyModel
{
const ATTRIBUTE_KEY_FOR_EMAIL = 'Email';
public function getEmailAttribute(){
$key = self::ATTRIBUTE_KEY_FOR_EMAIL;
$callable = [$this, 'getEmail'];
return $this->getCachedAttribute($key, $callable);
}
protected function getEmail()
{
$curl = new Curl;
$responseJson = $curl->post('https://www.dailycred.com/admin/api/user.json',array(
'client_id'=>getenv('dailycredId')
,'client_secret'=>getenv('dailycredSecret')
,'user_id'=>$this->id
));
$response = json_decode($responseJson);
return $response->email;
}
}
Call it from your code
$element = new ElementWithEmail();
echo $element->email;