Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
introwit committed Nov 19, 2016
1 parent cfc2a09 commit 6041bdc
Show file tree
Hide file tree
Showing 10 changed files with 462 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) <Harish Toshniwal>

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.
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Lubus\\Mojo\\": "src/"
}
},
"minimum-stability": "dev",
"require": {}
}
13 changes: 13 additions & 0 deletions contributing.md
Original file line number Diff line number Diff line change
@@ -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.
229 changes: 229 additions & 0 deletions src/Mojo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
<?php

namespace Lubus\Mojo;

use Lubus\Mojo\Models\MojoTransactionDetails;
use Lubus\Mojo\Models\MojoRefundDetails;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use DB;

class Mojo
{
public static function go($user,$amount,$purpose)
{
DB::beginTransaction();

try
{
$sub = config('laravelmojo.subdomain_for_endpoints');
$ch = static::setupCURL("https://$sub.instamojo.com/api/1.1/payment-requests/",config('laravelmojo.key'),config('laravelmojo.token'));

$details = ['purpose' => $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();
}

}

?>
26 changes: 26 additions & 0 deletions src/MojoServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Lubus\Mojo;

use Illuminate\Support\ServiceProvider;

class MojoServiceProvider extends ServiceProvider
{
public function boot()
{
$this->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');
}
}

?>
40 changes: 40 additions & 0 deletions src/config/laravelmojo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

return [

/* Specify your Private AUTH Token provided by Instamojo in the
* API & Plugins section of your integration page.
*/

'key' => '',

/*
* 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',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMojoRefundDetailsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('mojo_refund_details', function (Blueprint $table) {
$table->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');
}
}
Loading

0 comments on commit 6041bdc

Please sign in to comment.