Skip to content

Commit

Permalink
feat: getting klarna payment page, prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Klimchuk committed Jul 23, 2021
1 parent 03702dc commit 4cb36b7
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions core/mspklarna/services/klarna/klarnaservice.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use alroniks\mspklarna\KlarnaGatewayInterface as Klarna;
use Fig\Http\Message\RequestMethodInterface;
use GuzzleHttp\Client;
use League\Uri\Uri;
use League\Uri\UriTemplate;

require __DIR__ . '/../../vendor/autoload.php';

Expand All @@ -33,6 +35,38 @@ public function __construct(modX $modx, array $config = [])
$this->modx->lexicon->load('mspklarna:default');
}

public function requestRedirect(string $sid)
{

$uri = Uri::createFromBaseUri(
(new UriTemplate('/payments/v1/sessions/{kp_session_id}'))->expand(['kp_session_id' => $sid]),
$this->config[Klarna::OPTION_GATEWAY_URL]
);

echo $uri;

return $this->getClient()->request(
RequestMethodInterface::METHOD_POST,
'/hpp/v1/sessions',
[
'auth' => [
$this->config[Klarna::OPTION_USERNAME],
$this->config[Klarna::OPTION_PASSWORD],
],
'json' => [
'payment_session_url' => (string)$uri,
// 'merchant_urls' => [
// 'success' => '',
// 'cancel' => '',
// 'back' => '',
// 'failure' => '',
// 'error' => ''
// ]
]
]
);
}

/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
Expand All @@ -53,9 +87,16 @@ public function requestPayment(msOrder $order)
'purchase_country' => 'GB',
'purchase_currency' => 'GBP',
'locale' => 'en-GB',
'order_amount' => 10,
'order_amount' => 1000,
'order_tax_amount' => 0,

'order_lines' => [
[
'name' => 'Некий товар',
'quantity' => 2,
'total_amount' => 1000,
'unit_price' => 500,
]
]
]
]
);
Expand All @@ -76,8 +117,11 @@ protected function setUpConfig(msOrder $order): void
$this->config[Klarna::OPTION_GATEWAY_URL] = $this->config[Klarna::OPTION_GATEWAY_URL_TEST];
}

// need replace region

$this->config[Klarna::OPTION_GATEWAY_URL] = str_replace(
'<region>',
$this->config[Klarna::OPTION_GATEWAY_REGION],
$this->config[Klarna::OPTION_GATEWAY_URL]
);
}

protected function getClient(): Client
Expand Down

0 comments on commit 4cb36b7

Please sign in to comment.