Skip to content

Commit

Permalink
feat(ATL-1383): Add payment intention key (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
NadaHamada authored Oct 28, 2024
1 parent 87a1e64 commit 5f7b2a0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Models/PaymentKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,27 @@ final class PaymentKey
public int $integrationId;
public string $orderId;
public int $expiration;
public ?string $provider = null;
public ?string $secretKey = null;
public ?int $motoIntegrationId = null;

public function __construct(
string $currency,
float $amount,
int $integrationId,
string $orderId,
int $expiration = 3600
int $expiration = 3600,
?string $provider = null,
?string $secretKey = null,
?int $motoIntegrationId = null
) {
$this->currency = $currency;
$this->amount = $amount;
$this->integrationId = $integrationId;
$this->orderId = $orderId;
$this->expiration = $expiration;
$this->provider = $provider;
$this->secretKey = $secretKey;
$this->motoIntegrationId = $motoIntegrationId;
}
}
44 changes: 44 additions & 0 deletions src/Paymob.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ public function createOrder(PaymentOrder $order): Paymob
* @param array $data order details
*/
public function createPaymentKey(PaymentKey $paymentKey): Paymob
{
if ($paymentKey->provider === 'paymob_flash') {
$this->createIntentionPaymentKey($paymentKey);
return $this;
}

$this->createAcceptancePaymentKey($paymentKey);
return $this;
}
public function createAcceptancePaymentKey(PaymentKey $paymentKey): Paymob
{
$response = Http::withHeaders(['Content-Type' => 'application/json'])
->post($this->api . 'acceptance/payment_keys', [
Expand Down Expand Up @@ -103,6 +113,40 @@ public function createPaymentKey(PaymentKey $paymentKey): Paymob
return $this;
}

public function createIntentionPaymentKey(PaymentKey $paymentKey): Paymob
{
$response = Http::withHeaders([
'Authorization' => 'Token ' . $paymentKey->secretKey,
'Content-Type' => 'application/json',
])->post($this->api . 'v1/intention/', [
'amount' => $paymentKey->amount,
'currency' => $paymentKey->currency,
'payment_methods' => [$paymentKey->motoIntegrationId],
'items' => [],
'special_reference' => $paymentKey->orderId,
'billing_data' => [
'apartment' => '',
'email' => '',
'floor' => '',
'first_name' => 'NA',
'last_name' => 'NA',
'street' => '',
'building' => '',
'phone_number' => 'NA',
'shipping_method' => '',
'postal_code' => '',
'city' => '',
'country' => '',
'state' => '',
],
]);

$this->response = new PaymentKeyResponse($response);
$this->paymentKeyToken = $this->response->getIntentionPaymentKeyToken();

return $this;
}

public function payWithSavedToken(string $cardToken): Paymob
{
try {
Expand Down
8 changes: 8 additions & 0 deletions src/Response/PaymentKeyResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ public function getPaymentKeyToken(): string
return $this->body->token;
}

/**
* Return card token
*/
public function getIntentionPaymentKeyToken(): string
{
return $this->body->payment_keys['key'];
}

private function handleResponseExceptions(): void
{
switch ($this->response->getStatusCode()) {
Expand Down

0 comments on commit 5f7b2a0

Please sign in to comment.