From b254b72c163618113952614882175447e55fd2fb Mon Sep 17 00:00:00 2001 From: Stephen Barlow Date: Tue, 26 Apr 2016 14:07:06 -0700 Subject: [PATCH] Add a PHP payment-processing example that uses the SquareConnect PHP client library --- .gitignore | 3 + connect-examples/v2/README.md | 13 +- connect-examples/v2/php_payment/README.md | 49 ++++++++ connect-examples/v2/php_payment/composer.json | 5 + connect-examples/v2/php_payment/index.html | 118 ++++++++++++++++++ .../v2/php_payment/process-card.php | 68 ++++++++++ 6 files changed, 251 insertions(+), 5 deletions(-) create mode 100644 connect-examples/v2/php_payment/README.md create mode 100644 connect-examples/v2/php_payment/composer.json create mode 100644 connect-examples/v2/php_payment/index.html create mode 100644 connect-examples/v2/php_payment/process-card.php diff --git a/.gitignore b/.gitignore index 2f7896d1d..321a5d586 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ target/ +vendor/ +composer.phar +composer.lock diff --git a/connect-examples/v2/README.md b/connect-examples/v2/README.md index 4109ff34e..03da150fe 100644 --- a/connect-examples/v2/README.md +++ b/connect-examples/v2/README.md @@ -1,12 +1,15 @@ -The project is structured under two folders: +This folder includes the following samples: -* rails_payment: rails server implementation, within here it also includes frontend implementations for: - * plain JS +* __rails_payment__: a rails server implementation. This example includes front-end +implementations for: + * Standard Javascript * JQuery * Angular 1 * React * Ember * JS + Bootstrap -* node_payment: node server implementation, within here there is a frontend implementation: - * plain JS +* __node_payment__: a node server implementation. This example includes a single, +standard Javascript front-end implementation. +* __php_payment__: a simple PHP server implementation with an accompanying payment +form. diff --git a/connect-examples/v2/php_payment/README.md b/connect-examples/v2/php_payment/README.md new file mode 100644 index 000000000..b8e81996e --- /dev/null +++ b/connect-examples/v2/php_payment/README.md @@ -0,0 +1,49 @@ +# Payment processing example: PHP + +This sample demonstrates processing card payments with Square Connect API, using the +Square Connect PHP client library. + +## Setup + +### Install the PHP client library + +This sample already includes the `square/connect` 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/). + +2. Run the following command from the directory containing `composer.json`: + + php composer.phar install + +### Provide required credentials + +Both `process-card.php` and `index.html` have values near the top of the file +that you need to replace with various credentials associated with your application. +If you're just testing things out, it's recommended that you use your _sandbox_ +credentials for now. See +[this article](https://docs.connect.squareup.com/articles/using-sandbox/) +for more information on the API sandbox. + +You can `grep` for `REPLACE_ME` to find all of the fields to replace. + + +## Running the sample + +From the sample's root directory, run: + + php -S localhost:8000 + +You can then visit `localhost:8000` in your browser to see the card form. + +If you're using your sandbox credentials, you can test out an valid credit card +transaction by providing the following card information in the form: + +* Card Number 4532 7597 3454 5858 +* Card CVV 111 +* Card Expiration (Any time in the future) +* Card Postal Code (Any valid US postal code) + +**Note that if you are _not_ using your sandbox credentials and you enter _real_ +credit card information, YOU WILL CHARGE THE CARD.** diff --git a/connect-examples/v2/php_payment/composer.json b/connect-examples/v2/php_payment/composer.json new file mode 100644 index 000000000..584f63705 --- /dev/null +++ b/connect-examples/v2/php_payment/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "square/connect": "*" + } +} diff --git a/connect-examples/v2/php_payment/index.html b/connect-examples/v2/php_payment/index.html new file mode 100644 index 000000000..1b4ba8862 --- /dev/null +++ b/connect-examples/v2/php_payment/index.html @@ -0,0 +1,118 @@ + + + My Payment Form + + + + + + +

My Payment Form

+ +
+ +
+ +
+ +
+ +
+ + +
+ +
+ + diff --git a/connect-examples/v2/php_payment/process-card.php b/connect-examples/v2/php_payment/process-card.php new file mode 100644 index 000000000..b9bcea25d --- /dev/null +++ b/connect-examples/v2/php_payment/process-card.php @@ -0,0 +1,68 @@ + $nonce, + + # Monetary amounts are specified in the smallest unit of the applicable currency. + # This amount is in cents. It's also hard-coded for $1.00, which isn't very useful. + "amount_money" => array ( + "amount" => 100, + "currency" => "USD" + ), + + # Every payment you process with the SDK must have a unique idempotency key. + # If you're unsure whether a particular payment succeeded, you can reattempt + # it with the same idempotency key without worrying about double charging + # the buyer. + "idempotency_key" => uniqid() +); + +# The SDK throws an exception if a Connect endpoint responds with anything besides +# a 200-level HTTP code. This block catches any exceptions that occur from the request. +try { + $result = $transaction_api->charge($access_token, $location_id, $request_body); + echo "
";
+  print_r($result);
+  echo "
"; +} catch (\SquareConnect\ApiException $e) { + echo "Caught exception!
"; + print_r("Response body:
"); + echo "
"; var_dump($e->getResponseBody()); echo "
"; + echo "
Response headers:
"; + echo "
"; var_dump($e->getResponseHeaders()); echo "
"; +}