|
15 | 15 | * set to "authorize" (to authorize a payment to be captured later) rather than
|
16 | 16 | * "sale" (which is used to capture a payment immediately).
|
17 | 17 | *
|
18 |
| - * Example: |
| 18 | + * ### Example |
| 19 | + * |
| 20 | + * #### Initialize Gateway |
19 | 21 | *
|
20 | 22 | * <code>
|
21 | 23 | * // Create a gateway for the PayPal RestGateway
|
|
28 | 30 | * 'secret' => 'MyPayPalSecret',
|
29 | 31 | * 'testMode' => true, // Or false when you are ready for live transactions
|
30 | 32 | * ));
|
| 33 | + * </code> |
| 34 | + * |
| 35 | + * #### Direct Credit Card Authorize |
31 | 36 | *
|
| 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> |
32 | 44 | * // Create a credit card object
|
33 | 45 | * // DO NOT USE THESE CARD VALUES -- substitute your own
|
34 | 46 | * // see the documentation in the class header.
|
|
66 | 78 | * As of January 2015 these transactions are only supported in the UK
|
67 | 79 | * and in the USA.
|
68 | 80 | *
|
| 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 | + * |
69 | 210 | * @link https://developer.paypal.com/docs/integration/direct/capture-payment/#authorize-the-payment
|
70 | 211 | * @link https://developer.paypal.com/docs/api/#authorizations
|
71 | 212 | * @link http://bit.ly/1wUQ33R
|
|
0 commit comments