Completed
Push — master ( aa410a...b7205b )
by Martin
06:25
created

Klarna   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPaymentUrl() 0 24 2
A __construct() 0 3 1
1
<?php
2
3
namespace App\PaymentProvider;
4
5
use Sofort\SofortLib\Sofortueberweisung;
6
use App\Purchase;
7
use App\Exceptions\PaymentProviderException;
8
use Illuminate\Support\Facades\Log;
9
10
/**
11
 * Class to process requests to Klarna
12
 * 
13
 * Find docs @ https://github.com/sofort/sofortlib-php
14
 */
15
class Klarna
16
{
17
    /**
18
     * API-Object; initialized by constructor
19
     */
20
    protected $sofortApi;
21
22
    /**
23
     * 
24
     * @param $configKey Configuration key for this exact project
0 ignored issues
show
Bug introduced by
The type App\PaymentProvider\Configuration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
     * @return void
26
     */
27
    public function __construct($configKey)
28
    {
29
        $this->sofortApi = new Sofortueberweisung($configKey);
30
    }
31
32
    /**
33
     * Generates for a given Purchase a payment request at Klarna and
34
     * returns a payment url where the customer can pay the purchase
35
     * 
36
     * @param $purchase the customer's purchase
0 ignored issues
show
Bug introduced by
The type App\PaymentProvider\the was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
     * @return string PaymentUrl to Klarna for the given purchase
38
     */
39
    public function getPaymentUrl(Purchase $purchase)
40
    {
41
        $this->sofortApi->setAmount($purchase->total());
42
        $this->sofortApi->setCurrencyCode('EUR');
43
        $this->sofortApi->setReason('Ticket purchase #' . $purchase->id);
44
        $this->sofortApi->setSuccessUrl(route('ts.payment.successful', [
45
            'purchase' => $purchase->random_id,
46
            'secret' => $purchase->payment_secret
47
        ]));
48
        $this->sofortApi->setAbortUrl(route('ts.payment.aborted', ['purchase' => $purchase->random_id]));
49
        $this->sofortApi->setTimeoutUrl(route('ts.payment.timedout', ['purchase' => $purchase->random_id]));
50
51
        $this->sofortApi->sendRequest();
52
53
        if ($this->sofortApi->isError()) {
54
            // SOFORT-API didn't accept the data
55
            Log::error($this->sofortApi->getErrors());
0 ignored issues
show
Bug introduced by
$this->sofortApi->getErrors() of type array is incompatible with the type string expected by parameter $message of Illuminate\Support\Facades\Log::error(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
            Log::error(/** @scrutinizer ignore-type */ $this->sofortApi->getErrors());
Loading history...
56
            throw new PaymentProviderException("SOFORT got errors...");
57
        }
58
        // get unique transaction-ID useful for check payment status
59
        $transactionId = $this->sofortApi->getTransactionId();
60
        Log::info('[Klarna] TransactionId for purchase #' . $purchase->id . ' is ' . $transactionId);
61
        // buyer must be redirected to $paymentUrl else payment cannot be successfully completed!
62
        return $this->sofortApi->getPaymentUrl();
63
    }
64
65
}
66