Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paymob Flash New Integration #35

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/nafezly-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
return [
#PAYMOB
'PAYMOB_API_KEY' => env('PAYMOB_API_KEY'),
'PAYMOB_PUBLIC_API_KEY' => env('PAYMOB_PUBLIC_API_KEY'),
'PAYMOB_SECRET_API_KEY' => env('PAYMOB_SECRET_API_KEY'),
'PAYMOB_INTEGRATION_ID' => env('PAYMOB_INTEGRATION_ID'),
'PAYMOB_IFRAME_ID' => env('PAYMOB_IFRAME_ID'),
'PAYMOB_HMAC' => env('PAYMOB_HMAC'),
'PAYMOB_CURRENCY'=> env('PAYMOB_CURRENCY',"EGP"),

#PAYMOB UNIFIED CHECKOUT, aka: Flash Checkout, ex: 5000,5001, default wii take PAYMOB_INTEGRATION_ID and PAYMOB_WALLET_INTEGRATION_ID as values,
'PAYMOB_UNIFIED_INTEGRATION_IDS' => env('PAYMOB_UNIFIED_INTEGRATION_IDS'),

#HYPERPAY
'HYPERPAY_BASE_URL' => env('HYPERPAY_BASE_URL', "https://eu-test.oppwa.com"),
'HYPERPAY_URL' => env('HYPERPAY_URL', env('HYPERPAY_BASE_URL') . "/v1/checkouts"),
Expand Down
78 changes: 44 additions & 34 deletions src/Classes/PaymobPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

namespace Nafezly\Payments\Classes;

use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Nafezly\Payments\Exceptions\MissingPaymentInfoException;
use Nafezly\Payments\Interfaces\PaymentInterface;
use Nafezly\Payments\Classes\BaseController;

class PaymobPayment extends BaseController implements PaymentInterface
{
private $paymob_api_key;
private $paymob_public_key;
private $paymob_secret_key;
private $paymob_integration_id;
private $paymob_iframe_id;


public function __construct()
{
$this->paymob_api_key = config('nafezly-payments.PAYMOB_API_KEY');
$this->paymob_public_key = config('nafezly-payments.PAYMOB_PUBLIC_API_KEY');
$this->paymob_secret_key = config('nafezly-payments.PAYMOB_SECRET_API_KEY');
$this->paymob_integration_id = config('nafezly-payments.PAYMOB_INTEGRATION_ID');
$this->paymob_iframe_id = config("nafezly-payments.PAYMOB_IFRAME_ID");
$this->currency = config("nafezly-payments.PAYMOB_CURRENCY");
}

Expand All @@ -31,34 +32,43 @@ public function __construct()
* @param null $user_email
* @param null $user_phone
* @param null $source
* @return void
* @throws MissingPaymentInfoException
* @return array
* @throws MissingPaymentInfoException|ConnectionException|RequestException|\Random\RandomException
*/
public function pay($amount = null, $user_id = null, $user_first_name = null, $user_last_name = null, $user_email = null, $user_phone = null, $source = null)
{
$this->setPassedVariablesToGlobal($amount,$user_id,$user_first_name,$user_last_name,$user_email,$user_phone,$source);
$required_fields = ['amount', 'user_first_name', 'user_last_name', 'user_email', 'user_phone'];
$this->checkRequiredFields($required_fields, 'PayMob');

$request_new_token = Http::withHeaders(['content-type' => 'application/json'])
->post('https://accept.paymobsolutions.com/api/auth/tokens', [
"api_key" => $this->paymob_api_key
])->json();

$get_order = Http::withHeaders(['content-type' => 'application/json'])
->post('https://accept.paymobsolutions.com/api/ecommerce/orders', [
"auth_token" => $request_new_token['token'],
"delivery_needed" => "false",
"amount_cents" => $this->amount * 100,
"items" => []
])->json();

$get_url_token = Http::withHeaders(['content-type' => 'application/json'])
->post('https://accept.paymobsolutions.com/api/acceptance/payment_keys', [
"auth_token" => $request_new_token['token'],
"expiration" => 36000,
"amount_cents" => $get_order['amount_cents'],
"order_id" => $get_order['id'],
// New integration (Flash) that we are forced to use now doesnt let us register order first, so we generate one for us.
// Docs: https://developers.paymob.com/egypt/checkout-api/integration-guide-and-api-reference/create-intention-payment-api
$merchant_order_id = str($this->paymob_public_key)
->beforeLast('_')
->append('-')
->append(now()->getTimestampMs())
->append('-')
->append(bin2hex(random_bytes(16)))
->toString();

$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Token ' . $this->paymob_secret_key,
])
->post('https://accept.paymob.com/v1/intention/', [
"amount" => $this->amount * 100,
"currency" => $this->currency,
"payment_methods" => [
(int)$this->paymob_integration_id,
],
"special_reference" => $merchant_order_id,
"items" => [],
"customer" => [
"first_name" => $this->user_first_name,
"last_name" => $this->user_last_name,
"email" => $this->user_email,
],
"billing_data" => [
"apartment" => "NA",
"email" => $this->user_email,
Expand All @@ -74,14 +84,14 @@ public function pay($amount = null, $user_id = null, $user_first_name = null, $u
"last_name" => $this->user_last_name,
"state" => "NA"
],
"currency" => $this->currency,
"integration_id" => $this->paymob_integration_id
])->json();
])
->throw()
->json();

return [
'payment_id'=>$get_order['id'],
'payment_id' => $merchant_order_id,
'html' => "",
'redirect_url'=>"https://accept.paymobsolutions.com/api/acceptance/iframes/" . $this->paymob_iframe_id . "?payment_token=" . $get_url_token['token']
'redirect_url' => "https://accept.paymob.com/unifiedcheckout/?publicKey=$this->paymob_public_key&clientSecret={$response['client_secret']}"
];
}

Expand All @@ -97,14 +107,14 @@ public function verify(Request $request): array
if ($request['success'] == "true") {
return [
'success' => true,
'payment_id'=>$request['order'],
'payment_id'=>$request['merchant_order_id'] ?? $request['order'],
'message' => __('nafezly::messages.PAYMENT_DONE'),
'process_data' => $request->all()
];
} else {
return [
'success' => false,
'payment_id'=>$request['order'],
'payment_id'=>$request['merchant_order_id'] ?? $request['order'],
'message' => __('nafezly::messages.PAYMENT_FAILED_WITH_CODE',['CODE'=>$this->getErrorMessage($request['txn_response_code'])]),
'process_data' => $request->all()
];
Expand All @@ -113,7 +123,7 @@ public function verify(Request $request): array
} else {
return [
'success' => false,
'payment_id'=>$request['order'],
'payment_id'=>$request['merchant_order_id'] ?? $request['order'],
'message' => __('nafezly::messages.PAYMENT_FAILED'),
'process_data' => $request->all()
];
Expand Down
84 changes: 44 additions & 40 deletions src/Classes/PaymobWalletPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

namespace Nafezly\Payments\Classes;

use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Nafezly\Payments\Exceptions\MissingPaymentInfoException;
use Nafezly\Payments\Interfaces\PaymentInterface;
use Nafezly\Payments\Classes\BaseController;

class PaymobWalletPayment extends BaseController implements PaymentInterface
{
private $paymob_api_key;
private $paymob_public_key;
private $paymob_secret_key;
private $paymob_wallet_integration_id;

public function __construct()
{
$this->paymob_api_key = config('nafezly-payments.PAYMOB_API_KEY');
$this->currency = config("nafezly-payments.PAYMOB_CURRENCY");
$this->paymob_public_key = config('nafezly-payments.PAYMOB_PUBLIC_API_KEY');
$this->paymob_secret_key = config('nafezly-payments.PAYMOB_SECRET_API_KEY');
$this->paymob_wallet_integration_id = config("nafezly-payments.PAYMOB_WALLET_INTEGRATION_ID");
$this->currency = config("nafezly-payments.PAYMOB_CURRENCY");
}

/**
Expand All @@ -28,33 +31,43 @@ public function __construct()
* @param null $user_email
* @param null $user_phone
* @param null $source
* @return void
* @throws MissingPaymentInfoException
* @return array
* @throws MissingPaymentInfoException|ConnectionException|RequestException|\Random\RandomException
*/
public function pay($amount = null, $user_id = null, $user_first_name = null, $user_last_name = null, $user_email = null, $user_phone = null, $source = null)
{
$this->setPassedVariablesToGlobal($amount,$user_id,$user_first_name,$user_last_name,$user_email,$user_phone,$source);
$required_fields = ['amount', 'user_first_name', 'user_last_name', 'user_email', 'user_phone'];
$this->checkRequiredFields($required_fields, 'PayMob');

$request_new_token = Http::withHeaders(['content-type' => 'application/json'])
->post('https://accept.paymobsolutions.com/api/auth/tokens', [
"api_key" => $this->paymob_api_key
])->json();
// New integration (Flash) that we are forced to use now doesnt let us register order first, so we generate one for us.
// Docs: https://developers.paymob.com/egypt/checkout-api/integration-guide-and-api-reference/create-intention-payment-api
$merchant_order_id = str($this->paymob_public_key)
->beforeLast('_')
->append('-')
->append(now()->getTimestampMs())
->append('-')
->append(bin2hex(random_bytes(16)))
->toString();

$get_order = Http::withHeaders(['content-type' => 'application/json'])
->post('https://accept.paymobsolutions.com/api/ecommerce/orders', [
"auth_token" => $request_new_token['token'],
"delivery_needed" => "false",
"amount_cents" => $this->amount * 100,
"items" => []
])->json();
$get_url_token = Http::withHeaders(['content-type' => 'application/json'])
->post('https://accept.paymobsolutions.com/api/acceptance/payment_keys', [
"auth_token" => $request_new_token['token'],
"expiration" => 36000,
"amount_cents" => $get_order['amount_cents'],
"order_id" => $get_order['id'],
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Token ' . $this->paymob_secret_key,
])
->post('https://accept.paymob.com/v1/intention/', [
"amount" => $this->amount * 100,
"currency" => $this->currency,
"payment_methods" => [
(int)$this->paymob_wallet_integration_id,
],
"special_reference" => $merchant_order_id,
"items" => [],
"customer" => [
"first_name" => $this->user_first_name,
"last_name" => $this->user_last_name,
"email" => $this->user_email,
],
"billing_data" => [
"apartment" => "NA",
"email" => $this->user_email,
Expand All @@ -70,23 +83,14 @@ public function pay($amount = null, $user_id = null, $user_first_name = null, $u
"last_name" => $this->user_last_name,
"state" => "NA"
],
"currency" => $this->currency,
"integration_id" => $this->paymob_wallet_integration_id,
'lock_order_when_paid'=>true
])->json();
])
->throw()
->json();

$get_pay_link = Http::withHeaders(['content-type' => 'application/json'])
->post('https://accept.paymob.com/api/acceptance/payments/pay', [
'source'=>[
"identifier"=>$this->user_phone,
'subtype'=>"WALLET"
],
"payment_token"=>$get_url_token['token']
])->json();
return [
'payment_id'=>$get_order['id'],
'payment_id' => $merchant_order_id,
'html' => "",
'redirect_url'=>$get_pay_link['redirect_url']
'redirect_url' => "https://accept.paymob.com/unifiedcheckout/?publicKey=$this->paymob_public_key&clientSecret={$response['client_secret']}"
];

}
Expand All @@ -103,14 +107,14 @@ public function verify(Request $request): array
if ($request['success'] == "true") {
return [
'success' => true,
'payment_id'=>$request['order'],
'payment_id'=>$request['merchant_order_id'] ?? $request['order'],
'message' => __('nafezly::messages.PAYMENT_DONE'),
'process_data' => $request->all()
];
} else {
return [
'success' => false,
'payment_id'=>$request['order'],
'payment_id'=>$request['merchant_order_id'] ?? $request['order'],
'message' => __('nafezly::messages.PAYMENT_FAILED_WITH_CODE',['CODE'=>$this->getErrorMessage($request['txn_response_code'])]),
'process_data' => $request->all()
];
Expand All @@ -119,7 +123,7 @@ public function verify(Request $request): array
} else {
return [
'success' => false,
'payment_id'=>$request['order'],
'payment_id'=>$request['merchant_order_id'] ?? $request['order'],
'message' => __('nafezly::messages.PAYMENT_FAILED'),
'process_data' => $request->all()
];
Expand Down
Loading