Completed
Push — master ( 919a96...568a86 )
by wiese
73:55 queued 09:12
created

SofortUrlGenerator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 51
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A generateUrl() 0 23 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Presentation;
6
7
use WMDE\Fundraising\Frontend\Infrastructure\Sofort\Transfer\Client;
8
use WMDE\Fundraising\Frontend\Infrastructure\Sofort\Transfer\Request;
9
use WMDE\Euro\Euro;
10
use RuntimeException;
11
12
/**
13
 * Generate the URL of the Sofort checkout process
14
 */
15
class SofortUrlGenerator {
16
17
	private const CURRENCY = 'EUR';
18
19
	/**
20
	 * @var SofortUrlConfig
21
	 */
22
	private $config;
23
	/**
24
	 * @var Client
25
	 */
26
	private $client;
27
28
	public function __construct( SofortUrlConfig $config, Client $client ) {
29
		$this->config = $config;
30
		$this->client = $client;
31
	}
32
33
	/**
34
	 * Generate a URL to use (refer the donor to) to finalize a purchase on a 3rd party payment provider page
35
	 *
36
	 * @param int $internalItemId Internal (WMDE-use only) Id of the item to pay
37
	 * @param string $externalItemId External (3rd parties may use to reference the item with this) Id of the item to pay
38
	 * @param Euro $amount The amount of money to pay
39
	 * @param string $accessToken A token to return to the payment process after completing the 3rd party process
40
	 * @return string
41
	 */
42
	public function generateUrl( int $internalItemId, string $externalItemId, Euro $amount, string $accessToken ): string {
43
		$request = new Request();
44
		$request->setAmount( $amount );
45
		$request->setCurrencyCode( self::CURRENCY );
46
		$request->setReasons( [ $this->config->getReasonText(), $externalItemId ] );
47
		$request->setSuccessUrl(
48
			$this->config->getReturnUrl() . '?' . http_build_query( [
49
				'id' => $internalItemId,
50
				'accessToken' => $accessToken
51
			] )
52
		);
53
		$request->setAbortUrl( $this->config->getCancelUrl() );
54
		// @todo To use in T167882
55
		$request->setNotificationUrl( '' );
56
57
		try {
58
			$response = $this->client->get( $request );
59
		} catch ( RuntimeException $exception ) {
60
			throw new RuntimeException( 'Could not generate Sofort URL: ' . $exception->getMessage() );
61
		}
62
63
		return $response->getPaymentUrl();
64
	}
65
}
66