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

php_payment should use the correct location id #350

Merged
merged 1 commit into from
Sep 30, 2022
Merged
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
43 changes: 22 additions & 21 deletions connect-examples/v2/php_checkout/README.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
# Useful Links

* [PHP SDK Page](https://developer.squareup.com/docs/sdks/php)
* [Checkout API Overview](https://developer.squareup.com/docs/checkout-api/what-it-does)
* [Checkout in the API Reference](https://developer.squareup.com/reference/square/checkout-api)
- [PHP SDK Page](https://developer.squareup.com/docs/sdks/php)
- [Checkout API Overview](https://developer.squareup.com/docs/checkout-api/what-it-does)
- [Checkout in the API Reference](https://developer.squareup.com/reference/square/checkout-api)

Square Checkout Demo
=========================
# Square Checkout Demo

This is a simple example application that utilizes Square's Checkout API. This examples does assume you are familiar with PHP development.

It takes a single payment, declared by the user, and creates an order to use in the Checkout API.
## Setup

## Setup

### Requirements

* 7.1 <= PHP
- 8.0 <= PHP

### Install the PHP client library

This sample already includes the `square/square` dependency in its `composer.json`
file. To install the client library:

1. Make sure you've downloaded Composer, following the instructions
[here](https://getcomposer.org/download/).
[here](https://getcomposer.org/download/).

2. Run the following command from the directory containing `composer.json`:

```
composer install
php composer.phar install
```

### Specify your application credentials

In order for the example to work, you must create a new file `.env` by copying the contents of the `.env.example` file. Edit this file with your application credentials and environment configuration.

Open your [developer dashboard](https://developer.squareup.com/). Now supply either production, sandbox, or both credentials. Open this file and update the following variables:
* WARNING: never upload .env with your credential/access_token

| Variable | Type | Description |
|------------------------|:---------|-----------------|
| ENVIRONMENT | `string` | `production` or `sandbox` depending on what type of endpoint you want to hit. For testing purposes please use the sandbox mode (already configured in the `.env`) |
| SQUARE_APPLICATION_ID | `string` | Application ID found on your Developer App Dashboard, Credentials tab. Must match the corresponding `ENVIRONMENT`. |
| SQUARE_ACCESS_TOKEN | `string` | Access Token found at the Developer App Dashboard, Credentials tab. Must match the corresponding `ENVIRONMENT`. |
| SQUARE_LOCATION_ID | `string` | Location found at the Developer App Dashboard, Location tab. Must match the corresponding `ENVIRONMENT`. |
- WARNING: never upload .env with your credential/access_token

| Variable | Type | Description |
| --------------------- | :------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ENVIRONMENT | `string` | `production` or `sandbox` depending on what type of endpoint you want to hit. For testing purposes please use the sandbox mode (already configured in the `.env`) |
| SQUARE_APPLICATION_ID | `string` | Application ID found on your Developer App Dashboard, Credentials tab. Must match the corresponding `ENVIRONMENT`. |
| SQUARE_ACCESS_TOKEN | `string` | Access Token found at the Developer App Dashboard, Credentials tab. Must match the corresponding `ENVIRONMENT`. |
| SQUARE_LOCATION_ID | `string` | Location found at the Developer App Dashboard, Location tab. Must match the corresponding `ENVIRONMENT`. |

## Running the sample

Expand All @@ -53,11 +53,12 @@ From the sample's root directory, run:

This will start the server on `localhost:8888`, which you can navigate to in your favorite browser.


For more information about Checkout please visit:
* https://developer.squareup.com/docs/checkout-api-overview
* https://developer.squareup.com/reference/square/checkout-api
* https://github.com/square/square-php-sdk/blob/master/docs/Api/CheckoutApi.md

- https://developer.squareup.com/docs/checkout-api-overview
- https://developer.squareup.com/reference/square/checkout-api
- https://github.com/square/square-php-sdk/blob/master/docs/Api/CheckoutApi.md

## Feedback
Rate this sample app [here](https://delighted.com/t/Z1xmKSqy)!

Rate this sample app [here](https://delighted.com/t/Z1xmKSqy)!
73 changes: 38 additions & 35 deletions connect-examples/v2/php_payment/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Useful Links

* [Square PHP SDK](https://developer.squareup.com/docs/sdks/php)
- [Web Payments SDK Overview](https://developer.squareup.com/docs/web-payments/overview)
- [Web Payments SDK Reference](https://developer.squareup.com/reference/sdks/web/payments)
- [Payments API Reference](https://developer.squareup.com/reference/square/payments-api)
- [Square PHP SDK](https://developer.squareup.com/docs/sdks/php)

* [Web Payments SDK Overview](https://developer.squareup.com/docs/web-payments/overview)
* [Web Payments SDK Reference](https://developer.squareup.com/reference/sdks/web/payments)
* [Payments API Reference](https://developer.squareup.com/reference/square/payments-api)

# Payment processing example: PHP

Expand All @@ -16,38 +17,37 @@ There are two sections in this README.

### Requirements

* 7.1 <= PHP
- 8.0 <= PHP

### Install the PHP client library

This sample already includes the `square/square` dependency in its `composer.json`
file. To install the client library:

1. Make sure you've downloaded Composer, following the instructions
[here](https://getcomposer.org/download/).
[here](https://getcomposer.org/download/).

2. Run the following command from the directory containing `composer.json`:

```
composer install
```
```
php composer.phar install
```

### Specify your application credentials

In order for the example to work, you must create a new file `.env` by copying the contents of the `.env.example` file. Edit this file with your application credentials and environment configuration.
<b>WARNING</b>: never save your credentials in your code.

<b>WARNING</b>: never save your credentials in your code.

Open your [developer dashboard](https://developer.squareup.com/apps). Now supply either production, sandbox, or both credentials. Open this file and update the following variables:
* WARNING: never upload .env with your credential/access_token

| Variable | Type | Description |
|------------------------|:---------|-----------------|
| ENVIRONMENT | `string` | `production` or `sandbox` depending on what type of endpoint you want to hit. For testing purposes please use the sandbox mode (already configured in the `.env`) |
| SQUARE_APPLICATION_ID | `string` | Application ID found on your Developer App Dashboard, Credentials tab. Must match the corresponding `ENVIRONMENT`. |
| SQUARE_ACCESS_TOKEN | `string` | Access Token found at the Developer App Dashboard, Credentials tab. Must match the corresponding `ENVIRONMENT`. |
| SQUARE_LOCATION_ID | `string` | Location found at the Developer App Dashboard, Location tab. Must match the corresponding `ENVIRONMENT`. |
- WARNING: never upload .env with your credential/access_token

| Variable | Type | Description |
| --------------------- | :------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ENVIRONMENT | `string` | `production` or `sandbox` depending on what type of endpoint you want to hit. For testing purposes please use the sandbox mode (already configured in the `.env`) |
| SQUARE_APPLICATION_ID | `string` | Application ID found on your Developer App Dashboard, Credentials tab. Must match the corresponding `ENVIRONMENT`. |
| SQUARE_ACCESS_TOKEN | `string` | Access Token found at the Developer App Dashboard, Credentials tab. Must match the corresponding `ENVIRONMENT`. |
| SQUARE_LOCATION_ID | `string` | Location found at the Developer App Dashboard, Location tab. Must match the corresponding `ENVIRONMENT`. |

## Running the sample

Expand All @@ -60,10 +60,10 @@ You can then visit [`localhost:8888`](http://localhost:8888) in your browser to
If you're using your sandbox credentials, you can test a valid credit card
payment by providing the following card information in the form:

* Card Number `4111 1111 1111 1111`
* Card CVV `111`
* Card Expiration (Any time in the future)
* Card Postal Code (Any valid US postal code)
- Card Number `4111 1111 1111 1111`
- Card CVV `111`
- Card Expiration (Any time in the future)
- Card Postal Code (Any valid US postal code)

You can find more testing values in this [article](https://developer.squareup.com/docs/testing/test-values).

Expand Down Expand Up @@ -101,19 +101,20 @@ When the page loads it renders the form defined in the **index.php** file. The p
- `sq-apple-pay.js`

- Provides the global method that fires a `fetch` request to the server after receiving the payment token.

```javascript
window.createPayment = async function(token) {
window.createPayment = async function (token) {
const dataJsonString = JSON.stringify({
token
token,
});

try {
const response = await fetch('process-payment', {
method: 'POST',
const response = await fetch("process-payment", {
method: "POST",
headers: {
'Content-Type': 'application/json'
"Content-Type": "application/json",
},
body: dataJsonString
body: dataJsonString,
});

const data = await response.json();
Expand All @@ -122,24 +123,25 @@ When the page loads it renders the form defined in the **index.php** file. The p
if (data.errors[0].detail) {
window.showError(data.errors[0].detail);
} else {
window.showError('Payment Failed.');
window.showError("Payment Failed.");
}
} else {
window.showSuccess('Payment Successful!');
window.showSuccess("Payment Successful!");
}
} catch (error) {
console.error('Error:', error);
console.error("Error:", error);
}
}
};
```

### Step 2: Charge the Payment Source Using the Token

All the remaining actions take place in the **process-payment.php** file. This server-side component uses the [Square PHP SDK](https://developer.squareup.com/docs/sdks/php) library to call the Connect V2 **Payments** API to charge the payment source using the token as shown in the following code fragment.
All the remaining actions take place in the **process-payment.php** file. This server-side component uses the [Square PHP SDK](https://developer.squareup.com/docs/sdks/php) library to call the Connect V2 **Payments** API to charge the payment source using the token as shown in the following code fragment.

```php
...
$square_client = new SquareClient([
'accessToken' => $access_token,
'accessToken' => $access_token,
'environment' => getenv('ENVIRONMENT')
]);

Expand Down Expand Up @@ -167,4 +169,5 @@ if ($response->isSuccess()) {
```

## Feedback
Rate this sample app [here](https://delighted.com/t/Z1xmKSqy)!

Rate this sample app [here](https://delighted.com/t/Z1xmKSqy)!
1 change: 1 addition & 0 deletions connect-examples/v2/php_payment/process-payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
// it with the same idempotency key without worrying about double charging
// the buyer.
$create_payment_request = new CreatePaymentRequest($token, Uuid::uuid4(), $money);
$create_payment_request->setLocationId($location_info->getId());

$response = $payments_api->createPayment($create_payment_request);

Expand Down
6 changes: 6 additions & 0 deletions connect-examples/v2/php_payment/utils/location-info.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class LocationInfo {
// Initialize the Square client.
var $currency;
var $country;
var $location_id;

function __construct() {
$access_token = getenv('SQUARE_ACCESS_TOKEN');
Expand All @@ -28,6 +29,7 @@ function __construct() {
'environment' => getenv('ENVIRONMENT')
]);
$location = $this->square_client->getLocationsApi()->retrieveLocation(getenv('SQUARE_LOCATION_ID'))->getResult()->getLocation();
$this->location_id = $location->getId();
$this->currency = $location->getCurrency();
$this->country = $location->getCountry();
}
Expand All @@ -39,6 +41,10 @@ public function getCurrency() {
public function getCountry() {
return $this->country;
}

public function getId() {
return $this->location_id;
}
}

$location_info = new LocationInfo();
Expand Down