Everything is set well no typo error and I don't thing I have wrong connection betweene MVC yet I'm getting an error
"Creating default object from empty value"
For 3 days trying to solve this problem and can't locate the exact reason of it.
The View File:
@extends('layouts.app')
@section('header')
<h1>@lang('messages.paypalsettings')</h1>
<hr />
<div style="clear:both"> </div>
@endsection
@section('content')
<form action="" method="POST">
<div class="col-sm-12">
<div class="col-sm-4 float-left">
<div class"class="form-group">
<label for="merchant_email">@lang('messages.merchant_email')</label>
<input type="text" class="form-control" id="merchant_email" name="merchant_email" value="">
</div>
</div>
<div class="col-sm-4 float-left">
<div class"class="form-group">
<label for="sandbox">@lang('messages.sandbox')</label>
<select id="sandbox" name="sandbox" class="form-control">
<option value="1">Yes</option>
<option value="">No</option>
</select>
</div>
</div>
<div class="col-sm-4 float-left">
<div class"class="form-group">
<label for="api_username">@lang('messages.api_username')</label>
<input type="text" class="form-control" id="api_username" name="api_username" value="">
</div>
</div>
</div>
<div style="clear:both"> </div>
<div class="col-sm-12">
<div class="col-sm-4 float-left">
<div class"class="form-group">
<label for="api_password">@lang('messages.api_password')</label>
<input type="text" class="form-control" id="api_password" name="api_password" value="">
</div>
</div>
<div class="col-sm-4 float-left">
<div class"class="form-group">
<label for="api_secret">@lang('messages.api_secret')</label>
<input type="text" class="form-control" id="api_secret" name="api_secret" value="">
</div>
</div>
<div class="col-sm-4 float-left">
<div class"class="form-group">
<label for="currency_code">@lang('messages.currency_code')</label>
<input type="text" class="form-control" id="currency_code" name="currency_code" value="">
</div>
</div>
</div>
<div style="clear:both"> </div>
<div class="form-group float-right">
<button type="submit" class="btn btn-primary">@lang('messages.save')</button>
</div>
</form>
@endsection
The controller side
namespace AppExample\Http\Controllers;
use Illuminate\Http\Request;
use AppExample\models\Paypal;
use AppExample\models\PaypalSettings;
use AppExample\User;
use App\Invoice;
use App\Item;
use Auth;
use Lang;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Srmklive\PayPal\Services\ExpressCheckout;
use AppExample\IPNStatus;
class PayPalController extends Controller
{
/**
* @var ExpressCheckout
*/
protected $provider;
public function __construct()
{
$this->middleware('auth');
//$this->provider = new ExpressCheckout();
}
public function paypalsettings(){
$paypalsettings = PaypalSettings::find(1);
if(!$paypalsettings){
$paypalsettings = new PaypalSettings;
$paypalsettings->sandbox = '';
$paypalsettings->merchant_email = '';
$paypalsettings->api_username = '';
$paypalsettings->api_password = '';
$paypalsettings->api_secret = '';
$paypalsettings->currency_code = '';
}
return view('paypal.index')->with('paypal', $paypalsettings);
}
public function save_paypalsettings(Request $request){
$merchant_email = $request->merchant_email;
$sandbox = $request->sandbox;
$api_username = $request->api_username;
$api_password = $request->api_password;
$api_secret = $request->api_secret;
$currency_code = $request->currency_code;
$paypalsettings = PaypalSettings::find(1);
$paypalsettings->merchant_email = $request->merchant_email;
$paypalsettings->sandbox = $sandbox;
$paypalsettings->api_username = $api_username;
$paypalsettings->api_password = $api_password;
$paypalsettings->api_secret = $api_secret;
$paypalsettings->currency_code = $currency_code;
$paypalsettings->save();
// $paypalitems = Paypal::find(1);
// $paypalitems->item_number = $item_number;
// $paypalitems->item_name = $item_name;
// $paypalitems->amount = $amount;
// $paypalitems->save();
return back();
}
public function paypal_items(){
$paypal = Paypal::find(1);
if(!$paypal){
$paypal = new Paypal;
$paypal->item_number = '';
$paypal->item_name = '';
$paypal->amount = '';
}
return view('paypal.index')->with('paypal', $paypal);
}
/**
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function getExpressCheckout(Request $request)
{
$recurring = ($request->get('mode') === 'recurring') ? true : false;
$cart = $this->getCheckoutData($recurring);
try {
$response = $this->provider->setExpressCheckout($cart, $recurring);
return redirect($response['paypal_link']);
} catch (\Exception $e) {
$invoice = $this->createInvoice($cart, 'Invalid');
session()->put(['code' => 'danger', 'message' => "Error processing PayPal payment for Order $invoice->id!"]);
}
}
/**
* Process payment on PayPal.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function getExpressCheckoutSuccess(Request $request)
{
$recurring = ($request->get('mode') === 'recurring') ? true : false;
$token = $request->get('token');
$PayerID = $request->get('PayerID');
$cart = $this->getCheckoutData($recurring);
// Verify Express Checkout Token
$response = $this->provider->getExpressCheckoutDetails($token);
if (in_array(strtoupper($response['ACK']), ['SUCCESS', 'SUCCESSWITHWARNING'])) {
if ($recurring === true) {
$response = $this->provider->createMonthlySubscription($response['TOKEN'], 9.99, $cart['subscription_desc']);
if (!empty($response['PROFILESTATUS']) && in_array($response['PROFILESTATUS'], ['ActiveProfile', 'PendingProfile'])) {
$status = 'Processed';
} else {
$status = 'Invalid';
}
} else {
// Perform transaction on PayPal
$payment_status = $this->provider->doExpressCheckoutPayment($cart, $token, $PayerID);
$status = $payment_status['PAYMENTINFO_0_PAYMENTSTATUS'];
}
$invoice = $this->createInvoice($cart, $status);
if ($invoice->paid) {
session()->put(['code' => 'success', 'message' => "Order $invoice->id has been paid successfully!"]);
} else {
session()->put(['code' => 'danger', 'message' => "Error processing PayPal payment for Order $invoice->id!"]);
}
return redirect('/');
}
}
/**
* Parse PayPal IPN.
*
* @param \Illuminate\Http\Request $request
*/
public function notify(Request $request)
{
if (!($this->provider instanceof ExpressCheckout)) {
$this->provider = new ExpressCheckout();
}
$post = [
'cmd' => '_notify-validate'
];
$data = $request->all();
foreach ($data as $key => $value) {
$post[$key] = $value;
}
$response = (string) $this->provider->verifyIPN($post);
$ipn = new IPNStatus();
$ipn->payload = json_encode($post);
$ipn->status = $response;
$ipn->save();
}
/**
* Set cart data for processing payment on PayPal.
*
* @param bool $recurring
*
* @return array
*/
protected function getCheckoutData($recurring = false)
{
$data = [];
$order_id = Invoice::all()->count() + 1;
if ($recurring === true) {
$data['items'] = [
[
'name' => 'Monthly Subscription '.config('paypal.invoice_prefix').' #'.$order_id,
'price' => 0,
'qty' => 1,
],
];
$data['return_url'] = url('/teacher/{id}/success?mode=recurring');
$data['subscription_desc'] = 'Monthly Subscription '.config('paypal.invoice_prefix').' #'.$order_id;
} else {
$data['items'] = [
[
'name' => 'Product 1',
'price' => 9.99,
'qty' => 1,
],
[
'name' => 'Product 2',
'price' => 4.99,
'qty' => 2,
],
];
$data['return_url'] = url('/teacher/{id}/success');
}
$data['invoice_id'] = config('paypal.invoice_prefix').'_'.$order_id;
$data['invoice_description'] = "Order #$order_id Invoice";
$data['cancel_url'] = url('/');
$total = 0;
foreach ($data['items'] as $item) {
$total += $item['price'] * $item['qty'];
}
$data['total'] = $total;
return $data;
}
/**
* Create invoice.
*
* @param array $cart
* @param string $status
*
* @return \App\Invoice
*/
protected function createInvoice($cart, $status)
{
$invoice = new Invoice();
$invoice->title = $cart['invoice_description'];
$invoice->price = $cart['total'];
if (!strcasecmp($status, 'Completed') || !strcasecmp($status, 'Processed')) {
$invoice->paid = 1;
} else {
$invoice->paid = 0;
}
$invoice->save();
collect($cart['items'])->each(function ($product) use ($invoice) {
$item = new Item();
$item->invoice_id = $invoice->id;
$item->item_name = $product['name'];
$item->item_price = $product['price'];
$item->item_qty = $product['qty'];
$item->save();
});
return $invoice;
}
}
The Model Side
namespace AppExample\models;
use Illuminate\Database\Eloquent\Model;
class Paypal extends Model
{
protected $table = 'paypal_duration';
protected $fillable = [
'id', 'item_number', 'item_name', 'amount'
];
public function teacher(){
return $this->belongsTo('AppExample\models\Teacher');
}
public function subscription(){
return $this->belongsTo('AppExample\models\Subscription');
}
public function get_packages(){
return $packages = Packages::whereHas('paypal_packages', function($query){
$query->where('id', '=', $this->id);
})->get();
}
public function has_expired(){
$now = new Carbon();
$end = Carbon::parse($this->enddate);
return $now->gt($end);
}
}
The Route side
Route::get('admin/paypal/edit', 'PaypalController@paypalsettings')->name('admin.paypal.edit');
Route::post('admin/paypal/save', 'PaypalController@save_paypalsettings')->name('admin.paypal.save');
Maybe I stopped seeing things well should consider to go to the doctor. But am I wrong why I am getting this error while everything is well set I think. Any help much appreciated, thanks in advanced!
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2TYNgej
via IFTTT
Aucun commentaire:
Enregistrer un commentaire