Skip to content

Commit

Permalink
fix: fixed paypal gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
CorwinDev committed May 28, 2024
1 parent 2fc32aa commit 8f04eac
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions app/Extensions/Gateways/PayPal/PayPal.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function getMetadata()
{
return [
'display_name' => 'PayPal',
'version' => '1.0.0',
'version' => '1.0.1',
'author' => 'Paymenter',
'website' => 'https://paymenter.org',
];
Expand Down Expand Up @@ -91,7 +91,36 @@ public function webhook(Request $request)
$data = json_decode($body, true);
if ($data['event_type'] == 'CHECKOUT.ORDER.APPROVED') {
$orderId = $data['resource']['purchase_units'][0]['reference_id'];
ExtensionHelper::paymentDone($orderId, 'PayPal', $data['resource']['id'] ?? null);
// Capture order
$client_id = ExtensionHelper::getConfig('PayPal', 'client_id');
$client_secret = ExtensionHelper::getConfig('PayPal', 'client_secret');
$live = ExtensionHelper::getConfig('PayPal', 'live');
if ($live) {
$url = 'https://api-m.paypal.com';
} else {
$url = 'https://api-m.sandbox.paypal.com';
}

$response = Http::withHeaders([
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Basic ' . base64_encode($client_id . ':' . $client_secret),
])->asForm()->post($url . '/v1/oauth2/token', [
'grant_type' => 'client_credentials',
]);

$token = $response->json()['access_token'];
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $token,
])->post($url . '/v2/checkout/orders/' . $data['resource']['id'] . '/capture', [
'note_to_payer' => 'Thank you for your payment!',
]);

if ($response->failed()) {
ExtensionHelper::error('PayPal', $response->json());
}

ExtensionHelper::paymentDone($orderId, 'PayPal', $data['resource']['id']);
}
}
}
Expand Down

0 comments on commit 8f04eac

Please sign in to comment.