Skip to content

Commit dc788fc

Browse files
committed
Merge pull request #95 from delatbabel/update-rest-docblocks-6
Update REST docblocks
2 parents 08c5f1f + d46e532 commit dc788fc

File tree

5 files changed

+363
-53
lines changed

5 files changed

+363
-53
lines changed

src/Message/RestAuthorizeRequest.php

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
* set to "authorize" (to authorize a payment to be captured later) rather than
1616
* "sale" (which is used to capture a payment immediately).
1717
*
18-
* Example:
18+
* ### Example
19+
*
20+
* #### Initialize Gateway
1921
*
2022
* <code>
2123
* // Create a gateway for the PayPal RestGateway
@@ -28,7 +30,17 @@
2830
* 'secret' => 'MyPayPalSecret',
2931
* 'testMode' => true, // Or false when you are ready for live transactions
3032
* ));
33+
* </code>
34+
*
35+
* #### Direct Credit Card Authorize
3136
*
37+
* This is for the use case where a customer has presented their
38+
* credit card details and you intend to use the PayPal REST gateway
39+
* for processing a transaction using that credit card data.
40+
*
41+
* This does not require the customer to have a PayPal account.
42+
*
43+
* </code>
3244
* // Create a credit card object
3345
* // DO NOT USE THESE CARD VALUES -- substitute your own
3446
* // see the documentation in the class header.
@@ -66,6 +78,135 @@
6678
* As of January 2015 these transactions are only supported in the UK
6779
* and in the USA.
6880
*
81+
* #### PayPal Account Authorization
82+
*
83+
* This is for the use case where the customer intends to pay using their
84+
* PayPal account. Note that no credit card details are provided, instead
85+
* both a return URL and a cancel URL are required.
86+
*
87+
* The optimal solution here is to provide a unique return URL and cancel
88+
* URL per transaction. That way your code will know what transaction is
89+
* being returned or cancelled by PayPal.
90+
*
91+
* So step 1 is to store some transaction data somewhere on your system so
92+
* that you have an ID when your transaction returns. How you do this of
93+
* course depends on what framework, database layer, etc, you are using but
94+
* for this step let's assume that you have a class set up that can save
95+
* a transaction and return the object, and that you can retrieve the ID
96+
* of that saved object using some call like getId() on the object. Most
97+
* ORMs such as Doctrine ORM, Propel or Eloquent will have some methods
98+
* that will allow you to do this or something similar.
99+
*
100+
* <code>
101+
* $transaction = MyClass::saveTransaction($some_data);
102+
* $txn_id = $transaction->getId();
103+
* </code>
104+
*
105+
* Step 2 is to send the purchase request.
106+
*
107+
* </code>
108+
* // Do a purchase transaction on the gateway
109+
* try {
110+
* $transaction = $gateway->authorize(array(
111+
* 'amount' => '10.00',
112+
* 'currency' => 'AUD',
113+
* 'description' => 'This is a test authorize transaction.',
114+
* 'returnUrl' => 'http://mysite.com/paypal/return/?txn_id=' . $txn_id,
115+
* 'cancelUrl' => 'http://mysite.com/paypal/return/?txn_id=' . $txn_id,
116+
* ));
117+
* $response = $transaction->send();
118+
* $data = $response->getData();
119+
* echo "Gateway purchase response data == " . print_r($data, true) . "\n";
120+
*
121+
* if ($response->isSuccessful()) {
122+
* echo "Step 2 was successful!\n";
123+
* }
124+
*
125+
* } catch (\Exception $e) {
126+
* echo "Exception caught while attempting authorize.\n";
127+
* echo "Exception type == " . get_class($e) . "\n";
128+
* echo "Message == " . $e->getMessage() . "\n";
129+
* }
130+
* </code>
131+
*
132+
* Step 3 is where your code needs to redirect the customer to the PayPal
133+
* gateway so that the customer can sign in to their PayPal account and
134+
* agree to authorize the payment. The response will implement an interface
135+
* called RedirectResponseInterface from which the redirect URL can be obtained.
136+
*
137+
* How you do this redirect is up to your platform, code or framework at
138+
* this point. For the below example I will assume that there is a
139+
* function called redirectTo() which can handle it for you.
140+
*
141+
* </code>
142+
* if ($response->isRedirect()) {
143+
* // Redirect the customer to PayPal so that they can sign in and
144+
* // authorize the payment.
145+
* echo "The transaction is a redirect";
146+
* redirectTo($response->getRedirectUrl());
147+
* }
148+
* </code>
149+
*
150+
* Step 4 is where the customer returns to your site. This will happen on
151+
* either the returnUrl or the cancelUrl, that you provided in the purchase()
152+
* call.
153+
*
154+
* If the cancelUrl is called then you can assume that the customer has not
155+
* authorized the payment, therefore you can cancel the transaction.
156+
*
157+
* If the returnUrl is called, then you need to complete the transaction via
158+
* a further call to PayPal.
159+
*
160+
* Note this example assumes that the authorize has been successful.
161+
*
162+
* The payer ID and the payment ID returned from the callback after the authorize
163+
* will be passed to the return URL as GET parameters payerId and paymentId
164+
* respectively.
165+
*
166+
* <code>
167+
* $paymentId = $_GET['paymentId'];
168+
* $payerId = $_GET['payerId'];
169+
*
170+
* // Once the transaction has been approved, we need to complete it.
171+
* $transaction = $gateway->completePurchase(array(
172+
* 'payer_id' => $payer_id,
173+
* 'transactionReference' => $sale_id,
174+
* ));
175+
* $response = $transaction->send();
176+
* if ($response->isSuccessful()) {
177+
* // The customer has successfully paid.
178+
* echo "Step 4 was successful!\n";
179+
* } else {
180+
* // There was an error returned by completePurchase(). You should
181+
* // check the error code and message from PayPal, which may be something
182+
* // like "card declined", etc.
183+
* }
184+
* </code>
185+
*
186+
* #### Note on Handling Error Messages
187+
*
188+
* PayPal account payments are a 2 step process. Firstly the customer needs to
189+
* authorize the payment from PayPal to your application. Secondly, assuming that
190+
* the customer does not have enough balance to pay the invoice from their PayPal
191+
* balance, PayPal needs to transfer the funds from the customer's credit card to
192+
* their PayPal account. This transaction is between PayPal and the customer, and
193+
* not between the customer and you.
194+
*
195+
* If the second transaction fails then a call to completePurchase() will return
196+
* an error. However this error message will be fairly generic. For privacy
197+
* reasons, PayPal will not disclose to the merchant the full reason for the
198+
* failure, they will only disclose this to the customer.
199+
*
200+
* Therefore on a failed completeAuthorize() call you could display an error message
201+
* like this one:
202+
*
203+
* "PayPal failed to process the transaction from your card. For privacy reasons,
204+
* PayPal are unable to disclose to us the reason for this failure. You should try
205+
* a different payment method, a different card within PayPal, or contact PayPal
206+
* support if you need to understand the reason for the failed transaction. PayPal
207+
* may advise you to use a different card if the particular card is rejected
208+
* by the card issuer."
209+
*
69210
* @link https://developer.paypal.com/docs/integration/direct/capture-payment/#authorize-the-payment
70211
* @link https://developer.paypal.com/docs/api/#authorizations
71212
* @link http://bit.ly/1wUQ33R

src/Message/RestCaptureRequest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
* indicate a final capture (prevent future captures) by setting the is_final_capture
1919
* value to true.
2020
*
21-
* Example -- note this example assumes that the authorization has been successful
21+
* ### Example
22+
*
23+
* Note this example assumes that the authorization has been successful
2224
* and that the authorization ID returned from the authorization is held in $auth_id.
2325
* See RestAuthorizeRequest for the first part of this example transaction:
2426
*

src/Message/RestCompletePurchaseRequest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,31 @@
1515
* This call only works after a buyer has approved the payment using the
1616
* provided PayPal approval URL.
1717
*
18-
* Example -- note this example assumes that the purchase has been successful
19-
* and that the payer ID returned from the callback after the purchase is held
20-
* in $payer_id, and that the transaction ID returned by the initial payment is
21-
* held in $sale_id
18+
* ### Example
19+
*
20+
* The payer ID and the payment ID returned from the callback after the purchase
21+
* will be passed to the return URL as GET parameters payerId and paymentId
22+
* respectively.
23+
*
2224
* See RestPurchaseRequest for the first part of this example transaction:
2325
*
2426
* <code>
27+
* $paymentId = $_GET['paymentId'];
28+
* $payerId = $_GET['payerId'];
29+
*
2530
* // Once the transaction has been approved, we need to complete it.
2631
* $transaction = $gateway->completePurchase(array(
2732
* 'payer_id' => $payer_id,
2833
* 'transactionReference' => $sale_id,
2934
* ));
3035
* $response = $transaction->send();
36+
* if ($response->isSuccessful()) {
37+
* // The customer has successfully paid.
38+
* } else {
39+
* // There was an error returned by completePurchase(). You should
40+
* // check the error code and message from PayPal, which may be something
41+
* // like "card declined", etc.
42+
* }
3143
* </code>
3244
*
3345
* @see RestPurchaseRequest

0 commit comments

Comments
 (0)