├── .gitignore
├── LICENSE
├── README.md
├── composer.json
├── gen.yml
├── homepage.jpg
├── init
├── lib
└── PayPalCheckoutSdk
│ ├── Core
│ ├── AccessToken.php
│ ├── AccessTokenRequest.php
│ ├── AuthorizationInjector.php
│ ├── FPTIInstrumentationInjector.php
│ ├── GzipInjector.php
│ ├── PayPalEnvironment.php
│ ├── PayPalHttpClient.php
│ ├── ProductionEnvironment.php
│ ├── RefreshTokenRequest.php
│ ├── SandboxEnvironment.php
│ ├── UserAgent.php
│ └── Version.php
│ ├── Orders
│ ├── OrdersAuthorizeRequest.php
│ ├── OrdersCaptureRequest.php
│ ├── OrdersCreateRequest.php
│ ├── OrdersGetRequest.php
│ ├── OrdersPatchRequest.php
│ └── OrdersValidateRequest.php
│ └── Payments
│ ├── AuthorizationsCaptureRequest.php
│ ├── AuthorizationsGetRequest.php
│ ├── AuthorizationsReauthorizeRequest.php
│ ├── AuthorizationsVoidRequest.php
│ ├── CapturesGetRequest.php
│ ├── CapturesRefundRequest.php
│ └── RefundsGetRequest.php
├── phpunit.xml
├── samples
├── AuthorizeIntentExamples
│ ├── AuthorizeOrder.php
│ ├── CaptureOrder.php
│ ├── CreateOrder.php
│ └── RunAll.php
├── CaptureIntentExamples
│ ├── CaptureOrder.php
│ ├── CreateOrder.php
│ └── RunAll.php
├── ErrorSample.php
├── GetOrder.php
├── PatchOrder.php
├── PayPalClient.php
└── RefundOrder.php
└── tests
├── Orders
├── OrdersAuthorizeTest.php
├── OrdersCaptureTest.php
├── OrdersCreateTest.php
├── OrdersGetTest.php
└── OrdersPatchTest.php
└── TestHarness.php
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/
2 | vendor/
3 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright 2016 PayPal
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Deprecation Notice:
2 | This SDK is deprecated; you can continue to use it, but no new features or support requests will be accepted. An integration with [the new Server SDK](https://github.com/paypal/PayPal-Server-SDKs) is recommended. Review the [docs](https://developer.paypal.com/serversdk/http/getting-started/how-to-get-started/) for details.
3 |
4 | ## REST API SDK for PHP V2
5 |
6 | 
7 |
8 | ### To consolidate support across various channels, we have currently turned off the feature of GitHub issues. Please visit https://www.paypal.com/support to submit your request or ask questions within our community forum.
9 |
10 | __Welcome to PayPal PHP SDK__. This repository contains PayPal's PHP SDK and samples for [v2/checkout/orders](https://developer.paypal.com/docs/api/orders/v2/) and [v2/payments](https://developer.paypal.com/docs/api/payments/v2/) APIs.
11 |
12 | This is a part of the next major PayPal SDK. It includes a simplified interface to only provide simple model objects and blueprints for HTTP calls. This repo currently contains functionality for PayPal Checkout APIs which includes [Orders V2](https://developer.paypal.com/docs/api/orders/v2/) and [Payments V2](https://developer.paypal.com/docs/api/payments/v2/).
13 |
14 | Please refer to the [PayPal Checkout Integration Guide](https://developer.paypal.com/docs/checkout/) for more information.
15 | ## Latest Updates
16 | Beginning January 2020, PayPal will require an update on the Personal Home Page (PHP) Checkout Software Developer Kit (SDK) to version 1.0.1. Merchants who have not updated their PHP Checkout SDK to version 1.0.1 will not be able to deserialize responses using outdated SDK integrations.
17 | All PHP Checkout SDK integrations are expected to be updated by March 1, 2020. Merchants are encouraged to prepare for the update as soon as possible to avoid possible service disruption.
18 | The Status Page has been updated with this information. The bulletin can be found [here](https://www.paypal-status.com/history/eventdetails/11015)
19 |
20 | ## Prerequisites
21 |
22 | PHP 5.6 and above
23 |
24 | An environment which supports TLS 1.2 (see the TLS-update site for more information)
25 |
26 | ## Usage
27 |
28 | ### Binaries
29 |
30 | It is not mandatory to fork this repository for using the PayPal SDK. You can refer [PayPal Checkout Server SDK](https://developer.paypal.com/docs/checkout/reference/server-integration) for configuring and working with SDK without forking this code.
31 |
32 | For contributing or referring the samples, You can fork/refer this repository.
33 |
34 | ### Setting up credentials
35 | Get client ID and client secret by going to https://developer.paypal.com/developer/applications and generating a REST API app. Get Client ID and Secret from there.
36 |
37 | ```php
38 | require __DIR__ . '/vendor/autoload.php';
39 | use PayPalCheckoutSdk\Core\PayPalHttpClient;
40 | use PayPalCheckoutSdk\Core\SandboxEnvironment;
41 | // Creating an environment
42 | $clientId = "<>";
43 | $clientSecret = "<>";
44 |
45 | $environment = new SandboxEnvironment($clientId, $clientSecret);
46 | $client = new PayPalHttpClient($environment);
47 | ```
48 |
49 | ## Examples
50 | ### Creating an Order
51 | #### Code:
52 | ```php
53 | // Construct a request object and set desired parameters
54 | // Here, OrdersCreateRequest() creates a POST request to /v2/checkout/orders
55 | use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
56 | $request = new OrdersCreateRequest();
57 | $request->prefer('return=representation');
58 | $request->body = [
59 | "intent" => "CAPTURE",
60 | "purchase_units" => [[
61 | "reference_id" => "test_ref_id1",
62 | "amount" => [
63 | "value" => "100.00",
64 | "currency_code" => "USD"
65 | ]
66 | ]],
67 | "application_context" => [
68 | "cancel_url" => "https://example.com/cancel",
69 | "return_url" => "https://example.com/return"
70 | ]
71 | ];
72 |
73 | try {
74 | // Call API with your client and get a response for your call
75 | $response = $client->execute($request);
76 |
77 | // If call returns body in response, you can get the deserialized version from the result attribute of the response
78 | print_r($response);
79 | }catch (HttpException $ex) {
80 | echo $ex->statusCode;
81 | print_r($ex->getMessage());
82 | }
83 | ```
84 | #### Example Output:
85 | ```
86 | Status Code: 201
87 | Id: 8GB67279RC051624C
88 | Intent: CAPTURE
89 | Gross_amount:
90 | Currency_code: USD
91 | Value: 100.00
92 | Purchase_units:
93 | 1:
94 | Amount:
95 | Currency_code: USD
96 | Value: 100.00
97 | Create_time: 2018-08-06T23:34:31Z
98 | Links:
99 | 1:
100 | Href: https://api.sandbox.paypal.com/v2/checkout/orders/8GB67279RC051624C
101 | Rel: self
102 | Method: GET
103 | 2:
104 | Href: https://www.sandbox.paypal.com/checkoutnow?token=8GB67279RC051624C
105 | Rel: approve
106 | Method: GET
107 | 3:
108 | Href: https://api.sandbox.paypal.com/v2/checkout/orders/8GB67279RC051624C/capture
109 | Rel: capture
110 | Method: POST
111 | Status: CREATED
112 | ```
113 |
114 | ## Capturing an Order
115 | Before capture, Order should be approved by the buyer using the approval URL returned in the create order response.
116 | ### Code to Execute:
117 | ```php
118 | use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
119 | // Here, OrdersCaptureRequest() creates a POST request to /v2/checkout/orders
120 | // $response->result->id gives the orderId of the order created above
121 | $request = new OrdersCaptureRequest("APPROVED-ORDER-ID");
122 | $request->prefer('return=representation');
123 | try {
124 | // Call API with your client and get a response for your call
125 | $response = $client->execute($request);
126 |
127 | // If call returns body in response, you can get the deserialized version from the result attribute of the response
128 | print_r($response);
129 | }catch (HttpException $ex) {
130 | echo $ex->statusCode;
131 | print_r($ex->getMessage());
132 | }
133 | ```
134 |
135 | #### Example Output:
136 | ```
137 | Status Code: 201
138 | Id: 8GB67279RC051624C
139 | Create_time: 2018-08-06T23:39:11Z
140 | Update_time: 2018-08-06T23:39:11Z
141 | Payer:
142 | Name:
143 | Given_name: test
144 | Surname: buyer
145 | Email_address: test-buyer@paypal.com
146 | Payer_id: KWADC7LXRRWCE
147 | Phone:
148 | Phone_number:
149 | National_number: 408-411-2134
150 | Address:
151 | Country_code: US
152 | Links:
153 | 1:
154 | Href: https://api.sandbox.paypal.com/v2/checkout/orders/3L848818A2897925Y
155 | Rel: self
156 | Method: GET
157 | Status: COMPLETED
158 | ```
159 |
160 | ## Running tests
161 |
162 | To run integration tests using your client id and secret, clone this repository and run the following command:
163 | ```sh
164 | $ composer install
165 | $ CLIENT_ID=YOUR_SANDBOX_CLIENT_ID CLIENT_SECRET=OUR_SANDBOX_CLIENT_SECRET composer integration
166 | ```
167 |
168 | ## Samples
169 |
170 | You can start off by trying out [creating and capturing an order](/samples/CaptureIntentExamples/RunAll.php)
171 |
172 | To try out different samples for both create and authorize intent check [this link](/samples)
173 |
174 | Note: Update the `PayPalClient.php` with your sandbox client credentials or pass your client credentials as environment variable while executing the samples.
175 |
176 |
177 | ## License
178 | Code released under [SDK LICENSE](LICENSE)
179 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "paypal/paypal-checkout-sdk",
3 | "description": "PayPal's PHP SDK for Checkout REST APIs",
4 | "keywords": ["paypal", "payments", "rest", "sdk", "orders", "checkout"],
5 | "type": "library",
6 | "license": "Apache-2.0",
7 | "homepage": "http://github.com/paypal/Checkout-PHP-SDK/",
8 | "require": {
9 | "paypal/paypalhttp": "1.0.1"
10 | },
11 | "authors": [
12 | {
13 | "name": "PayPal",
14 | "homepage": "https://github.com/paypal/Checkout-PHP-SDK/contributors"
15 | }
16 | ],
17 | "require-dev": {
18 | "phpunit/phpunit": "^5.7"
19 | },
20 | "autoload": {
21 | "psr-4": {
22 | "PayPalCheckoutSdk\\": "lib/PayPalCheckoutSdk",
23 | "Sample\\":"samples/"
24 | }
25 | },
26 | "autoload-dev": {
27 | "psr-4": {
28 | "Test\\":"tests/"
29 | }
30 | },
31 | "scripts": {
32 | "unit": "phpunit --testsuite unit",
33 | "integration": "phpunit --testsuite integration"
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/gen.yml:
--------------------------------------------------------------------------------
1 | ---
2 | projectName: PayPalCheckoutSdk
3 | language: php
4 | sourcePath: lib/PayPalCheckoutSdk
5 | testPath: tests
6 | apiVersioning: false
7 |
--------------------------------------------------------------------------------
/homepage.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/paypal/Checkout-PHP-SDK/d072b3e3579faf62c95da8b1ccc2fcddaa1f99cc/homepage.jpg
--------------------------------------------------------------------------------
/init:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/paypal/Checkout-PHP-SDK/d072b3e3579faf62c95da8b1ccc2fcddaa1f99cc/init
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Core/AccessToken.php:
--------------------------------------------------------------------------------
1 | token = $token;
16 | $this->tokenType = $tokenType;
17 | $this->expiresIn = $expiresIn;
18 | $this->createDate = time();
19 | }
20 |
21 | public function isExpired()
22 | {
23 | return time() >= $this->createDate + $this->expiresIn;
24 | }
25 | }
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Core/AccessTokenRequest.php:
--------------------------------------------------------------------------------
1 | headers["Authorization"] = "Basic " . $environment->authorizationString();
13 | $body = [
14 | "grant_type" => "client_credentials"
15 | ];
16 |
17 | if (!is_null($refreshToken))
18 | {
19 | $body["grant_type"] = "refresh_token";
20 | $body["refresh_token"] = $refreshToken;
21 | }
22 |
23 | $this->body = $body;
24 | $this->headers["Content-Type"] = "application/x-www-form-urlencoded";
25 | }
26 | }
27 |
28 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Core/AuthorizationInjector.php:
--------------------------------------------------------------------------------
1 | client = $client;
19 | $this->environment = $environment;
20 | $this->refreshToken = $refreshToken;
21 | }
22 |
23 | public function inject($request)
24 | {
25 | if (!$this->hasAuthHeader($request) && !$this->isAuthRequest($request))
26 | {
27 | if (is_null($this->accessToken) || $this->accessToken->isExpired())
28 | {
29 | $this->accessToken = $this->fetchAccessToken();
30 | }
31 | $request->headers['Authorization'] = 'Bearer ' . $this->accessToken->token;
32 | }
33 | }
34 |
35 | private function fetchAccessToken()
36 | {
37 | $accessTokenResponse = $this->client->execute(new AccessTokenRequest($this->environment, $this->refreshToken));
38 | $accessToken = $accessTokenResponse->result;
39 | return new AccessToken($accessToken->access_token, $accessToken->token_type, $accessToken->expires_in);
40 | }
41 |
42 | private function isAuthRequest($request)
43 | {
44 | return $request instanceof AccessTokenRequest || $request instanceof RefreshTokenRequest;
45 | }
46 |
47 | private function hasAuthHeader(HttpRequest $request)
48 | {
49 | return array_key_exists("Authorization", $request->headers);
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Core/FPTIInstrumentationInjector.php:
--------------------------------------------------------------------------------
1 | headers["sdk_name"] = "Checkout SDK";
12 | $request->headers["sdk_version"] = "1.0.2";
13 | $request->headers["sdk_tech_stack"] = "PHP " . PHP_VERSION;
14 | $request->headers["api_integration_type"] = "PAYPALSDK";
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Core/GzipInjector.php:
--------------------------------------------------------------------------------
1 | headers["Accept-Encoding"] = "gzip";
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Core/PayPalEnvironment.php:
--------------------------------------------------------------------------------
1 | clientId = $clientId;
15 | $this->clientSecret = $clientSecret;
16 | }
17 |
18 | public function authorizationString()
19 | {
20 | return base64_encode($this->clientId . ":" . $this->clientSecret);
21 | }
22 | }
23 |
24 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Core/PayPalHttpClient.php:
--------------------------------------------------------------------------------
1 | refreshToken = $refreshToken;
16 | $this->authInjector = new AuthorizationInjector($this, $environment, $refreshToken);
17 | $this->addInjector($this->authInjector);
18 | $this->addInjector(new GzipInjector());
19 | $this->addInjector(new FPTIInstrumentationInjector());
20 | }
21 |
22 | public function userAgent()
23 | {
24 | return UserAgent::getValue();
25 | }
26 | }
27 |
28 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Core/ProductionEnvironment.php:
--------------------------------------------------------------------------------
1 | headers["Authorization"] = "Basic " . $environment->authorizationString();
13 | $this->headers["Content-Type"] = "application/x-www-form-urlencoded";
14 | $this->body = [
15 | "grant_type" => "authorization_code",
16 | "code" => $authorizationCode
17 | ];
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Core/SandboxEnvironment.php:
--------------------------------------------------------------------------------
1 | path = str_replace("{order_id}", urlencode($orderId), $this->path);
21 | $this->headers["Content-Type"] = "application/json";
22 | }
23 |
24 |
25 | public function payPalClientMetadataId($payPalClientMetadataId)
26 | {
27 | $this->headers["PayPal-Client-Metadata-Id"] = $payPalClientMetadataId;
28 | }
29 | public function payPalRequestId($payPalRequestId)
30 | {
31 | $this->headers["PayPal-Request-Id"] = $payPalRequestId;
32 | }
33 | public function prefer($prefer)
34 | {
35 | $this->headers["Prefer"] = $prefer;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Orders/OrdersCaptureRequest.php:
--------------------------------------------------------------------------------
1 | path = str_replace("{order_id}", urlencode($orderId), $this->path);
21 | $this->headers["Content-Type"] = "application/json";
22 | }
23 |
24 |
25 | public function payPalClientMetadataId($payPalClientMetadataId)
26 | {
27 | $this->headers["PayPal-Client-Metadata-Id"] = $payPalClientMetadataId;
28 | }
29 | public function payPalRequestId($payPalRequestId)
30 | {
31 | $this->headers["PayPal-Request-Id"] = $payPalRequestId;
32 | }
33 | public function prefer($prefer)
34 | {
35 | $this->headers["Prefer"] = $prefer;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Orders/OrdersCreateRequest.php:
--------------------------------------------------------------------------------
1 | headers["Content-Type"] = "application/json";
20 | }
21 |
22 |
23 | public function payPalPartnerAttributionId($payPalPartnerAttributionId)
24 | {
25 | $this->headers["PayPal-Partner-Attribution-Id"] = $payPalPartnerAttributionId;
26 | }
27 | public function prefer($prefer)
28 | {
29 | $this->headers["Prefer"] = $prefer;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Orders/OrdersGetRequest.php:
--------------------------------------------------------------------------------
1 | path = str_replace("{order_id}", urlencode($orderId), $this->path);
21 | $this->headers["Content-Type"] = "application/json";
22 | }
23 |
24 |
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Orders/OrdersPatchRequest.php:
--------------------------------------------------------------------------------
1 | path = str_replace("{order_id}", urlencode($orderId), $this->path);
21 | $this->headers["Content-Type"] = "application/json";
22 | }
23 |
24 |
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Orders/OrdersValidateRequest.php:
--------------------------------------------------------------------------------
1 | path = str_replace("{order_id}", urlencode($orderId), $this->path);
21 | $this->headers["Content-Type"] = "application/json";
22 | }
23 |
24 |
25 | public function payPalClientMetadataId($payPalClientMetadataId)
26 | {
27 | $this->headers["PayPal-Client-Metadata-Id"] = $payPalClientMetadataId;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Payments/AuthorizationsCaptureRequest.php:
--------------------------------------------------------------------------------
1 | path = str_replace("{authorization_id}", urlencode($authorizationId), $this->path);
21 | $this->headers["Content-Type"] = "application/json";
22 | }
23 |
24 |
25 | public function payPalRequestId($payPalRequestId)
26 | {
27 | $this->headers["PayPal-Request-Id"] = $payPalRequestId;
28 | }
29 | public function prefer($prefer)
30 | {
31 | $this->headers["Prefer"] = $prefer;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Payments/AuthorizationsGetRequest.php:
--------------------------------------------------------------------------------
1 | path = str_replace("{authorization_id}", urlencode($authorizationId), $this->path);
21 | $this->headers["Content-Type"] = "application/json";
22 | }
23 |
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Payments/AuthorizationsReauthorizeRequest.php:
--------------------------------------------------------------------------------
1 | path = str_replace("{authorization_id}", urlencode($authorizationId), $this->path);
21 | $this->headers["Content-Type"] = "application/json";
22 | }
23 |
24 |
25 | public function payPalRequestId($payPalRequestId)
26 | {
27 | $this->headers["PayPal-Request-Id"] = $payPalRequestId;
28 | }
29 | public function prefer($prefer)
30 | {
31 | $this->headers["Prefer"] = $prefer;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Payments/AuthorizationsVoidRequest.php:
--------------------------------------------------------------------------------
1 | path = str_replace("{authorization_id}", urlencode($authorizationId), $this->path);
21 | $this->headers["Content-Type"] = "application/json";
22 | }
23 |
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Payments/CapturesGetRequest.php:
--------------------------------------------------------------------------------
1 | path = str_replace("{capture_id}", urlencode($captureId), $this->path);
21 | $this->headers["Content-Type"] = "application/json";
22 | }
23 |
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Payments/CapturesRefundRequest.php:
--------------------------------------------------------------------------------
1 | path = str_replace("{capture_id}", urlencode($captureId), $this->path);
21 | $this->headers["Content-Type"] = "application/json";
22 | }
23 |
24 |
25 | public function payPalRequestId($payPalRequestId)
26 | {
27 | $this->headers["PayPal-Request-Id"] = $payPalRequestId;
28 | }
29 | public function prefer($prefer)
30 | {
31 | $this->headers["Prefer"] = $prefer;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/lib/PayPalCheckoutSdk/Payments/RefundsGetRequest.php:
--------------------------------------------------------------------------------
1 | path = str_replace("{refund_id}", urlencode($refundId), $this->path);
21 | $this->headers["Content-Type"] = "application/json";
22 | }
23 |
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/phpunit.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | ./tests/
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/samples/AuthorizeIntentExamples/AuthorizeOrder.php:
--------------------------------------------------------------------------------
1 | body = self::buildRequestBody();
29 |
30 | $client = PayPalClient::client();
31 | $response = $client->execute($request);
32 | if ($debug)
33 | {
34 | print "Status Code: {$response->statusCode}\n";
35 | print "Status: {$response->result->status}\n";
36 | print "Order ID: {$response->result->id}\n";
37 | print "Authorization ID: {$response->result->purchase_units[0]->payments->authorizations[0]->id}\n";
38 | print "Links:\n";
39 | foreach($response->result->links as $link)
40 | {
41 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
42 | }
43 | print "Authorization Links:\n";
44 | foreach($response->result->purchase_units[0]->payments->authorizations[0]->links as $link)
45 | {
46 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
47 | }
48 | // To toggle printing the whole response body comment/uncomment below line
49 | echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
50 | }
51 | return $response;
52 | }
53 | }
54 |
55 | /**
56 | * This is an driver function which invokes authorize order.
57 | */
58 | if (!count(debug_backtrace()))
59 | {
60 | AuthorizeOrder::authorizeOrder('1U242387CB956380X', true);
61 | }
--------------------------------------------------------------------------------
/samples/AuthorizeIntentExamples/CaptureOrder.php:
--------------------------------------------------------------------------------
1 | body = self::buildRequestBody();
29 | $client = PayPalClient::client();
30 | $response = $client->execute($request);
31 |
32 | if ($debug)
33 | {
34 | print "Status Code: {$response->statusCode}\n";
35 | print "Status: {$response->result->status}\n";
36 | print "Capture ID: {$response->result->id}\n";
37 | print "Links:\n";
38 | foreach($response->result->links as $link)
39 | {
40 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
41 | }
42 | // To toggle printing the whole response body comment/uncomment below line
43 | echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
44 | }
45 | return $response;
46 | }
47 | }
48 |
49 | /**
50 | * Driver function for invoking the capture flow.
51 | */
52 | if (!count(debug_backtrace()))
53 | {
54 | CaptureOrder::captureOrder('18A38324BV5456924', true);
55 | }
--------------------------------------------------------------------------------
/samples/AuthorizeIntentExamples/CreateOrder.php:
--------------------------------------------------------------------------------
1 | 'AUTHORIZE',
21 | 'application_context' =>
22 | array(
23 | 'return_url' => 'https://example.com/return',
24 | 'cancel_url' => 'https://example.com/cancel',
25 | 'brand_name' => 'EXAMPLE INC',
26 | 'locale' => 'en-US',
27 | 'landing_page' => 'BILLING',
28 | 'shipping_preference' => 'SET_PROVIDED_ADDRESS',
29 | 'user_action' => 'PAY_NOW',
30 | ),
31 | 'purchase_units' =>
32 | array(
33 | 0 =>
34 | array(
35 | 'reference_id' => 'PUHF',
36 | 'description' => 'Sporting Goods',
37 | 'custom_id' => 'CUST-HighFashions',
38 | 'soft_descriptor' => 'HighFashions',
39 | 'amount' =>
40 | array(
41 | 'currency_code' => 'USD',
42 | 'value' => '220.00',
43 | 'breakdown' =>
44 | array(
45 | 'item_total' =>
46 | array(
47 | 'currency_code' => 'USD',
48 | 'value' => '180.00',
49 | ),
50 | 'shipping' =>
51 | array(
52 | 'currency_code' => 'USD',
53 | 'value' => '20.00',
54 | ),
55 | 'handling' =>
56 | array(
57 | 'currency_code' => 'USD',
58 | 'value' => '10.00',
59 | ),
60 | 'tax_total' =>
61 | array(
62 | 'currency_code' => 'USD',
63 | 'value' => '20.00',
64 | ),
65 | 'shipping_discount' =>
66 | array(
67 | 'currency_code' => 'USD',
68 | 'value' => '10.00',
69 | ),
70 | ),
71 | ),
72 | 'items' =>
73 | array(
74 | 0 =>
75 | array(
76 | 'name' => 'T-Shirt',
77 | 'description' => 'Green XL',
78 | 'sku' => 'sku01',
79 | 'unit_amount' =>
80 | array(
81 | 'currency_code' => 'USD',
82 | 'value' => '90.00',
83 | ),
84 | 'tax' =>
85 | array(
86 | 'currency_code' => 'USD',
87 | 'value' => '10.00',
88 | ),
89 | 'quantity' => '1',
90 | 'category' => 'PHYSICAL_GOODS',
91 | ),
92 | 1 =>
93 | array(
94 | 'name' => 'Shoes',
95 | 'description' => 'Running, Size 10.5',
96 | 'sku' => 'sku02',
97 | 'unit_amount' =>
98 | array(
99 | 'currency_code' => 'USD',
100 | 'value' => '45.00',
101 | ),
102 | 'tax' =>
103 | array(
104 | 'currency_code' => 'USD',
105 | 'value' => '5.00',
106 | ),
107 | 'quantity' => '2',
108 | 'category' => 'PHYSICAL_GOODS',
109 | ),
110 | ),
111 | 'shipping' =>
112 | array(
113 | 'method' => 'United States Postal Service',
114 | 'name' =>
115 | array(
116 | 'full_name' => 'John Doe',
117 | ),
118 | 'address' =>
119 | array(
120 | 'address_line_1' => '123 Townsend St',
121 | 'address_line_2' => 'Floor 6',
122 | 'admin_area_2' => 'San Francisco',
123 | 'admin_area_1' => 'CA',
124 | 'postal_code' => '94107',
125 | 'country_code' => 'US',
126 | ),
127 | ),
128 | ),
129 | ),
130 | );
131 | }
132 |
133 | /**
134 | * Setting up the JSON request body for creating the Order with minimum request body. The Intent in the
135 | * request body should be set as "AUTHORIZE" for authorize intent flow.
136 | *
137 | */
138 | private static function buildMinimumRequestBody()
139 | {
140 | return array(
141 | 'intent' => 'AUTHORIZE',
142 | 'application_context' =>
143 | array(
144 | 'return_url' => 'https://example.com/return',
145 | 'cancel_url' => 'https://example.com/cancel'
146 | ),
147 | 'purchase_units' =>
148 | array(
149 | 0 =>
150 | array(
151 | 'amount' =>
152 | array(
153 | 'currency_code' => 'USD',
154 | 'value' => '220.00'
155 | )
156 | )
157 | )
158 | );
159 | }
160 |
161 | /**
162 | * This is the sample function which can be used to create an order. It uses the
163 | * JSON body returned by buildRequestBody() to create an new Order.
164 | */
165 | public static function createOrder($debug=false)
166 | {
167 | $request = new OrdersCreateRequest();
168 | $request->headers["prefer"] = "return=representation";
169 | $request->body = CreateOrder::buildRequestBody();
170 |
171 | $client = PayPalClient::client();
172 | $response = $client->execute($request);
173 | if ($debug)
174 | {
175 | print "Status Code: {$response->statusCode}\n";
176 | print "Status: {$response->result->status}\n";
177 | print "Order ID: {$response->result->id}\n";
178 | print "Intent: {$response->result->intent}\n";
179 | print "Links:\n";
180 | foreach($response->result->links as $link)
181 | {
182 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
183 | }
184 |
185 | print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";
186 |
187 | // To toggle printing the whole response body comment/uncomment below line
188 | echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
189 | }
190 |
191 |
192 | return $response;
193 | }
194 |
195 | /**
196 | * This is the sample function which can be used to create an order. It uses the
197 | * JSON body returned by buildMinimumRequestBody() to create an new Order.
198 | */
199 | public static function createOrderWithMinimumBody($debug=false)
200 | {
201 | $request = new OrdersCreateRequest();
202 | $request->headers["prefer"] = "return=representation";
203 | $request->body = CreateOrder::buildMinimumRequestBody();
204 |
205 | $client = PayPalClient::client();
206 | $response = $client->execute($request);
207 | if ($debug)
208 | {
209 | print "Order With Minimum Body\n";
210 | print "Status Code: {$response->statusCode}\n";
211 | print "Status: {$response->result->status}\n";
212 | print "Order ID: {$response->result->id}\n";
213 | print "Intent: {$response->result->intent}\n";
214 | print "Links:\n";
215 | foreach($response->result->links as $link)
216 | {
217 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
218 | }
219 |
220 | print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";
221 |
222 | // To toggle printing the whole response body comment/uncomment below line
223 | echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
224 | }
225 |
226 |
227 | return $response;
228 | }
229 | }
230 |
231 |
232 |
233 | if (!count(debug_backtrace()))
234 | {
235 | CreateOrder::createOrder(true);
236 | CreateOrder::createOrderWithMinimumBody(true);
237 | }
--------------------------------------------------------------------------------
/samples/AuthorizeIntentExamples/RunAll.php:
--------------------------------------------------------------------------------
1 | statusCode == 201)
15 | {
16 | $orderId = $order->result->id;
17 | print "Links:\n";
18 | for ($i = 0; $i < count($order->result->links); ++$i)
19 | {
20 | $link = $order->result->links[$i];
21 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
22 | }
23 | print "Created Successfully\n";
24 | print "Copy approve link and paste it in browser. Login with buyer account and follow the instructions.\nOnce approved hit enter...\n";
25 | }
26 | else {
27 | exit(1);
28 | }
29 |
30 | $handle = fopen ("php://stdin","r");
31 | $line = fgets($handle);
32 | fclose($handle);
33 |
34 | print "Authorizing Order...\n";
35 | $response = AuthorizeOrder::authorizeOrder($orderId);
36 | $authId = "";
37 |
38 | if ($response->statusCode == 201)
39 | {
40 | print "Authorized Successfully\n";
41 | $authId = $response->result->purchase_units[0]->payments->authorizations[0]->id;
42 | }
43 | else {
44 | exit(1);
45 | }
46 |
47 | print "\nCapturing Order...\n";
48 | $response = CaptureOrder::captureOrder($authId);
49 | if ($response->statusCode == 201)
50 | {
51 | print "Captured Successfully\n";
52 | print "Status Code: {$response->statusCode}\n";
53 | print "Status: {$response->result->status}\n";
54 | $captureId = $response->result->id;
55 | print "Capture ID: {$captureId}\n";
56 | print "Links:\n";
57 | for ($i = 0; $i < count($response->result->links); ++$i){
58 | $link = $response->result->links[$i];
59 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
60 | }
61 | }
62 | else {
63 | exit(1);
64 | }
65 |
66 | print "\nRefunding Order...\n";
67 | $response = RefundOrder::refundOrder($captureId);
68 | if ($response->statusCode == 201)
69 | {
70 | print "Refunded Successfully\n";
71 | print "Status Code: {$response->statusCode}\n";
72 | print "Status: {$response->result->status}\n";
73 | print "Refund ID: {$response->result->id}\n";
74 | print "Links:\n";
75 | for ($i = 0; $i < count($response->result->links); ++$i){
76 | $link = $response->result->links[$i];
77 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
78 | }
79 | }
80 | else {
81 | exit(1);
82 | }
83 |
--------------------------------------------------------------------------------
/samples/CaptureIntentExamples/CaptureOrder.php:
--------------------------------------------------------------------------------
1 | execute($request);
27 | if ($debug)
28 | {
29 | print "Status Code: {$response->statusCode}\n";
30 | print "Status: {$response->result->status}\n";
31 | print "Order ID: {$response->result->id}\n";
32 | print "Links:\n";
33 | foreach($response->result->links as $link)
34 | {
35 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
36 | }
37 | print "Capture Ids:\n";
38 | foreach($response->result->purchase_units as $purchase_unit)
39 | {
40 | foreach($purchase_unit->payments->captures as $capture)
41 | {
42 | print "\t{$capture->id}";
43 | }
44 | }
45 | // To toggle printing the whole response body comment/uncomment below line
46 | echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
47 | }
48 |
49 | return $response;
50 | }
51 | }
52 |
53 | /**
54 | * This is the driver function which invokes the captureOrder function with
55 | * Approved Order Id to capture the order payment.
56 | */
57 | if (!count(debug_backtrace()))
58 | {
59 | CaptureOrder::captureOrder('0F105083N67049335', true);
60 | }
--------------------------------------------------------------------------------
/samples/CaptureIntentExamples/CreateOrder.php:
--------------------------------------------------------------------------------
1 | 'CAPTURE',
23 | 'application_context' =>
24 | array(
25 | 'return_url' => 'https://example.com/return',
26 | 'cancel_url' => 'https://example.com/cancel',
27 | 'brand_name' => 'EXAMPLE INC',
28 | 'locale' => 'en-US',
29 | 'landing_page' => 'BILLING',
30 | 'shipping_preference' => 'SET_PROVIDED_ADDRESS',
31 | 'user_action' => 'PAY_NOW',
32 | ),
33 | 'purchase_units' =>
34 | array(
35 | 0 =>
36 | array(
37 | 'reference_id' => 'PUHF',
38 | 'description' => 'Sporting Goods',
39 | 'custom_id' => 'CUST-HighFashions',
40 | 'soft_descriptor' => 'HighFashions',
41 | 'amount' =>
42 | array(
43 | 'currency_code' => 'USD',
44 | 'value' => '220.00',
45 | 'breakdown' =>
46 | array(
47 | 'item_total' =>
48 | array(
49 | 'currency_code' => 'USD',
50 | 'value' => '180.00',
51 | ),
52 | 'shipping' =>
53 | array(
54 | 'currency_code' => 'USD',
55 | 'value' => '20.00',
56 | ),
57 | 'handling' =>
58 | array(
59 | 'currency_code' => 'USD',
60 | 'value' => '10.00',
61 | ),
62 | 'tax_total' =>
63 | array(
64 | 'currency_code' => 'USD',
65 | 'value' => '20.00',
66 | ),
67 | 'shipping_discount' =>
68 | array(
69 | 'currency_code' => 'USD',
70 | 'value' => '10.00',
71 | ),
72 | ),
73 | ),
74 | 'items' =>
75 | array(
76 | 0 =>
77 | array(
78 | 'name' => 'T-Shirt',
79 | 'description' => 'Green XL',
80 | 'sku' => 'sku01',
81 | 'unit_amount' =>
82 | array(
83 | 'currency_code' => 'USD',
84 | 'value' => '90.00',
85 | ),
86 | 'tax' =>
87 | array(
88 | 'currency_code' => 'USD',
89 | 'value' => '10.00',
90 | ),
91 | 'quantity' => '1',
92 | 'category' => 'PHYSICAL_GOODS',
93 | ),
94 | 1 =>
95 | array(
96 | 'name' => 'Shoes',
97 | 'description' => 'Running, Size 10.5',
98 | 'sku' => 'sku02',
99 | 'unit_amount' =>
100 | array(
101 | 'currency_code' => 'USD',
102 | 'value' => '45.00',
103 | ),
104 | 'tax' =>
105 | array(
106 | 'currency_code' => 'USD',
107 | 'value' => '5.00',
108 | ),
109 | 'quantity' => '2',
110 | 'category' => 'PHYSICAL_GOODS',
111 | ),
112 | ),
113 | 'shipping' =>
114 | array(
115 | 'method' => 'United States Postal Service',
116 | 'name' =>
117 | array(
118 | 'full_name' => 'John Doe',
119 | ),
120 | 'address' =>
121 | array(
122 | 'address_line_1' => '123 Townsend St',
123 | 'address_line_2' => 'Floor 6',
124 | 'admin_area_2' => 'San Francisco',
125 | 'admin_area_1' => 'CA',
126 | 'postal_code' => '94107',
127 | 'country_code' => 'US',
128 | ),
129 | ),
130 | ),
131 | ),
132 | );
133 | }
134 |
135 | /**
136 | * This is the sample function which can be sued to create an order. It uses the
137 | * JSON body returned by buildRequestBody() to create an new Order.
138 | */
139 | public static function createOrder($debug=false)
140 | {
141 | $request = new OrdersCreateRequest();
142 | $request->headers["prefer"] = "return=representation";
143 | $request->body = self::buildRequestBody();
144 |
145 | $client = PayPalClient::client();
146 | $response = $client->execute($request);
147 | if ($debug)
148 | {
149 | print "Status Code: {$response->statusCode}\n";
150 | print "Status: {$response->result->status}\n";
151 | print "Order ID: {$response->result->id}\n";
152 | print "Intent: {$response->result->intent}\n";
153 | print "Links:\n";
154 | foreach($response->result->links as $link)
155 | {
156 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
157 | }
158 | // To toggle printing the whole response body comment/uncomment below line
159 | echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
160 | }
161 |
162 |
163 | return $response;
164 | }
165 | }
166 |
167 |
168 | /**
169 | * This is the driver function which invokes the createOrder function to create
170 | * an sample order.
171 | */
172 | if (!count(debug_backtrace()))
173 | {
174 | CreateOrder::createOrder(true);
175 | }
176 |
177 |
178 |
179 |
--------------------------------------------------------------------------------
/samples/CaptureIntentExamples/RunAll.php:
--------------------------------------------------------------------------------
1 | statusCode == 201)
14 | {
15 | $orderId = $order->result->id;
16 | print "Links:\n";
17 | for ($i = 0; $i < count($order->result->links); ++$i)
18 | {
19 | $link = $order->result->links[$i];
20 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
21 | }
22 | print "Created Successfully\n";
23 | print "Copy approve link and paste it in browser. Login with buyer account and follow the instructions.\nOnce approved hit enter...\n";
24 | }
25 | else {
26 | exit(1);
27 | }
28 |
29 | $handle = fopen ("php://stdin","r");
30 | $line = fgets($handle);
31 | fclose($handle);
32 |
33 | print "Capturing Order...\n";
34 | $response = CaptureOrder::captureOrder($orderId);
35 | if ($response->statusCode == 201)
36 | {
37 | print "Captured Successfully\n";
38 | print "Status Code: {$response->statusCode}\n";
39 | print "Status: {$response->result->status}\n";
40 | print "Order ID: {$response->result->id}\n";
41 | print "Links:\n";
42 | for ($i = 0; $i < count($response->result->links); ++$i){
43 | $link = $response->result->links[$i];
44 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
45 | }
46 | foreach($response->result->purchase_units as $purchase_unit)
47 | {
48 | foreach($purchase_unit->payments->captures as $capture)
49 | {
50 | $captureId = $capture->id;
51 | }
52 | }
53 | }
54 | else {
55 | exit(1);
56 | }
57 |
58 | print "\nRefunding Order...\n";
59 | $response = RefundOrder::refundOrder($captureId);
60 | if ($response->statusCode == 201)
61 | {
62 | print "Refunded Successfully\n";
63 | print "Status Code: {$response->statusCode}\n";
64 | print "Status: {$response->result->status}\n";
65 | print "Refund ID: {$response->result->id}\n";
66 | print "Links:\n";
67 | for ($i = 0; $i < count($response->result->links); ++$i){
68 | $link = $response->result->links[$i];
69 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
70 | }
71 | }
72 | else {
73 | exit(1);
74 | }
75 |
--------------------------------------------------------------------------------
/samples/ErrorSample.php:
--------------------------------------------------------------------------------
1 | $val)
15 | {
16 | $pretty .= $pre . ucfirst($key) .": ";
17 | if (strcmp(gettype($val), "array") == 0){
18 | $pretty .= "\n";
19 | $sno = 1;
20 | foreach ($val as $value)
21 | {
22 | $pretty .= $pre . "\t" . $sno++ . ":\n";
23 | $pretty .= self::prettyPrint($value, $pre . "\t\t");
24 | }
25 | }
26 | else {
27 | $pretty .= $val . "\n";
28 | }
29 | }
30 | return $pretty;
31 | }
32 |
33 | /**
34 | * Body has no required parameters (intent, purchase_units)
35 | */
36 | public static function createError1()
37 | {
38 | $request = new OrdersCreateRequest();
39 | $request->body = "{}";
40 | print "Request Body: {}\n\n";
41 |
42 | print "Response:\n";
43 | try{
44 | $client = PayPalClient::client();
45 | $response = $client->execute($request);
46 | }
47 | catch(HttpException $exception){
48 | $message = json_decode($exception->getMessage(), true);
49 | print "Status Code: {$exception->statusCode}\n";
50 | print(self::prettyPrint($message));
51 | }
52 | }
53 |
54 | /**
55 | * Body has invalid parameter value for intent
56 | */
57 | public static function createError2()
58 | {
59 | $request = new OrdersCreateRequest();
60 | $request->body = array (
61 | 'intent' => 'INVALID',
62 | 'purchase_units' =>
63 | array (
64 | 0 =>
65 | array (
66 | 'amount' =>
67 | array (
68 | 'currency_code' => 'USD',
69 | 'value' => '100.00',
70 | ),
71 | ),
72 | ),
73 | );
74 | print "Request Body:\n" . json_encode($request->body, JSON_PRETTY_PRINT) . "\n\n";
75 |
76 | try{
77 | $client = PayPalClient::client();
78 | $response = $client->execute($request);
79 | }
80 | catch(HttpException $exception){
81 | print "Response:\n";
82 | $message = json_decode($exception->getMessage(), true);
83 | print "Status Code: {$exception->statusCode}\n";
84 | print(self::prettyPrint($message));
85 | }
86 |
87 | }
88 | }
89 |
90 | print "Calling createError1 (Body has no required parameters (intent, purchase_units))\n";
91 | ErrorSample::createError1();
92 |
93 | print "\n\nCalling createError2 (Body has invalid parameter value for intent)\n";
94 | ErrorSample::createError2();
95 |
--------------------------------------------------------------------------------
/samples/GetOrder.php:
--------------------------------------------------------------------------------
1 | execute(new OrdersGetRequest($orderId));
22 | /**
23 | * Enable below line to print complete response as JSON.
24 | */
25 | //print json_encode($response->result);
26 | print "Status Code: {$response->statusCode}\n";
27 | print "Status: {$response->result->status}\n";
28 | print "Order ID: {$response->result->id}\n";
29 | print "Intent: {$response->result->intent}\n";
30 | print "Links:\n";
31 | foreach($response->result->links as $link)
32 | {
33 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
34 | }
35 |
36 | print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";
37 |
38 | // To toggle printing the whole response body comment/uncomment below line
39 | echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
40 | }
41 | }
42 |
43 | /**
44 | * This is the driver function which invokes the getOrder function to retrieve
45 | * an sample order.
46 | *
47 | * To get the correct Order id, we are using the createOrder to create new order
48 | * and then we are using the newly created order id.
49 | */
50 | if (!count(debug_backtrace()))
51 | {
52 | $createdOrder = CreateOrder::createOrder()->result;
53 | GetOrder::getOrder($createdOrder ->id);
54 | }
--------------------------------------------------------------------------------
/samples/PatchOrder.php:
--------------------------------------------------------------------------------
1 |
17 | array (
18 | 'op' => 'replace',
19 | 'path' => '/intent',
20 | 'value' => 'CAPTURE',
21 | ),
22 | 1 =>
23 | array (
24 | 'op' => 'replace',
25 | 'path' => '/purchase_units/@reference_id==\'PUHF\'/amount',
26 | 'value' =>
27 | array (
28 | 'currency_code' => 'USD',
29 | 'value' => '200.00',
30 | 'breakdown' =>
31 | array (
32 | 'item_total' =>
33 | array (
34 | 'currency_code' => 'USD',
35 | 'value' => '180.00',
36 | ),
37 | 'tax_total' =>
38 | array (
39 | 'currency_code' => 'USD',
40 | 'value' => '20.00',
41 | ),
42 | ),
43 | ),
44 | ),
45 | );
46 | }
47 |
48 | public static function patchOrder($orderId)
49 | {
50 |
51 | $client = PayPalClient::client();
52 |
53 | $request = new OrdersPatchRequest($orderId);
54 | $request->body = PatchOrder::buildRequestBody();
55 | $client->execute($request);
56 |
57 | $response = $client->execute(new OrdersGetRequest($orderId));
58 |
59 | print "Status Code: {$response->statusCode}\n";
60 | print "Status: {$response->result->status}\n";
61 | print "Order ID: {$response->result->id}\n";
62 | print "Intent: {$response->result->intent}\n";
63 | print "Links:\n";
64 | foreach($response->result->links as $link)
65 | {
66 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
67 | }
68 |
69 | print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";
70 |
71 | // To toggle printing the whole response body comment/uncomment below line
72 | echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
73 | }
74 | }
75 |
76 | if (!count(debug_backtrace()))
77 | {
78 | print "Before PATCH:\n";
79 | $createdOrder = CreateOrder::createOrder(true)->result;
80 | print "\nAfter PATCH (Changed Intent and Amount):\n";
81 | PatchOrder::patchOrder($createdOrder->id);
82 | }
--------------------------------------------------------------------------------
/samples/PayPalClient.php:
--------------------------------------------------------------------------------
1 | >";
32 | $clientSecret = getenv("CLIENT_SECRET") ?: "<>";
33 | return new SandboxEnvironment($clientId, $clientSecret);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/samples/RefundOrder.php:
--------------------------------------------------------------------------------
1 |
19 | array(
20 | 'value' => '20.00',
21 | 'currency_code' => 'USD'
22 | )
23 | );
24 | }
25 |
26 | /**
27 | * This function can be used to preform refund on the capture.
28 | */
29 | public static function refundOrder($captureId, $debug=false)
30 | {
31 | $request = new CapturesRefundRequest($captureId);
32 | $request->body = self::buildRequestBody();
33 | $client = PayPalClient::client();
34 | $response = $client->execute($request);
35 |
36 | if ($debug)
37 | {
38 | print "Status Code: {$response->statusCode}\n";
39 | print "Status: {$response->result->status}\n";
40 | print "Order ID: {$response->result->id}\n";
41 | print "Links:\n";
42 | foreach($response->result->links as $link)
43 | {
44 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
45 | }
46 | // To toggle printing the whole response body comment/uncomment below line
47 | echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
48 | }
49 | return $response;
50 | }
51 | }
52 |
53 | /**
54 | * This is the driver function which invokes the refund capture function with
55 | * Capture Id to perform refund on capture.
56 | */
57 | if (!count(debug_backtrace()))
58 | {
59 | RefundOrder::refundOrder('8XL09935J2224701N', true);
60 | }
61 |
--------------------------------------------------------------------------------
/tests/Orders/OrdersAuthorizeTest.php:
--------------------------------------------------------------------------------
1 | markTestSkipped("Need an approved Order ID to execute this test.");
18 | $request = new OrdersAuthorizeRequest('ORDER-ID');
19 | $request->body = $this->buildRequestBody();
20 |
21 | $client = TestHarness::client();
22 | $response = $client->execute($request);
23 | $this->assertEquals(201, $response->statusCode);
24 | $this->assertNotNull($response->result);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/tests/Orders/OrdersCaptureTest.php:
--------------------------------------------------------------------------------
1 | markTestSkipped("Need an approved Order ID to execute this test.");
19 | $request = new OrdersCaptureRequest('ORDER-ID');
20 |
21 | $client = TestHarness::client();
22 | $response = $client->execute($request);
23 | $this->assertEquals(201, $response->statusCode);
24 | $this->assertNotNull($response->result);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/tests/Orders/OrdersCreateTest.php:
--------------------------------------------------------------------------------
1 | "CAPTURE",
19 | "purchase_units" => [[
20 | "reference_id" => "test_ref_id1",
21 | "amount" => [
22 | "value" => "100.00",
23 | "currency_code" => "USD"
24 | ]
25 | ]],
26 | "redirect_urls" => [
27 | "cancel_url" => "https://example.com/cancel",
28 | "return_url" => "https://example.com/return"
29 | ]
30 | ];
31 | }
32 |
33 | public static function create($client) {
34 | $request = new OrdersCreateRequest();
35 | $request->prefer("return=representation");
36 | $request->body = self::buildRequestBody();
37 | return $client->execute($request);
38 | }
39 |
40 | public function testOrdersCreateRequest()
41 | {
42 | $client = TestHarness::client();
43 | $response = self::create($client);
44 | $this->assertEquals(201, $response->statusCode);
45 | $this->assertNotNull($response->result);
46 |
47 | $createdOrder = $response->result;
48 | $this->assertNotNull($createdOrder->id);
49 | $this->assertNotNull($createdOrder->purchase_units);
50 | $this->assertEquals(1, count($createdOrder->purchase_units));
51 | $firstPurchaseUnit = $createdOrder->purchase_units[0];
52 | $this->assertEquals("test_ref_id1", $firstPurchaseUnit->reference_id);
53 | $this->assertEquals("USD", $firstPurchaseUnit->amount->currency_code);
54 | $this->assertEquals("100.00", $firstPurchaseUnit->amount->value);
55 |
56 | $this->assertNotNull($createdOrder->create_time);
57 | $this->assertNotNull($createdOrder->links);
58 | $foundApproveUrl = false;
59 | foreach ($createdOrder->links as $link) {
60 | if ("approve" === $link->rel) {
61 | $foundApproveUrl = true;
62 | $this->assertNotNull($link->href);
63 | $this->assertEquals("GET", $link->method);
64 | }
65 | }
66 | $this->assertTrue($foundApproveUrl);
67 | $this->assertEquals("CREATED", $createdOrder->status);
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/tests/Orders/OrdersGetTest.php:
--------------------------------------------------------------------------------
1 | result->id);
22 | $response = $client->execute($request);
23 | $this->assertEquals(200, $response->statusCode);
24 | $this->assertNotNull($response->result);
25 |
26 | $createdOrder = $response->result;
27 | $this->assertNotNull($createdOrder->id);
28 | $this->assertNotNull($createdOrder->purchase_units);
29 | $this->assertEquals(1, count($createdOrder->purchase_units));
30 | $firstPurchaseUnit = $createdOrder->purchase_units[0];
31 | $this->assertEquals("test_ref_id1", $firstPurchaseUnit->reference_id);
32 | $this->assertEquals("USD", $firstPurchaseUnit->amount->currency_code);
33 | $this->assertEquals("100.00", $firstPurchaseUnit->amount->value);
34 |
35 | $this->assertNotNull($createdOrder->create_time);
36 | $this->assertNotNull($createdOrder->links);
37 | $foundApproveUrl = false;
38 | foreach ($createdOrder->links as $link) {
39 | if ("approve" === $link->rel) {
40 | $foundApproveUrl = true;
41 | $this->assertNotNull($link->href);
42 | $this->assertEquals("GET", $link->method);
43 | }
44 | }
45 | $this->assertTrue($foundApproveUrl);
46 | $this->assertEquals("CREATED", $createdOrder->status);
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/tests/Orders/OrdersPatchTest.php:
--------------------------------------------------------------------------------
1 | "add",
21 | "path" => "/purchase_units/@reference_id=='test_ref_id1'/description",
22 | "value" => "added_description"
23 | ],
24 | [
25 | "op" => "replace",
26 | "path" => "/purchase_units/@reference_id=='test_ref_id1'/amount",
27 | "value" => [
28 | "currency_code" => "USD",
29 | "value" => "200.00"
30 | ]
31 | ]
32 | ];
33 | }
34 |
35 | public function testOrdersPatchRequest()
36 | {
37 | $client = TestHarness::client();
38 | $createdOrder = OrdersCreateTest::create($client);
39 |
40 | $request = new OrdersPatchRequest($createdOrder->result->id);
41 | $request->body = $this->buildRequestBody();
42 | $response = $client->execute($request);
43 | $this->assertEquals(204, $response->statusCode);
44 |
45 | $request = new OrdersGetRequest($createdOrder->result->id);
46 | $response = $client->execute($request);
47 | $this->assertEquals(200, $response->statusCode);
48 | $this->assertNotNull($response->result);
49 |
50 | $createdOrder = $response->result;
51 | $this->assertNotNull($createdOrder->id);
52 | $this->assertNotNull($createdOrder->purchase_units);
53 | $this->assertEquals(1, count($createdOrder->purchase_units));
54 | $firstPurchaseUnit = $createdOrder->purchase_units[0];
55 | $this->assertEquals("test_ref_id1", $firstPurchaseUnit->reference_id);
56 | $this->assertEquals("USD", $firstPurchaseUnit->amount->currency_code);
57 | $this->assertEquals("200.00", $firstPurchaseUnit->amount->value);
58 | $this->assertEquals("added_description", $firstPurchaseUnit->description);
59 |
60 | $this->assertNotNull($createdOrder->create_time);
61 | $this->assertNotNull($createdOrder->links);
62 | $foundApproveUrl = false;
63 | foreach ($createdOrder->links as $link) {
64 | if ("approve" === $link->rel) {
65 | $foundApproveUrl = true;
66 | $this->assertNotNull($link->href);
67 | $this->assertEquals("GET", $link->method);
68 | }
69 | }
70 | $this->assertTrue($foundApproveUrl);
71 | $this->assertEquals("CREATED", $createdOrder->status);
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/tests/TestHarness.php:
--------------------------------------------------------------------------------
1 | >";
21 | $clientSecret = getenv("CLIENT_SECRET") ?: "<>";
22 | return new SandboxEnvironment($clientId, $clientSecret);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------