From 6041bdcfd720980d658b6c293ff2d2a4dc885f52 Mon Sep 17 00:00:00 2001 From: Harish Date: Sat, 19 Nov 2016 14:17:50 +0530 Subject: [PATCH] Initial commit --- LICENSE.txt | 21 ++ composer.json | 19 ++ contributing.md | 13 + src/Mojo.php | 229 ++++++++++++++++++ src/MojoServiceProvider.php | 26 ++ src/config/laravelmojo.php | 40 +++ ...00000_create_mojo_refund_details_table.php | 39 +++ ..._create_mojo_transaction_details_table.php | 46 ++++ src/models/MojoRefundDetails.php | 14 ++ src/models/MojoTransactionDetails.php | 15 ++ 10 files changed, 462 insertions(+) create mode 100644 LICENSE.txt create mode 100644 composer.json create mode 100644 contributing.md create mode 100644 src/Mojo.php create mode 100644 src/MojoServiceProvider.php create mode 100644 src/config/laravelmojo.php create mode 100644 src/migrations/0000_00_00_000000_create_mojo_refund_details_table.php create mode 100644 src/migrations/0000_00_00_000000_create_mojo_transaction_details_table.php create mode 100644 src/models/MojoRefundDetails.php create mode 100644 src/models/MojoTransactionDetails.php diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..e78ade7 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0d8516e --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "lubusin/mojo", + "description": "Laravel Mojo provides an expressive, fluent interface to Instamojo's payment and refund services.", + "keywords": ["laravel", "instamojo", "package"], + "license": "MIT", + "authors": [ + { + "name": "Harish Toshniwal", + "email": "haristoshniwal@gmail.com" + } + ], + "autoload": { + "psr-4": { + "Lubus\\Mojo\\": "src/" + } + }, + "minimum-stability": "dev", + "require": {} +} diff --git a/contributing.md b/contributing.md new file mode 100644 index 0000000..5c82c66 --- /dev/null +++ b/contributing.md @@ -0,0 +1,13 @@ +# Mojo Contribution Guide + +This page contains guidelines for contributing to the Laravel Mojo. Please review these guidelines before submitting any pull requests to this package. + +## Pull Requests + +The pull request process differs for new features and bugs. Before sending a pull request for a new feature, you should first create an issue with `[Proposal]` in the title. The proposal should describe the new feature, as well as implementation ideas. The proposal will then be reviewed and either approved or denied. Once a proposal is approved, a pull request may be created implementing the new feature. Pull requests which do not follow this guideline will be closed immediately. + +Pull requests for bugs may be sent without creating any proposal issue. If you believe that you know of a solution for a bug that has been filed on GitHub, please leave a comment detailing your proposed fix. + +### Feature Requests + +If you have an idea for a new feature you would like to see added to this package, you may create an issue on GitHub with `[Request]` in the title. The feature request will then be reviewed by @harishtoshniwal. \ No newline at end of file diff --git a/src/Mojo.php b/src/Mojo.php new file mode 100644 index 0000000..1be471d --- /dev/null +++ b/src/Mojo.php @@ -0,0 +1,229 @@ + $purpose, + 'amount' => $amount, + 'phone' => $user->phone, + 'buyer_name' => $user->name, + 'redirect_url' => config('laravelmojo.redirect_url_after_payment'), + 'send_email' => false, + 'webhook' => '', + 'send_sms' => false, + 'email' => $user->email, + 'allow_repeated_payments' => false ]; + + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($details)); + $response = curl_exec($ch); + curl_close($ch); + + $finalResponse = json_decode($response); + + DB::commit(); + return $finalResponse->payment_request->longurl; + } + + catch(Exception $e) + { + DB::rollback(); + return false; + } + + } + + public static function done() + { + DB::beginTransaction(); + + try + { + $payment_id = $_GET['payment_id']; + $payment_request_id = $_GET['payment_request_id']; + $sub = config('laravelmojo.subdomain_for_endpoints'); + $ch = static::setupCURL("https://$sub.instamojo.com/api/1.1/payment-requests/$payment_request_id/$payment_id/",config('laravelmojo.key'),config('laravelmojo.token')); + + $response = curl_exec($ch); + curl_close($ch); + + $decoded_response = json_decode($response); + $details = $decoded_response->payment_request; + + static::updateDB($details); + + DB::commit(); + return $details; + } + + catch(Exception $e) + { + DB::rollback(); + return false; + } + + } + + public static function updateDB($details) + { + DB::beginTransaction(); + + try + { + $user = User::where('email',$details->email)->first(); + $user_id = $user->id; + + MojoTransactionDetails::create(['user_id' => $user_id, + 'buyer_email' => $details->email, + 'buyer_name' => $details->buyer_name, + 'buyer_phone' => $details->phone, + 'currency' => $details->payment->currency, + 'amount' => $details->amount, + 'fees' => $details->payment->fees, + 'longurl' => $details->longurl, + 'payment_id' => $details->payment->payment_id, + 'payment_request_id' => $details->id, + 'purpose' => $details->purpose, + 'shorturl' => $details->shorturl, + 'request_status' => $details->status, + 'payment_status' => $details->payment->status, + ]); + DB::commit(); + return true; + } + + catch(Exception $e) + { + DB::rollback(); + return false; + } + } + + public static function setupCURL($endPoint,$key,$token) + { + $c_init = curl_init(); + + curl_setopt($c_init, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($c_init, CURLOPT_URL, $endPoint); + curl_setopt($c_init, CURLOPT_HEADER, FALSE); + curl_setopt($c_init, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($c_init, CURLOPT_FOLLOWLOCATION, TRUE); + curl_setopt($c_init, CURLOPT_HTTPHEADER,[ + "X-Api-Key: $key", + "X-Auth-Token: $token"] + ); + + return $c_init; + } + + public static function allTransactions() + { + return $allTransactionDetails = MojoTransactionDetails::all(); + } + + public static function allTransactionsFor(User $user) + { + return $userSpecificDetails = MojoTransactionDetails::where('user_id',$user->id)->get(); + } + + public static function failedPayments() + { + return $failedPayments = MojoTransactionDetails::where('payment_status','!=','credit')->get(); + } + + public static function successfulPayments() + { + return $successfulPayments = MojoTransactionDetails::where('payment_status','credit')->get(); + } + + public static function myAndMojosIncome() + { + return $totalIncome = MojoTransactionDetails::sum('amount'); + } + + public static function myIncome() + { + $a = MojoTransactionDetails::sum('amount'); + $f = MojoTransactionDetails::sum('fees'); + return $earnings = $a - $f; + } + + public static function mojosIncome() + { + return $mojoShare = MojoTransactionDetails::sum('fees'); + } + + public static function refund($payment_id,$type,$reason) + { + DB::beginTransaction(); + + try + { + $sub = config('laravelmojo.subdomain_for_endpoints'); + $ch = static::setupCURL("https://$sub.instamojo.com/api/1.1/refunds/",config('laravelmojo.key'),config('laravelmojo.token')); + + $details = ['payment_id' => $payment_id, + 'type' => $type, + 'body' => $reason ]; + + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($details)); + $response = curl_exec($ch); + curl_close($ch); + + $finalResponse = json_decode($response); + $refund = $finalResponse->refund; + + $inst = MojoTransactionDetails::where('payment_id',$payment_id)->first(); + $user_id = $inst->user_id; + + MojoRefundDetails::create(['user_id' => $user_id, + 'payment_id' => $payment_id, + 'status' => $refund->status, + 'type' => $refund->type, + 'body' => $refund->body, + 'refund_amount' => $refund->refund_amount, + 'total_amount' => $refund->total_amount, + ]); + + DB::commit(); + return $refund; + } + + catch(Exception $e) + { + DB::rollback(); + return false; + } + } + + public static function allRefunds() + { + return $allRefundDetails = MojoRefundDetails::all(); + } + + public static function allRefundsFor(User $user) + { + return $userSpecificDetails = MojoRefundDetails::where('user_id',$user->id)->get(); + } + +} + +?> \ No newline at end of file diff --git a/src/MojoServiceProvider.php b/src/MojoServiceProvider.php new file mode 100644 index 0000000..8d67d52 --- /dev/null +++ b/src/MojoServiceProvider.php @@ -0,0 +1,26 @@ +publishes([ + __DIR__ . '/migrations' => $this->app->databasePath() . '/migrations' + ], 'migrations'); + + $this->publishes([ + __DIR__.'/config/laravelmojo.php' => config_path('laravelmojo.php'), + ], 'config'); + } + + public function register() + { + $this->app->make('Lubus\Mojo\Mojo'); + } +} + +?> \ No newline at end of file diff --git a/src/config/laravelmojo.php b/src/config/laravelmojo.php new file mode 100644 index 0000000..451cfa8 --- /dev/null +++ b/src/config/laravelmojo.php @@ -0,0 +1,40 @@ + '', + + /* + * Specify your Private AUTH Token provided by Instamojo in the + * API & Plugins section of your integration page. + * Also I haven't watched a single episode of Game of thrones yet, please + * don't judge me for that. It's on my list. + */ + + 'token' => '', + + /* + * The URL of your app to which the user will be redirected after the + * payment process at Instamojo's end. + * Tip : If you are testing on localhost , create a alias in your host + * with a custom domain for 127.0.0.1 , use that domain/your_project/public + * as you base URL. And add it to your routes. + */ + + 'redirect_url_after_payment' => '', + + /* + * These took me an hour to figure out , for testing purposers you will + * most probably use the instamojo sandbox testing account , if thats + * the case, then the subdomain for all your endpoints will be "test" + * thats why it has been set as the default , OR if you are + * using the production verified accound details , + * then it will be "www" + */ + + 'subdomain_for_endpoints' => 'test', +]; \ No newline at end of file diff --git a/src/migrations/0000_00_00_000000_create_mojo_refund_details_table.php b/src/migrations/0000_00_00_000000_create_mojo_refund_details_table.php new file mode 100644 index 0000000..2d80864 --- /dev/null +++ b/src/migrations/0000_00_00_000000_create_mojo_refund_details_table.php @@ -0,0 +1,39 @@ +increments('id'); + $table->integer('user_id')->unsigned(); + $table->foreign('user_id')->references('id')->on('users'); + $table->string('payment_id'); + $table->string('status'); + $table->string('type'); + $table->string('body'); + $table->string('refund_amount'); + $table->string('total_amount'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('mojo_refund_details'); + } +} \ No newline at end of file diff --git a/src/migrations/0000_00_00_000000_create_mojo_transaction_details_table.php b/src/migrations/0000_00_00_000000_create_mojo_transaction_details_table.php new file mode 100644 index 0000000..15d1134 --- /dev/null +++ b/src/migrations/0000_00_00_000000_create_mojo_transaction_details_table.php @@ -0,0 +1,46 @@ +increments('id'); + $table->integer('user_id')->unsigned(); + $table->foreign('user_id')->references('id')->on('users'); + $table->string('buyer_email'); + $table->string('buyer_name'); + $table->string('buyer_phone'); + $table->string('currency'); + $table->string('amount'); + $table->string('fees'); + $table->string('longurl'); + $table->string('payment_id'); + $table->string('payment_request_id'); + $table->string('purpose'); + $table->string('shorturl')->nullable(); + $table->string('request_status'); + $table->string('payment_status'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('mojo_transaction_details'); + } +} \ No newline at end of file diff --git a/src/models/MojoRefundDetails.php b/src/models/MojoRefundDetails.php new file mode 100644 index 0000000..15315a4 --- /dev/null +++ b/src/models/MojoRefundDetails.php @@ -0,0 +1,14 @@ + \ No newline at end of file diff --git a/src/models/MojoTransactionDetails.php b/src/models/MojoTransactionDetails.php new file mode 100644 index 0000000..a36f3ef --- /dev/null +++ b/src/models/MojoTransactionDetails.php @@ -0,0 +1,15 @@ + \ No newline at end of file