Skip to content

Commit

Permalink
Use Laravel Http
Browse files Browse the repository at this point in the history
  • Loading branch information
sawirricardo committed Feb 20, 2022
1 parent 4fd02d6 commit b2e7bbc
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 12 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
],
"require": {
"php": "^8.0",
"guzzlehttp/guzzle": "^7.4",
"illuminate/contracts": "^8.73 | ^9.0",
"sawirricardo/midtrans-api": "^0.1",
"spatie/laravel-package-tools": "^1.9.2"
Expand Down
2 changes: 1 addition & 1 deletion src/Facades/Midtrans.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* @see \Sawirricardo\Midtrans\Midtrans
* @method static \Sawirricardo\Midtrans\Midtrans new()
* @method static \Sawirricardo\Midtrans\Laravel\Midtrans new()
*/
class Midtrans extends Facade
{
Expand Down
54 changes: 45 additions & 9 deletions src/Midtrans.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@

namespace Sawirricardo\Midtrans\Laravel;

use Illuminate\Support\Facades\Http;
use Sawirricardo\Midtrans\Laravel\Snap;

class Midtrans
{
private $client;
private string $serverKey;
private string $clientKey;
private bool $isProduction;
private bool $is3ds;
private bool $isSanitized;

public function __construct(
$serverKey,
$clientKey,
$isProduction = false,
$is3ds = false,
$isSanitized = false
string $serverKey,
string $clientKey,
bool $isProduction = false,
bool $is3ds = false,
bool $isSanitized = false
) {
$this->client = new \Sawirricardo\Midtrans\Midtrans($serverKey, $clientKey, $isProduction, $is3ds, $isSanitized);
$this->serverKey = $serverKey;
$this->clientKey = $clientKey;
$this->isProduction = $isProduction;
$this->is3ds = $is3ds;
$this->isSanitized = $isSanitized;
}

public static function makeFromConfig($config)
Expand All @@ -27,9 +38,34 @@ public static function makeFromConfig($config)
);
}

public function new(): \Sawirricardo\Midtrans\Midtrans
public function new()
{
return $this;
}

public function snap()
{
return new Snap($this->createHttpFactory(
$this->isProduction
? 'https://app.midtrans.com/snap/v1'
: 'https://app.sandbox.midtrans.com/snap/v1'
));
}

public function payment()
{
return new Payment($this->createHttpFactory(
$this->isProduction
? 'https://api.midtrans.com'
: 'https://api.sandbox.midtrans.com'
));
}

private function createHttpFactory(string $baseUrl)
{
return $this->client;
return Http::acceptJson()->asJson()
->withToken(base64_encode($this->serverKey . ':'), 'Basic')
->baseUrl($baseUrl);
}

public static function snapScripts(): string
Expand Down
138 changes: 138 additions & 0 deletions src/Notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

namespace Sawirricardo\Midtrans\Laravel;

use Sawirricardo\Midtrans\Dto\TransactionStatus;

class Notification
{
private TransactionStatus $transactionStatus;
private $callbackCreditCardChallenged;
private $callbackCreditCardSuccess;
private $callbackSettlement;
private $callbackExpired;
private $callbackPending;
private $callbackCancelled;
private $callbackDenied;

public function __construct(TransactionStatus $transactionStatus)
{
$this->transactionStatus = $transactionStatus;
}

public static function make(TransactionStatus|array $transactionStatus)
{
if (is_array($transactionStatus)) {
return new static(new TransactionStatus($transactionStatus));
}
return new static($transactionStatus);
}

/**
* Set payment status in merchant's database to 'challenge'
* @param callable $callback accepts \Sawirricardo\Midtrans\Dto\TransactionStatus
*/
public function whenCreditCardChallenged(callable $callback): static
{
$this->callbackCreditCardChallenged = $callback;
return $this;
}

/**
* Set payment status in merchant's database to 'success'
* @param callable $callback accepts \Sawirricardo\Midtrans\Dto\TransactionStatus
*/
public function whenCreditCardSuccess(callable $callback): static
{
$this->callbackCreditCardSuccess = $callback;
return $this;
}

/**
* Set payment status in merchant's database to 'settlement'
* @param callable $callback accepts \Sawirricardo\Midtrans\Dto\TransactionStatus
*/
public function whenSettlement(callable $callback): static
{
$this->callbackSettlement = $callback;
return $this;
}

/**
* Set payment status in merchant's database to 'expired'
* @param callable $callback accepts \Sawirricardo\Midtrans\Dto\TransactionStatus
*/
public function whenPending(callable $callback): static
{
$this->callbackPending = $callback;
return $this;
}

/**
* Set payment status in merchant's database to 'denied'
* @param callable $callback accepts \Sawirricardo\Midtrans\Dto\TransactionStatus
*/
public function whenDenied(callable $callback): static
{
$this->callbackDenied = $callback;
return $this;
}

/**
* Set payment status in merchant's database to 'expired'
* @param callable $callback accepts \Sawirricardo\Midtrans\Dto\TransactionStatus
*/
public function whenExpired(callable $callback): static
{
$this->callbackExpired = $callback;
return $this;
}

/**
* Set payment status in merchant's database to 'denied'
* @param callable $callback accepts \Sawirricardo\Midtrans\Dto\TransactionStatus
*/
public function whenCancelled(callable $callback): static
{
$this->callbackCancelled = $callback;
return $this;
}

public function listen()
{
if ($this->transactionStatus->transaction_status == 'capture') {
// For credit card transaction, we need to check whether transaction is challenge by FDS or not
if ($this->transactionStatus->payment_type == 'credit_card') {
if ($this->transactionStatus->fraud_status == 'challenge') {
if (is_callable($this->callbackCreditCardChallenged)) {
call_user_func($this->callbackCreditCardChallenged, $this->transactionStatus);
}
} else {
if (is_callable($this->callbackCreditCardSuccess)) {
call_user_func($this->callbackCreditCardSuccess, $this->transactionStatus);
}
}
}
} elseif ($this->transactionStatus->transaction_status == 'settlement') {
if (is_callable($this->callbackSettlement)) {
call_user_func($this->callbackSettlement, $this->transactionStatus);
}
} elseif ($this->transactionStatus->transaction_status == 'pending') {
if (is_callable($this->callbackPending)) {
call_user_func($this->callbackPending, $this->transactionStatus);
}
} elseif ($this->transactionStatus->transaction_status == 'deny') {
if (is_callable($this->callbackDenied)) {
call_user_func($this->callbackDenied, $this->transactionStatus);
}
} elseif ($this->transactionStatus->transaction_status == 'expire') {
if (is_callable($this->callbackExpired)) {
call_user_func($this->callbackExpired, $this->transactionStatus);
}
} elseif ($this->transactionStatus->transaction_status == 'cancel') {
if (is_callable($this->callbackCancelled)) {
call_user_func($this->callbackCancelled, $this->transactionStatus);
}
}
}
}
31 changes: 31 additions & 0 deletions src/Payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Sawirricardo\Midtrans\Laravel;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Sawirricardo\Midtrans\Dto\TransactionDto;
use Sawirricardo\Midtrans\Dto\TransactionStatus;

class Payment
{
private PendingRequest $http;

public function __construct(PendingRequest $http)
{
$this->http = $http;
}

public function status(string $orderIdOrTransactionId): TransactionStatus
{
return new TransactionStatus(
$this->http->get("/v2/{$orderIdOrTransactionId}/status")->collect()->toArray()
);
}

public function charge(TransactionDto $transactionDto): Response
{
return $this->http->post('/v2/charge')
->json($transactionDto->toArray());
}
}
25 changes: 25 additions & 0 deletions src/Snap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Sawirricardo\Midtrans\Laravel;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
use Sawirricardo\Midtrans\Dto\SnapTokenDto;
use Sawirricardo\Midtrans\Dto\TransactionDto;

class Snap
{
private PendingRequest $http;

public function __construct(PendingRequest $http)
{
$this->http = $http;
}

public function create(TransactionDto $transactionDto): SnapTokenDto
{
return new SnapTokenDto(
$this->http->post('/transactions', $transactionDto->toArray())->json()
);
}
}
5 changes: 3 additions & 2 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

use Sawirricardo\Midtrans\Laravel\Facades\Midtrans as LaravelMidtrans;
use Sawirricardo\Midtrans\Laravel\Midtrans;

if (! function_exists('midtrans')) {
function midtrans(): \Sawirricardo\Midtrans\Midtrans
if (!function_exists('midtrans')) {
function midtrans(): Midtrans
{
return app(LaravelMidtrans::class)::new();
}
Expand Down

0 comments on commit b2e7bbc

Please sign in to comment.