|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\Infrastructure\Sofort\Transfer; |
|
6
|
|
|
|
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use Sofort\SofortLib\Sofortueberweisung; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Facade in front of Sofortueberweisung, an API to generate URLs of Sofort's checkout process |
|
12
|
|
|
*/ |
|
13
|
|
|
class Client { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var Sofortueberweisung |
|
17
|
|
|
*/ |
|
18
|
|
|
private $api; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct( string $configkey ) { |
|
21
|
|
|
$this->api = new Sofortueberweisung( $configkey ); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Set API to use instead of the one chosen by the facade |
|
26
|
|
|
*/ |
|
27
|
|
|
public function setApi( Sofortueberweisung $sofortueberweisung ): void { |
|
28
|
|
|
$this->api = $sofortueberweisung; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Perform the given request and return a response |
|
33
|
|
|
* |
|
34
|
|
|
* @throws RuntimeException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function get( Request $request ): Response { |
|
37
|
|
|
|
|
38
|
|
|
// Mapping currency amount to 3rd party float format. Known flaw |
|
39
|
|
|
$this->api->setAmount( $request->getAmount()->getEuroFloat() ); |
|
40
|
|
|
|
|
41
|
|
|
$this->api->setCurrencyCode( $request->getCurrencyCode() ); |
|
42
|
|
|
|
|
43
|
|
|
$reasons = $request->getReasons(); |
|
44
|
|
|
$this->api->setReason( $reasons[0] ?? '', $reasons[1] ?? '' ); |
|
45
|
|
|
|
|
46
|
|
|
$this->api->setSuccessUrl( $request->getSuccessUrl(), true ); |
|
47
|
|
|
$this->api->setAbortUrl( $request->getAbortUrl() ); |
|
48
|
|
|
$this->api->setNotificationUrl( $request->getNotificationUrl() ); |
|
49
|
|
|
|
|
50
|
|
|
$this->api->sendRequest(); |
|
51
|
|
|
|
|
52
|
|
|
if ( $this->api->isError() ) { |
|
53
|
|
|
throw new RuntimeException( $this->api->getError() ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$response = new Response(); |
|
57
|
|
|
$response->setPaymentUrl( $this->api->getPaymentUrl() ); |
|
58
|
|
|
$response->setTransactionId( $this->api->getTransactionId() ); |
|
59
|
|
|
|
|
60
|
|
|
return $response; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|