Add PayPal support

This commit is contained in:
DariusIII
2019-10-12 23:03:52 +02:00
parent 961eaf87e0
commit 2cf2805633
9 changed files with 213 additions and 2 deletions
+3
View File
@@ -161,3 +161,6 @@ HORIZON_PREFIX=horizon:
POSTMARK_TOKEN=
PURGE_INACTIVE_USERS=false
PAYPAL_CLIENTID=
PAYPAL_SECRET=
+2
View File
@@ -1,3 +1,5 @@
2019-10-12 DariusIII
* Chg: Add PayPal support
2019-10-10 DariusIII
* Chg: Add omnipay/paypal for future usage
* Fix: Fix download of zipped nzbs
+109 -2
View File
@@ -2,9 +2,12 @@
namespace App\Http\Controllers;
use App\Models\PaypalPayment;
use App\Models\Settings;
use App\Models\User;
use Illuminate\Http\Request;
use Blacklight\libraries\Geary;
use Omnipay\Omnipay;
use Spatie\Permission\Models\Role;
class BtcPaymentController extends BasePageController
@@ -38,7 +41,7 @@ class BtcPaymentController extends BasePageController
if ($order->payment_id) {
// Redirect to a payment gateway
$url = 'https://gateway.gear.mycelium.com/pay/'.$order->payment_id;
$url = 'https://gateway.gear.mycelium.com/pay/' . $order->payment_id;
return redirect($url);
}
@@ -77,10 +80,114 @@ class BtcPaymentController extends BasePageController
$amount = $callback_data['price'];
$addYear = $callback_data['addyears'];
// If order was paid in full (2) or overpaid (4)
if ((int) $order['status'] === 2 || (int) $order['status'] === 4) {
if ((int)$order['status'] === 2 || (int)$order['status'] === 4) {
User::updateUserRole($callback_data['user_id'], $newRole);
User::updateUserRoleChangeDate($callback_data['user_id'], null, $addYear);
}
}
}
/**
* @param Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws \Exception
*/
public function paypal(Request $request)
{
$this->setPrefs();
$gateway = Omnipay::create('PayPal_Rest');
$gateway->initialize(['clientId' => env('PAYPAL_CLIENTID'), 'secret' => env('PAYPAL_SECRET'), 'testMode' => true]);
$amount = $request->input('amount');
// Do a purchase transaction on the gateway
try {
$transaction = $gateway->purchase([
'amount' => $amount,
'currency' => 'USD',
'description' => $this->userdata->id,
'returnUrl' => 'http://homestead.test/thankyou?id=' . $this->userdata->id . '&amount=' . $amount,
'cancelUrl' => 'http://homestead.test/payment_failed',
]);
$response = $transaction->send();
if ($response->isSuccessful()) {
return redirect($response->getRedirectUrl());
} elseif ($response->isRedirect()) {
return $response->redirect();
}
} catch (\Exception $e) {
echo "Exception caught while attempting authorize.\n";
echo 'Exception type == ' . get_class($e) . "\n";
echo 'Message == ' . $e->getMessage() . "\n";
}
}
/**
* @throws \Exception
*/
public function showPaypal()
{
$this->setPrefs();
$donation = Role::query()->where('donation', '>', 0)->get(['id', 'name', 'donation', 'addyears']);
$this->smarty->assign('donation', $donation);
$title = 'Become a supporter';
$meta_title = 'Become a supporter';
$meta_description = 'Become a supporter';
$content = $this->smarty->fetch('pay_by_paypal.tpl');
$this->smarty->assign(compact('content', 'meta_title', 'title', 'meta_description'));
$this->pagerender();
}
/**
* @param Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws \Exception
*/
public function paypalCallback(Request $request)
{
$this->setPrefs();
$amount = $request->input('amount');
$userId = $request->input('id');
$role = Role::query()->where('donation', $amount)->first();
$gateway = Omnipay::create('PayPal_Rest');
$gateway->initialize(['clientId' => env('PAYPAL_CLIENTID'), 'secret' => env('PAYPAL_SECRET'), 'testMode' => true]);
$response = $gateway->completePurchase(
[
'amount' => $amount,
'currency' => 'USD',
'description' => $userId,
'payerId' => $request->input('PayerID'),
'transactionReference' => $request->input('paymentId'),
])->send();
if ($response->isSuccessful()) {
$check = PaypalPayment::query()->where('transaction_id', $request->input('paymentId'))->first();
if ($check === null) {
User::updateUserRole($userId, $role->id);
User::updateUserRoleChangeDate($userId, null, $role->addyears);
PaypalPayment::query()->insert(['users_id' => $userId, 'transaction_id' => $request->input('paymentId'), 'created_at' => now(), 'updated_at' => now()]);
$title = 'Cheers!';
$meta_title = Settings::settingValue('site.main.title') . ' - Payment Complete';
$meta_description = 'Payment Complete';
$content = $this->smarty->fetch('thankyou.tpl');
$this->smarty->assign(compact('content', 'meta_title', 'title', 'meta_description'));
$this->pagerender();
} else {
echo 'Transaction already exists!';
}
} else {
return redirect('payment_failed');
}
}
public function paypalFailed()
{
echo 'Shit happens';
}
}
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class PaypalPayment extends Model
{
//
}
+7
View File
@@ -392,6 +392,13 @@ class User extends Authenticatable
*/
public static function updateUserRole(int $uid, int $role)
{
$roleQuery = Role::query()->where('id', $role)->first();
$roleName = $roleQuery->name;
$user = self::find($uid);
$user->assignRole($roleName);
return self::find($uid)->update(['roles_id' => $role]);
}
@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePaypalPaymentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('paypal_payments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('users_id');
$table->string('transaction_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('paypal_payments');
}
}
@@ -0,0 +1,25 @@
<div class="card">
<div class="card-body pagination2">
<div class="row">
<div class="alert alert-info">
<span style="align-content: center"> This page will redirect you to site outside of {$site->title} to make your payment
<br>
If, for some reason, your account isn't updated automatically, please send us an email or use our contact form to inform us so we can fix the issue.</span>
</div>
</div>
</div>
<table class="data table table-sm responsive-utilities jambo-table">
{foreach $donation as $donate}
{{Form::open(['url' => "paypal?amount={$donate->donation}"])}}
<thead class="thead-light">
<tr>
<th>{$donate->name} ({$donate->donation}$)</th>
</tr>
</thead>
<td>
{{Form::submit('Pay with Paypal', ['class' => 'btn btn-success'])}}
</td>
{{Form::close()}}
{/foreach}
</table>
</div>
@@ -0,0 +1,8 @@
<div class="card">
<div class="card-body pagination2">
<div class="row">
<div class="alert alert-success">
<span style="align-content: center"> Thank you. Your payment has been processed and account upgraded.</span>
</div>
</div>
</div>
+16
View File
@@ -172,6 +172,22 @@ Route::group(['middleware' => ['isVerified', 'fw-block-blacklisted']], function
Route::post('btc_payment_callback', 'BtcPaymentController@callback')->name('btc_payment_callback');
Route::get('pay_by_paypal', 'BtcPaymentController@showPaypal')->name('pay_by_paypal');
Route::post('pay_by_paypal', 'BtcPaymentController@showpaypal')->name('pay_by_paypal');
Route::get('paypal', 'BtcPaymentController@paypal')->name('paypal');
Route::post('paypal', 'BtcPaymentController@paypal')->name('paypal');
Route::get('thankyou', 'BtcPaymentController@paypalCallback')->name('thankyou');
Route::post('thankyou', 'BtcPaymentController@paypalCallback')->name('thankyou');
Route::get('payment_failed', 'BtcPaymentController@paypalFailed')->name('payment_failed');
Route::post('payment_failed', 'BtcPaymentController@paypalFailed')->name('payment_failed');
Route::get('queue', 'QueueController@index')->name('queue');
Route::post('queue', 'QueueController@index')->name('queue');