Skip to content

Commit 807816f

Browse files
committed
Added the posibility to add merchants both in the config array, and also
as a database entry.
1 parent fa669e8 commit 807816f

File tree

8 files changed

+144
-37
lines changed

8 files changed

+144
-37
lines changed

config/dibsd2.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
11
<?php
22

33
return [
4-
'route_prefix' => env('DIBSD2_PREFIX', '/dibsd2/'),
4+
'public_route_prefix' => env('DIBSD2_PREFIX', '/dibsd2/'),
5+
'admin_route_prefix' => env('DIBSD2_ADMIN_PREFIX', '/admin/dibsd2/'),
56

6-
'account' => 'default',
7+
/*
8+
|--------------------------------------------------------------------------
9+
| Default Config Driver
10+
|--------------------------------------------------------------------------
11+
|
12+
| The dibs module, supports two driver modes. Array and database.
13+
| It is possible to store, merchant config, in either an array, so
14+
| in this config file, or it is possible to use the supplied model and
15+
| database driver for storing merchants.
16+
|
17+
| Supported: "array", "database"
18+
|
19+
*/
20+
'driver' => 'array',
21+
22+
/*
23+
|--------------------------------------------------------------------------
24+
| Default Merchant
25+
|--------------------------------------------------------------------------
26+
|
27+
| This is the default merchant to be used, if no other merchant is supplied.
28+
| If the driver used is array, you would need to supply the key in the array.
29+
| If the driver used is database, then it would be the id, on database level
30+
| of the merchant in the database, that needs to be used.
31+
|
32+
*/
33+
'default_merchant' => 'default',
734

835
'defaults' => array(
9-
'testMode' => false,
36+
'testMode' => false,
1037
),
1138

12-
'accounts' => [
39+
'merchants' => [
1340
'default' => [
1441
'key1' => '', // MD5 Key 1
1542
'key2' => '', // MD5 Key 2

migrations/2018_01_05_165234_create_transactions_table.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function up()
1515
{
1616
Schema::create('transactions', function (Blueprint $table) {
1717
$table->increments('id');
18-
$table->string('account');
18+
$table->string('merchant_id');
1919
$table->string('amount');
2020
$table->string('redirect_to');
2121
$table->string('transaction', 100)->nullable();
@@ -31,6 +31,26 @@ public function up()
3131
$table->text('payload')->nullable();
3232
$table->timestamps();
3333
});
34+
35+
Schema::create('merchants', function (Blueprint $table) {
36+
$table->increments('id');
37+
$table->string('merchant_id')->unique();
38+
$table->string('name');
39+
$table->string('key1');
40+
$table->string('key2');
41+
$table->string('username')->nullable();
42+
$table->string('password')->nullable();
43+
$table->string('lang');
44+
$table->string('currency');
45+
$table->string('pay_type');
46+
$table->timestamps();
47+
});
48+
49+
Schema::create('merchantables', function(Blueprint $table) {
50+
$table->unsignedInteger('merchant_id');
51+
$table->foreign('merchant_id')->references('id')->on('merchants')->onDelete('cascade');
52+
$table->morphs('merchantable');
53+
});
3454
}
3555

3656
/**
@@ -43,6 +63,8 @@ public function down()
4363
Schema::disableForeignKeyConstraints();
4464
Schema::dropIfExists('transaction_logs');
4565
Schema::dropIfExists('transactions');
66+
Schema::dropIfExists('merchants');
67+
Schema::dropIfExists('merchantables');
4668
Schema::enableForeignKeyConstraints();
4769
}
4870
}

routes/dibsd2.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
Route::group([
1717
'namespace' => 'Addgod\DibsD2\app\Http\Controllers',
18-
'middleware' => 'admin',
18+
'middleware' => ['web', 'admin'],
1919
'prefix' => config('dibsd2.admin_route_prefix'),
2020
], function() {
2121
Route::get('/capture/{transaction}', 'DibsD2Controller@capture')->name('dibsd2.capture');

src/GatewayManager.php

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Addgod\DibsD2;
44

55
use Omnipay\Common\GatewayFactory;
6+
use Addgod\DibsD2\app\Models\Merchant;
67

78
class GatewayManager {
89

@@ -14,9 +15,9 @@ class GatewayManager {
1415
protected $app;
1516

1617
/**
17-
* The registered accounts
18+
* The registered merchants
1819
*/
19-
protected $accounts;
20+
protected $merchants;
2021

2122
/**
2223
* The default settings, applied to every gateway
@@ -45,46 +46,53 @@ public function __construct($app, GatewayFactory $factory, $defaults = array())
4546
*/
4647
public function gateway()
4748
{
48-
$account = $this->getDefaultAccount();
49-
if(!isset($this->accounts[$account])){
49+
$merchant = $this->getDefaultMerchant();
50+
if(!isset($this->merchants[$merchant])){
5051
$gateway = $this->factory->create('DibsD2', null, $this->app['request']);
51-
$gateway->initialize($this->getConfig($account));
52-
$this->accounts[$account] = $gateway;
52+
$gateway->initialize($this->getConfig($merchant));
53+
$this->merchants[$merchant] = $gateway;
5354
}
5455

55-
return $this->accounts[$account];
56+
return $this->merchants[$merchant];
5657
}
5758

5859
/**
5960
* Get the configuration, based on the config and the defaults.
6061
*/
61-
protected function getConfig($name)
62+
protected function getConfig($id)
6263
{
63-
return array_merge(
64-
$this->defaults,
65-
$this->app['config']->get('dibsd2.accounts.'.$name, array())
66-
);
64+
if ($this->app['config']['dibsd2.driver'] === 'array') {
65+
return array_merge(
66+
$this->defaults,
67+
$this->app['config']->get('dibsd2.merchants.'.$id, array())
68+
);
69+
} elseif ($this->app['config']['dibsd2.driver'] === 'database') {
70+
return array_merge(
71+
$this->defaults,
72+
Merchant::find($id)->toConfig()
73+
);
74+
}
6775
}
6876

6977
/**
70-
* Get the default account name.
78+
* Get the default merchant name.
7179
*
7280
* @return string
7381
*/
74-
public function getDefaultAccount()
82+
public function getDefaultMerchant()
7583
{
76-
return $this->app['config']['dibsd2.account'];
84+
return $this->app['config']['dibsd2.default_merchant'];
7785
}
7886

7987
/**
80-
* Set the default account name.
88+
* Set the default merchant name.
8189
*
8290
* @param string $name
8391
* @return void
8492
*/
85-
public function setDefaultAccount($name)
93+
public function setDefaultMerchant($name)
8694
{
87-
$this->app['config']['dibsd2.account'] = $name;
95+
$this->app['config']['dibsd2.default_merchant'] = $name;
8896
}
8997

9098
/**

src/app/Http/Controllers/DibsD2Controller.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function purchase(Transaction $transaction)
1717
];
1818

1919
$dibs = app()->make('DibsD2');
20-
$dibs::setDefaultAccount($transaction->account);
20+
$dibs::setDefaultMerchant($transaction->merchant_id);
2121

2222
$response = $dibs::purchase($params)->send();
2323

@@ -61,7 +61,7 @@ public function authorize(Transaction $transaction)
6161
];
6262

6363
$dibs = app()->make('DibsD2');
64-
$dibs::setDefaultAccount($transaction->account);
64+
$dibs::setDefaultMerchant($transaction->merchant_id);
6565

6666
$response = $dibs::authorize($params)->send();
6767

@@ -100,7 +100,7 @@ public function reAuthorize(Transaction $transaction)
100100
'transactionId' => $transaction->transaction
101101
];
102102
$dibs = app()->make('DibsD2');
103-
$dibs::setDefaultAccount($transaction->account);
103+
$dibs::setDefaultMerchant($transaction->merchant_id);
104104

105105
$response = $dibs::reAuthorize($params)->send();
106106

@@ -124,7 +124,7 @@ public function capture(Transaction $transaction)
124124
];
125125

126126
$dibs = app()->make('DibsD2');
127-
$dibs::setDefaultAccount($transaction->account);
127+
$dibs::setDefaultMerchant($transaction->merchant_id);
128128

129129
$response = $dibs::capture($params)->send();
130130

@@ -141,6 +141,8 @@ public function capture(Transaction $transaction)
141141
} else {
142142
throw new \Exception('Capture of payment failed');
143143
}
144+
145+
return redirect()->back();
144146
}
145147

146148
public function void(Transaction $transaction)
@@ -151,7 +153,7 @@ public function void(Transaction $transaction)
151153
];
152154

153155
$dibs = app()->make('DibsD2');
154-
$dibs::setDefaultAccount($transaction->account);
156+
$dibs::setDefaultMerchant($transaction->merchant_id);
155157

156158
$response = $dibs::void($params)->send();
157159

@@ -166,8 +168,17 @@ public function void(Transaction $transaction)
166168
]
167169
]);
168170
} else {
171+
$transaction->logs()->create([
172+
'payload' => [
173+
'user' => \Auth::user()->toArray(),
174+
'action' => 'Void',
175+
'data' => $response->getData()
176+
]
177+
]);
169178
throw new \Exception('Void of payment failed');
170179
}
180+
181+
return redirect()->back();
171182
}
172183

173184
public function refund(Transaction $transaction, $amount = null)
@@ -179,7 +190,7 @@ public function refund(Transaction $transaction, $amount = null)
179190
];
180191

181192
$dibs = app()->make('DibsD2');
182-
$dibs::setDefaultAccount($transaction->account);
193+
$dibs::setDefaultMerchant($transaction->merchant_id);
183194

184195
$response = $dibs::refund($params)->send();
185196

@@ -202,6 +213,8 @@ public function refund(Transaction $transaction, $amount = null)
202213
} else {
203214
throw new \Exception('Refund of payment failed');
204215
}
216+
217+
return redirect()->back();
205218
}
206219

207220
public function callback()

src/app/Models/Merchant.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Addgod\DibsD2\app\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Merchant extends Model
8+
{
9+
protected $fillable = [
10+
'merchant_id',
11+
'key1',
12+
'key2',
13+
'username',
14+
'password',
15+
'lang',
16+
'currency',
17+
'pay_type',
18+
];
19+
20+
public function toConfig()
21+
{
22+
return [
23+
'merchantid' => $this->merchant_id,
24+
'key1' => $this->key1,
25+
'key2' => $this->key2,
26+
'username' => $this->username,
27+
'password' => $this->password,
28+
'lang' => $this->lang,
29+
'currency' => $this->currency,
30+
'payType' => $this->pay_type,
31+
];
32+
}
33+
}

src/app/Models/Transaction.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,21 @@ class Transaction extends Model
2323
*/
2424
protected $fillable = [
2525
'transaction',
26-
'account',
26+
'merchant_id',
2727
'status',
2828
'redirect_to',
2929
];
3030

31-
protected $attributes = [
32-
'account' => 'default'
33-
];
34-
3531
public function logs()
3632
{
3733
return $this->hasMany(TransactionLog::class);
3834
}
35+
36+
public function save(array $options = [])
37+
{
38+
if (empty($this->attributes['merchant_id'])) {
39+
$this->attributes['merchant_id'] = config('dibsd2.default_merchant');
40+
}
41+
return parent::save($options);
42+
}
3943
}

tests/unit/GatewayTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function getPackageAliases($app)
4646

4747
public function testGateway()
4848
{
49-
DibsD2::setDefaultAccount('default');
49+
DibsD2::setDefaultMerchant('default');
5050
}
5151

5252

@@ -65,4 +65,4 @@ public function testController()
6565

6666
$response->assertStatus(302);
6767
}
68-
}
68+
}

0 commit comments

Comments
 (0)