Skip to content

Commit 224693f

Browse files
committed
Merge in Opayo tweaks
1 parent e570f1f commit 224693f

File tree

5 files changed

+59
-10
lines changed

5 files changed

+59
-10
lines changed

resources/views/opayo/threed-secure-response.blade.php

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
<title>Document</title>
88
</head>
99
<body>
10+
<div style="text-align: center;display: flex;align-content: center;justify-content: center;">
11+
Processing...
12+
</div>
1013
<script>
1114
(function () {
1215
if ( typeof window.CustomEvent === "function" ) return false;

src/Opayo.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Opayo implements OpayoInterface
1616
public function __construct()
1717
{
1818
$this->http = Http::baseUrl(
19-
config('services.opayo.env', 'test') == 'test' ?
19+
strtolower(config('services.opayo.env', 'test')) == 'test' ?
2020
'https://pi-test.sagepay.com/api/v1/' :
2121
'https://pi-live.sagepay.com/api/v1/'
2222
)->withHeaders([

src/OpayoPaymentType.php

+24-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Lunar\Models\Transaction;
99
use Lunar\Opayo\Facades\Opayo;
1010
use Lunar\Opayo\Responses\PaymentAuthorize;
11+
use Lunar\Opayo\Responses\ThreeDSecureResponse;
1112
use Lunar\PaymentTypes\AbstractPayment;
1213

1314
class OpayoPaymentType extends AbstractPayment
@@ -32,7 +33,7 @@ public function __construct()
3233
*
3334
* @return \Lunar\Base\DataTransferObjects\PaymentAuthorize
3435
*/
35-
public function authorize(): PaymentAuthorize
36+
public function authorize(): PaymentAuthorize|ThreeDSecureResponse
3637
{
3738
if (! $this->order) {
3839
if (! $this->order = $this->cart->order) {
@@ -59,6 +60,7 @@ public function authorize(): PaymentAuthorize
5960

6061
$response = Opayo::api()->post('transactions', $payload);
6162

63+
6264
if (! $response->successful()) {
6365
return new PaymentAuthorize(
6466
success: false,
@@ -69,7 +71,7 @@ public function authorize(): PaymentAuthorize
6971
$response = $response->object();
7072

7173
if ($response->status == '3DAuth') {
72-
return new PaymentAuthorize(
74+
return new ThreeDSecureResponse(
7375
success: true,
7476
status: Opayo::THREE_D_AUTH,
7577
acsUrl: $response->acsUrl,
@@ -88,8 +90,11 @@ public function authorize(): PaymentAuthorize
8890
success: $successful
8991
);
9092

93+
$status = $this->data['status'] ?? null;
94+
9195
if ($successful) {
9296
$this->order->update([
97+
'status' => $status ?? ($this->config['authorized'] ?? null),
9398
'placed_at' => now(),
9499
]);
95100
}
@@ -160,6 +165,7 @@ public function refund(Transaction $transaction, int $amount = 0, $notes = null)
160165

161166
$data = $response->object();
162167

168+
163169
if (! $response->successful() || isset($data->code)) {
164170
return new PaymentRefund(
165171
success: false,
@@ -189,7 +195,6 @@ public function refund(Transaction $transaction, int $amount = 0, $notes = null)
189195
/**
190196
* Handle the Three D Secure response.
191197
*
192-
* @return void
193198
*/
194199
public function threedsecure()
195200
{
@@ -222,6 +227,16 @@ public function threedsecure()
222227
$data = $response->object();
223228

224229
if (($data->statusCode ?? null) == '4026') {
230+
$this->order->transactions()->create([
231+
'success' => false,
232+
'type' => 'capture',
233+
'driver' => 'opayo',
234+
'amount' => $data->amount?->totalAmount ?: 0,
235+
'reference' => $data->transactionId,
236+
'status' => $data->status,
237+
'notes' => $data->statusDetail,
238+
'card_type' => 'unknown',
239+
]);
225240
return new PaymentAuthorize(
226241
success: false,
227242
status: Opayo::THREED_SECURE_FAILED
@@ -244,8 +259,12 @@ public function threedsecure()
244259
success: $successful
245260
);
246261

262+
$status = $this->data['status'] ?? null;
263+
264+
247265
if ($successful) {
248266
$this->order->update([
267+
'status' => $status ?? ($this->config['authorized'] ?? null),
249268
'placed_at' => now(),
250269
]);
251270
}
@@ -322,9 +341,9 @@ protected function getAuthPayload($type = 'Payment')
322341
'transType' => 'GoodsAndServicePurchase',
323342
'browserLanguage' => $this->data['browserLanguage'] ?? null,
324343
'challengeWindowSize' => $this->data['challengeWindowSize'] ?? null,
325-
'browserIP' => $this->data['ip'] ?? null,
344+
'browserIP' => $this->data['browserIP'] ?? null,
326345
'notificationURL' => route('opayo.threed.response'),
327-
'browserAcceptHeader' => $this->data['accept'] ?? null,
346+
'browserAcceptHeader' => $this->data['browserAcceptHeader'] ?? null,
328347
'browserJavascriptEnabled' => true,
329348
'browserUserAgent' => $this->data['browserUserAgent'] ?? null,
330349
'browserJavaEnabled' => (bool) ($this->data['browserJavaEnabled'] ?? null),

src/OpayoServiceProvider.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,30 @@ public function boot()
3030

3131
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
3232

33-
Blade::directive('opayoScripts', function () {
33+
Blade::directive('opayoScripts', function ($incVendor = true) {
3434
$url = 'https://pi-test.sagepay.com/api/v1/js/sagepay.js';
3535

3636
$manifest = json_decode(file_get_contents(__DIR__.'/../dist/mix-manifest.json'), true);
3737

3838
$jsUrl = asset('/vendor/opayo'.$manifest['/opayo.js']);
3939

40-
if (config('services.opayo.env', 'test') == 'live') {
40+
if (strtolower(config('services.opayo.env', 'test')) == 'live') {
4141
$url = 'https://pi-live.sagepay.com/api/v1/js/sagepay.js';
4242
}
4343

4444
$manifest = json_decode(file_get_contents(__DIR__.'/../dist/mix-manifest.json'), true);
4545

4646
$jsUrl = asset('/vendor/lunar'.$manifest['/opayo.js']);
4747

48-
return <<<EOT
49-
<script src="{$jsUrl}"></script>
48+
if (!$incVendor) {
49+
return <<<EOT
5050
<script src="{$url}"></script>
5151
EOT;
52+
}
53+
return <<<EOT
54+
<script src="{$jsUrl}" async></script>
55+
<script src="{$url}" async></script>
56+
EOT;
5257
});
5358

5459
$this->loadViewsFrom(__DIR__.'/../resources/views', 'lunar');
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Lunar\Opayo\Responses;
4+
5+
use Lunar\Base\DataTransferObjects\PaymentAuthorize;
6+
7+
class ThreeDSecureResponse extends PaymentAuthorize
8+
{
9+
public function __construct(
10+
public bool $success = false,
11+
public ?string $status = null,
12+
public ?string $acsUrl = null,
13+
public ?string $acsTransId = null,
14+
public ?string $dsTransId = null,
15+
public ?string $cReq = null,
16+
public ?string $paReq = null,
17+
public ?string $transactionId = null,
18+
public ?string $message = null
19+
) {
20+
//
21+
}
22+
}

0 commit comments

Comments
 (0)