Skip to content

Commit d578fc6

Browse files
committed
Add reference type on transaction
1 parent aaddac1 commit d578fc6

9 files changed

+70
-10
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ php artisan vendor:publish --tag="duitnow-views"
8787
$redirectUrl = $duitNowPayment->initiatePayment(10, "Zarul Zubir", $bankType, "Ref: " . rand(100, 200), $bankId, 'A1000001');
8888
```
8989

90+
## Upgrade v1 to v2
91+
92+
This major upgrade add column on transaction table to allow morphing.
93+
94+
```
95+
Run `php artisan vendor:publish --tag=duitnow-v2-migration`
96+
```
97+
9098
## Contributing
9199

92100
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

database/migrations/create_duitnow_banks_table.php.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ return new class extends Migration
1313
$table->string('code')->unique();
1414
$table->string('name', 300);
1515
$table->string('status', 20)->nullable()->comment('Online, Offline');
16+
$table->boolean('is_active')->nullable()->default(true);
1617
$table->timestamps();
1718
});
1819
}

database/migrations/create_duitnow_transactions_table.php.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ return new class extends Migration
1111
Schema::create('duitnow_transactions', function (Blueprint $table) {
1212
$table->id();
1313
$table->string('reference_id', 100)->nullable();
14+
$table->string('reference_type', 100)->nullable();
1415
$table->string('transaction_id', 100)->nullable();
1516
$table->json('request_payload')->nullable();
1617
$table->json('response_payload')->nullable();

src/DuitNowPayment.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected function syncBanks($banks)
101101
}
102102
}
103103

104-
public function initiatePayment($amount, $customerName, $bankType, $bankId, $referenceId, $coordinate, $ipAddress)
104+
public function initiatePayment($amount, $customerName, $bankType, $bankId, $recipientReference, $coordinate, $ipAddress, $paymentDescription = null, $referenceId = null, $referenceType = null)
105105
{
106106
$this->coordinate = $coordinate;
107107
$this->ipAddress = $ipAddress;
@@ -132,8 +132,8 @@ public function initiatePayment($amount, $customerName, $bankType, $bankId, $ref
132132
'accountType' => config('duitnow.account_type'),
133133
],
134134
'sourceOfFunds' => config('duitnow.source_of_funds'),
135-
'recipientReference' => $referenceId,
136-
'paymentDescription' => $referenceId,
135+
'recipientReference' => $recipientReference,
136+
'paymentDescription' => $paymentDescription ?? $recipientReference,
137137
];
138138

139139
$url = config('duitnow.url.base') . '/merchants/v1/payments/redirect';
@@ -149,7 +149,7 @@ public function initiatePayment($amount, $customerName, $bankType, $bankId, $ref
149149
'Content-Type' => 'application/json',
150150
])->post($url, $body);
151151

152-
$this->saveTransaction($this->transactionId, $referenceId, $body, $this->messageId);
152+
$this->saveTransaction($this->transactionId, $referenceId, $referenceType, $body, $this->messageId);
153153

154154
if ($response->status() == 200) {
155155
$redirectUrl = $this->getUrl($bankId, $bankType, $this->messageId, $response->object()->endToEndIdSignature);
@@ -181,11 +181,12 @@ protected function getUrl($bankId, $bankType, $messageId, $endToEndIdSignature)
181181
);
182182
}
183183

184-
protected function saveTransaction($transactionId, $referenceId, $requestPayload, $endToEndId): DuitNowTransaction
184+
protected function saveTransaction($transactionId, $referenceId, $referenceType, $requestPayload, $endToEndId): DuitNowTransaction
185185
{
186186
return DuitNowTransaction::create([
187187
'transaction_id' => $transactionId,
188188
'reference_id' => $referenceId,
189+
'reference_type' => $referenceType,
189190
'request_payload' => $requestPayload,
190191
'end_to_end_id' => $endToEndId,
191192
]);

src/DuitNowPaymentServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public function configurePackage(Package $package): void
2828
$this->package->basePath('/../stubs/Controller.php') => app_path("Http/Controllers/DuitNow/Controller.php"),
2929
], "{$this->package->shortName()}-controller");
3030

31+
$this->publishes([
32+
$this->package->basePath('/../stubs/migrations') => database_path("migrations"),
33+
], "{$this->package->shortName()}-v2-migration");
34+
3135
$this->loadViewsFrom(base_path("resources/views/vendor/{$this->package->shortName()}"), "{$this->package->shortName()}");
3236
}
3337
}

src/Http/Requests/AuthorizationConfirmation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public function handle($type = 'webhook')
3535
$data = $this->all();
3636
$data['type'] = $type;
3737

38-
return (new AuthorizationConfirmationMessage())->handle($data);
38+
return (new AuthorizationConfirmationMessage)->handle($data);
3939
}
4040
}

src/Messages/AuthorizationConfirmation.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
class AuthorizationConfirmation implements Contract
1010
{
11+
public $paymentStatusCode;
12+
public $responsePayload;
13+
public $endToEndId;
14+
public $transactionStatus;
15+
1116
/**
1217
* handle a message
1318
*
@@ -45,8 +50,6 @@ public function format()
4550

4651
/**
4752
* returns collection of all fields
48-
*
49-
* @return collection
5053
*/
5154
public function list()
5255
{
@@ -62,6 +65,7 @@ private function saveTransaction(): DuitNowTransaction
6265
{
6366
$transaction = DuitNowTransaction::where(['end_to_end_id' => $this->endToEndId])->firstOrNew();
6467

68+
$transaction->end_to_end_id = $this->endToEndId;
6569
$transaction->payment_status_code = $this->paymentStatusCode;
6670
$transaction->payment_substate = $this->transactionStatus;
6771
$transaction->response_payload = $this->responsePayload;
@@ -87,8 +91,8 @@ private function getTransaction()
8791
$this->paymentStatusCode = $response['header']['status']['code'];
8892
$this->saveTransaction();
8993
} catch (\Throwable $th) {
90-
\Log::debug([
91-
'DuitNow.Messages.AuthorizationConfirmation' => $th->getMessage(),
94+
\Log::debug('DuitNow.Messages.AuthorizationConfirmation', [
95+
'message' => $th->getMessage(),
9296
]);
9397

9498
throw new \Exception($th->getMessage(), 400);

src/Models/DuitNowTransaction.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ZarulIzham\DuitNowPayment\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\MorphTo;
67

78
class DuitNowTransaction extends Model
89
{
@@ -23,4 +24,9 @@ class DuitNowTransaction extends Model
2324
'request_payload' => 'object',
2425
'response_payload' => 'object',
2526
];
27+
28+
public function reference() : MorphTo
29+
{
30+
return $this->morphTo();
31+
}
2632
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::table('duitnow_transactions', function (Blueprint $table) {
12+
$table->string('reference_type', 100)->nullable()->after('reference_id');
13+
});
14+
15+
Schema::table('duitnow_banks', function (Blueprint $table) {
16+
$table->boolean('is_active')->nullable()->default(true)->after('status');
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*
23+
* @return void
24+
*/
25+
public function down()
26+
{
27+
Schema::table('duitnow_transactions', function (Blueprint $table) {
28+
$table->dropColumn('reference_type');
29+
});
30+
31+
Schema::table('duitnow_banks', function (Blueprint $table) {
32+
$table->dropColumn('is_active');
33+
});
34+
}
35+
};

0 commit comments

Comments
 (0)