Skip to content

Commit

Permalink
feat: fetch paypal_client_id and paypal_client_secret from env
Browse files Browse the repository at this point in the history
  • Loading branch information
louislowjk authored Sep 20, 2024
2 parents b1aa4c8 + 2759a07 commit e501d89
Show file tree
Hide file tree
Showing 25 changed files with 246 additions and 137 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ dist
# dotnet
*.sln

# env
*.local

#Codespace
.vscode/settings.json
.devcontainer/update_settings.sh
.devcontainer/update_settings.sh
6 changes: 3 additions & 3 deletions advanced-integration/v2/client/html/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This guide will walk you through setting up and running the HTML/JS Advanced Int

### Installation

```sh
```bash
npm install
```

Expand All @@ -31,7 +31,7 @@ npm install
- Rename the .env.example file to .env
- Update the following keys with their actual values -

```sh
```bash
PAYPAL_CLIENT_ID=<PAYPAL_CLIENT_ID>
```

Expand Down Expand Up @@ -61,7 +61,7 @@ npm install

- **Start the client**:

```sh
```bash
npm run dev
```

Expand Down
6 changes: 3 additions & 3 deletions advanced-integration/v2/client/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This guide will walk you through setting up and running the React Advanced Integ

### Installation

```sh
```bash
npm install
```

Expand All @@ -31,7 +31,7 @@ npm install
- Rename the .env.example file to .env
- Update the following keys with their actual values -

```sh
```bash
PAYPAL_CLIENT_ID=<PAYPAL_CLIENT_ID>
```

Expand Down Expand Up @@ -61,7 +61,7 @@ npm install

- **Start the client**:

```sh
```bash
npm run dev
```

Expand Down
35 changes: 26 additions & 9 deletions advanced-integration/v2/server/dotnet/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
# Advanced Integartion .NET Sample

PayPal Advanced Integration sample in .NET

## Running the sample

1. Build the server
1. **Add your API credentials to the environment:**

- **Windows**

```powershell
$env:PAYPAL_CLIENT_ID = "<PAYPAL_CLIENT_ID>"
$env:PAYPAL_CLIENT_SECRET = "<PAYPAL_CLIENT_SECRET>"
```
- **Unix**
```bash
export PAYPAL_CLIENT_ID="<PAYPAL_CLIENT_ID>"
export PAYPAL_CLIENT_SECRET="<PAYPAL_CLIENT_SECRET>"
```
2. **Build the server**
~~~
dotnet restore
~~~
```bash
dotnet restore
```

2. Run the server
3. **Run the server**

~~~
dotnet run
~~~
```bash
dotnet run
```

3. Go to [http://localhost:8080/](http://localhost:8080/)
4. Go to [http://localhost:8080/](http://localhost:8080/)
10 changes: 2 additions & 8 deletions advanced-integration/v2/server/dotnet/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ public static void Main(string[] args)

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(
(context, config) =>
{
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
}
)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseUrls("http://localhost:8080");
Expand Down Expand Up @@ -66,11 +60,11 @@ public class CheckoutController : Controller
private IConfiguration _configuration { get; }
private string _paypalClientId
{
get { return _configuration["PAYPAL_CLIENT_ID"]; }
get { return Environment.GetEnvironmentVariable("PAYPAL_CLIENT_ID"); }
}
private string _paypalClientSecret
{
get { return _configuration["PAYPAL_CLIENT_SECRET"]; }
get { return Environment.GetEnvironmentVariable("PAYPAL_CLIENT_SECRET"); }
}
private readonly string _base = "https://api-m.sandbox.paypal.com";

Expand Down
4 changes: 0 additions & 4 deletions advanced-integration/v2/server/dotnet/appsettings.json

This file was deleted.

35 changes: 26 additions & 9 deletions advanced-integration/v2/server/java/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
# Advanced Integartion Java Sample

PayPal Advanced Integration sample in Java

## Running the sample

1. Build the server
1. Add your API credentials to the environment:

- **Windows**

```powershell
$env:PAYPAL_CLIENT_ID = "<PAYPAL_CLIENT_ID>"
$env:PAYPAL_CLIENT_SECRET = "<PAYPAL_CLIENT_SECRET>"
```
- **Unix**
```bash
export PAYPAL_CLIENT_ID="<PAYPAL_CLIENT_ID>"
export PAYPAL_CLIENT_SECRET="<PAYPAL_CLIENT_SECRET>"
```
2. Build the server
~~~
mvn clean install
~~~
```bash
mvn clean install
```

2. Run the server
3. Run the server

~~~
mvn spring-boot:run
~~~
```bash
mvn spring-boot:run
```

3. Go to [http://localhost:8080/](http://localhost:8080/)
4. Go to [http://localhost:8080/](http://localhost:8080/)
2 changes: 1 addition & 1 deletion advanced-integration/v2/server/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<url/>
</scm>
<properties>
<java.version>22</java.version>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
spring.application.name=v2-java
PAYPAL_CLIENT_ID=<PAYPAL_CLIENT_ID>
PAYPAL_CLIENT_SECRET=<PAYPAL_CLIENT_SECRET>
PAYPAL_CLIENT_ID=${PAYPAL_CLIENT_ID}
PAYPAL_CLIENT_SECRET=${PAYPAL_CLIENT_SECRET}
34 changes: 25 additions & 9 deletions advanced-integration/v2/server/node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,32 @@ PayPal Advanced Integration sample in Node.js

## Running the sample

1. Install the packages
1. Add your API credentials to the environment:

```sh
npm install
```
- **Windows**

2. Run the server
```powershell
$env:PAYPAL_CLIENT_ID = "<PAYPAL_CLIENT_ID>"
$env:PAYPAL_CLIENT_SECRET = "<PAYPAL_CLIENT_SECRET>"
```
```sh
npm run start
```
- **Unix**
3. Go to [http://localhost:8080/](http://localhost:8080/)
```bash
export PAYPAL_CLIENT_ID="<PAYPAL_CLIENT_ID>"
export PAYPAL_CLIENT_SECRET="<PAYPAL_CLIENT_SECRET>"
```
2. Install the packages
```bash
npm install
```

3. Run the server

```bash
npm run start
```

4. Go to [http://localhost:8080/](http://localhost:8080/)
20 changes: 17 additions & 3 deletions advanced-integration/v2/server/php/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,23 @@ This sample app demonstrates how to integrate with ACDC using PayPal's REST APIs

## How to Run Locally

1. Replace your Client ID & Client Secret in the server/.env file:
2. Open the `.env` file in a text editor and replace the placeholders with the appropriate values.
3. Follow the below instructions to setup & run server.
1. Add your API credentials to the environment:

- **Windows**

```powershell
$env:PAYPAL_CLIENT_ID = "<PAYPAL_CLIENT_ID>"
$env:PAYPAL_CLIENT_SECRET = "<PAYPAL_CLIENT_SECRET>"
```
- **Unix**
```bash
export PAYPAL_CLIENT_ID="<PAYPAL_CLIENT_ID>"
export PAYPAL_CLIENT_SECRET="<PAYPAL_CLIENT_SECRET>"
```
2. Follow the below instructions to setup & run server.
## Install the Composer
Expand Down
2 changes: 0 additions & 2 deletions advanced-integration/v2/server/php/server/.env

This file was deleted.

31 changes: 16 additions & 15 deletions advanced-integration/v2/server/php/server/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@

use GuzzleHttp\Client;

$env = parse_ini_file('.env');

$PAYPAL_CLIENT_ID = $env['PAYPAL_CLIENT_ID'];
$PAYPAL_CLIENT_SECRET = $env['PAYPAL_CLIENT_SECRET'];
$PAYPAL_CLIENT_ID = getenv('PAYPAL_CLIENT_ID');
$PAYPAL_CLIENT_SECRET = getenv('PAYPAL_CLIENT_SECRET');
$base = "https://api-m.sandbox.paypal.com";

/**
* Generate an OAuth 2.0 access token for authenticating with PayPal REST APIs.
* @see https://developer.paypal.com/api/rest/authentication/
*/
function generateAccessToken() {
function generateAccessToken()
{
global $PAYPAL_CLIENT_ID, $PAYPAL_CLIENT_SECRET, $base;

if (!$PAYPAL_CLIENT_ID || !$PAYPAL_CLIENT_SECRET) {
throw new Exception("MISSING_API_CREDENTIALS");
}

$auth = base64_encode($PAYPAL_CLIENT_ID . ":" . $PAYPAL_CLIENT_SECRET);

// Disabling certificate validation for local development
$client = new Client(['verify' => false]);
$response = $client->post("$base/v1/oauth2/token", [
Expand All @@ -41,11 +40,12 @@ function generateAccessToken() {
* Create an order to start the transaction.
* @see https://developer.paypal.com/docs/api/orders/v2/#orders_create
*/
function createOrder($cart) {
function createOrder($cart)
{
global $base;

$accessToken = generateAccessToken();

// Disabling certificate validation for local development
$client = new Client(['verify' => false]);
$payload = [
Expand Down Expand Up @@ -75,7 +75,8 @@ function createOrder($cart) {
* Capture payment for the created order to complete the transaction.
* @see https://developer.paypal.com/docs/api/orders/v2/#orders_capture
*/
function captureOrder($orderID) {
function captureOrder($orderID)
{
global $base;

$accessToken = generateAccessToken();
Expand All @@ -92,7 +93,8 @@ function captureOrder($orderID) {
return handleResponse($response);
}

function handleResponse($response) {
function handleResponse($response)
{
$jsonResponse = json_decode($response->getBody(), true);
return [
'jsonResponse' => $jsonResponse,
Expand All @@ -101,7 +103,7 @@ function handleResponse($response) {
}

$endpoint = $_SERVER['REQUEST_URI'];
if($endpoint === '/') {
if ($endpoint === '/') {
try {
$response = [
"message" => "Server is running"
Expand All @@ -114,7 +116,7 @@ function handleResponse($response) {
}
}

if($endpoint === '/api/orders') {
if ($endpoint === '/api/orders') {
$data = json_decode(file_get_contents('php://input'), true);
$cart = $data['cart'];
header('Content-Type: application/json');
Expand All @@ -128,7 +130,7 @@ function handleResponse($response) {
}


if(str_ends_with($endpoint, '/capture')) {
if (str_ends_with($endpoint, '/capture')) {
$urlSegments = explode('/', $endpoint);
end($urlSegments); // Will set the pointer to the end of array
$orderID = prev($urlSegments);
Expand All @@ -140,5 +142,4 @@ function handleResponse($response) {
echo json_encode(['error' => $e->getMessage()]);
http_response_code(500);
}
}
?>
}
Loading

0 comments on commit e501d89

Please sign in to comment.