1 2 3 4 5 |
composer require mollie/laravel-mollie:^2.0 |
1 2 3 4 5 |
php artisan vendor:publish |
config/app.php
providers
-Mollie\Laravel\MollieServiceProvider::class,
aliases
‘Mollie’ => Mollie\Laravel\Facades\Mollie::class,
config/mollie.php
MOLLIE_KEY : update with your test key
routes/api.php
Route::get(‘/subscribe_plan’, ‘SubscriptionController@subscribePlan’);
Route::post(‘/subscription_webhooks’, ‘SubscriptionController@webhooks’)->name(‘webhooks.mollie’);
Route::get(‘/subscription_completed’, ‘SubscriptionController@completed’)->name(‘order.success’);
Database Table :
SubscriptionController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
<?php namespace App\Http\Controllers\API; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use App\Models\SubscriptionPlan; use App\Models\UserTransaction; use Mollie; class SubscriptionController extends Controller { // public function subscribePlan(Request $request){ $validator = Validator::make($request->all(), [ 'user_id' => ['required'], 'plan_id' => ['required'], 'plan_days' => ['required'], ]); if ($validator->fails()) { $response=array(); $response['status']=0; $response['msg']="The request could not be understood by the server due to malformed syntax"; $response['statuscode']=400; $response['data']=$validator->errors(); }else{ $SubscriptionPlan= SubscriptionPlan::find($request->plan_id); if($SubscriptionPlan->is_active==1){ $plan_days=explode(",", $SubscriptionPlan->plan_days); $plan_amount=explode(",", $SubscriptionPlan->plan_amount); $selectedPlanDays=''; $selectedPlanAmount=''; foreach ($plan_days as $key => $value) { if($value==$request->plan_days){ $selectedPlanDays=$value; $selectedPlanAmount= $plan_amount[$key]; } } if($selectedPlanDays==''){ $response=array(); $response['status']=0; $response['msg']="Invalid user inputs"; $response['statuscode']=400; return response()->json($response)->header('Content-Type', 'application/json'); } $amount=$selectedPlanAmount; $amount=number_format($amount, 2, '.', ''); // $user_id=$request->user_id; $user_id=Auth::user()->id; $customer = Mollie::api()->customers()->create([ 'name' => "user".$user_id."", 'email' => $user_id.'@test.com', ]); $payment = Mollie::api()->payments()->create([ 'amount' => [ 'currency' => 'EUR', 'value' => $amount, // You must send the correct number of decimals, thus we enforce the use of strings ], 'customerId' => $customer->id, 'sequenceType' => 'first', 'method' => array('creditcard','directdebit'), 'description' => 'My Initial Payment', 'redirectUrl' => route('order.success'), 'webhookUrl' => route('webhooks.mollie'), ]); $UserTransaction=new UserTransaction(); $UserTransaction->user_id=$user_id; $UserTransaction->plan_id=$request->plan_id; $UserTransaction->payment_id=$payment->id; $UserTransaction->payment_status=''; $UserTransaction->payment_days=$selectedPlanDays; $UserTransaction->payment_date=date('Y-m-d H:i:s'); $UserTransaction->payment_expired_date=date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s'). ' + '.$selectedPlanDays.' days')); $UserTransaction->is_subscribed=1; $UserTransaction->save(); $payment = Mollie::api()->payments->get($payment->id); $response=array(); $response['status']=1; $response['msg']="success"; $response['statuscode']=200; $response['data']=$payment->getCheckoutUrl(); }else{ $response=array(); $response['status']=0; $response['msg']="Sorry, plan is not active"; $response['statuscode']=400; } } return response()->json($response)->header('Content-Type', 'application/json'); } function completed(Request $request){ echo "<h1 style='text-align:center;margin-top: 35px;'>Please wait...</h1>"; } function webhooks(Request $request){ $payment = Mollie::api()->payments->get($request->id); $statusOfPayment=''; if ($payment->isPaid() && !$payment->hasRefunds() && !$payment->hasChargebacks()) { /* * The payment is paid and isn't refunded or charged back. * At this point you'd probably want to start the process of delivering the product to the customer. */ $statusOfPayment='paid'; } elseif ($payment->isOpen()) { /* * The payment is open. */ $statusOfPayment='open'; } elseif ($payment->isPending()) { /* * The payment is pending. */ $statusOfPayment='pending'; } elseif ($payment->isFailed()) { /* * The payment has failed. */ $statusOfPayment='failed'; } elseif ($payment->isExpired()) { /* * The payment is expired. */ } elseif ($payment->isCanceled()) { /* * The payment has been canceled. */ $statusOfPayment='expired'; } elseif ($payment->hasRefunds()) { /* * The payment has been (partially) refunded. * The status of the payment is still "paid" */ $statusOfPayment='partially refunded'; } elseif ($payment->hasChargebacks()) { /* * The payment has been (partially) charged back. * The status of the payment is still "paid" */ $statusOfPayment='partially charged back'; } $UserTransaction=UserTransaction::where('payment_id',$request->id)->first(); $UserTransaction->payment_status=$statusOfPayment; $UserTransaction->save(); } } |
More Stories
CPU & Memory usage in PHP
Install PHP mcrypt extension on Ubuntu
Text to speech