Skip to content

Commit

Permalink
Add fetch payment key request api from paymob method
Browse files Browse the repository at this point in the history
  • Loading branch information
shabayekdes committed Dec 6, 2021
1 parent 7dae66d commit d9b6224
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Drivers/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function customer(CustomerContract $customer)
*/
public function address(AddressContract $address)
{
$this->address = $address;
$this->address = $address->addressDetails();

return $this;
}
Expand Down
55 changes: 50 additions & 5 deletions src/Drivers/PaymobMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ public function purchase()
{
$token = $this->getAuthenticationToken();
$orderCreation = $this->orderCreation($token);
}
$paymentKey = $this->paymentKeyRequest($token, $orderCreation['id']);

return "{$this->url}acceptance/iframes/{$this->iframeID}?payment_token={$paymentKey}";
}
/**
* Complete payment
*
Expand All @@ -70,7 +72,7 @@ public function pay()
/**
* Authentication Request
*
* @return void
* @return string
*/
private function getAuthenticationToken()
{
Expand Down Expand Up @@ -100,21 +102,64 @@ private function orderCreation($token)
'delivery_needed' => false,
'merchant_order_id' => $this->transaction_id . "-" . rand(10000, 99999),
'merchant_id' => $this->merchantID,
'amount_cents' => (int) $this->mount,
'amount_cents' => (int) $this->mount * 100,
'currency' => "EGP",
'items' => $this->items,
'shipping_data' => [
"first_name" => $this->customer['first_name'],
"last_name" => $this->customer['last_name'],
"email" => $this->customer['email'],
"phone_number" => $this->customer['phone'],
],
]
];
try {
$response = $this->client->request('POST', $this->url . 'ecommerce/orders', ['body' => $postData]);
$response = $this->client->post("{$this->url}ecommerce/orders", [
'body' => json_encode($postData)
]);
return json_encode($response->getBody());
} catch (\Exception $e) {
throw new \Exception("Order not created success in paymob #" . $e->getMessage());
}
}
/**
* Get payment key request
*
* @param int $orderPayId
* @return string
*/
private function paymentKeyRequest($token, $orderPayId)
{
$postData = [
'auth_token' => $token,
'amount_cents' => (int) $this->mount * 100,
'expiration' => 3600,
'order_id' => $orderPayId,
'currency' => "EGP",
'integration_id' => $this->integrationID,
'billing_data' => [
"first_name" => $this->customer['first_name'],
"last_name" => $this->customer['last_name'] ?? 'NA',
"phone_number" => $this->customer['phone'],
"email" => $this->customer['email'],
"apartment" => $this->address['apartment'] ?? "NA",
"floor" => $this->address['floor'] ?? "NA",
"city" => $this->address['city'] ?? "NA",
"state" => $this->address['state'] ?? "NA",
"street" => $this->address['street'] ?? "NA",
"building" => $this->address['building'] ?? "NA",
"country" => "EG",
],
];

try {
$response = $this->client->post("{$this->url}acceptance/payment_keys", [
'body' => json_encode($postData)
]);

$result = json_decode($response->getBody(), true);
return $result['token'];
} catch (\Exception $e) {
throw new \Exception("Payment key request not created success in paymob #" . $e->getMessage());
}
}
}

0 comments on commit d9b6224

Please sign in to comment.