|
1 |
| -# Lunar Opayo Payments |
| 1 | +<p align="center"><img src="https://github.com/lunarphp/lunar/assets/1488016/a21f1cfb-9259-4d21-9bb0-eca876957729" width="300" ></p> |
2 | 2 |
|
3 |
| -WIP |
| 3 | + |
| 4 | + |
| 5 | +<p align="center">This addon enables Opayo payments on your Lunar storefront.</p> |
| 6 | + |
| 7 | +## Alpha Release |
| 8 | + |
| 9 | +This addon is currently in Alpha, whilst every step is taken to ensure this is working as intended, it will not be considered out of Alpha until more tests have been added and proved. |
| 10 | + |
| 11 | +## Minimum Requirements |
| 12 | + |
| 13 | +- Lunar `1.x` |
| 14 | +- An [Elavon](https://www.elavon.com/) merchant account |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +### Require the composer package |
| 19 | + |
| 20 | +```sh |
| 21 | +composer require lunarphp/opayo |
| 22 | +``` |
| 23 | + |
| 24 | +### Configure the service |
| 25 | + |
| 26 | +Add the opayo config to the `config/services.php` file. |
| 27 | + |
| 28 | +```php |
| 29 | +// ... |
| 30 | +'opayo' => [ |
| 31 | + 'vendor' => env('OPAYO_VENDOR'), |
| 32 | + 'env' => env('OPAYO_ENV', 'test'), |
| 33 | + 'key' => env('OPAYO_KEY'), |
| 34 | + 'password' => env('OPAYO_PASSWORD'), |
| 35 | + 'host' => env('OPAYO_HOST'), |
| 36 | +], |
| 37 | +``` |
| 38 | + |
| 39 | + |
| 40 | +### Enable the driver |
| 41 | + |
| 42 | +Set the driver in `config/lunar/payments.php` |
| 43 | + |
| 44 | +```php |
| 45 | +<?php |
| 46 | + |
| 47 | +return [ |
| 48 | + // ... |
| 49 | + 'types' => [ |
| 50 | + 'card' => [ |
| 51 | + // ... |
| 52 | + 'driver' => 'opayo', |
| 53 | + ], |
| 54 | + ], |
| 55 | +]; |
| 56 | +``` |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +## Configuration |
| 61 | + |
| 62 | +Below is a list of the available configuration options this package uses in `config/lunar/opayo.php` |
| 63 | + |
| 64 | +| Key | Default | Description | |
| 65 | +| --- | --- | --- | |
| 66 | +| `policy` | `automatic` | Determines the policy for taking payments and whether you wish to capture the payment manually later or take payment straight away. Available options `deferred` or `automatic` | |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +## Backend Usage |
| 71 | + |
| 72 | +### Get a merchant key |
| 73 | + |
| 74 | +```php |
| 75 | +Lunar\Opayo\Facades\Opayo::getMerchantKey(); |
| 76 | +``` |
| 77 | + |
| 78 | +### Authorize a charge |
| 79 | + |
| 80 | +```php |
| 81 | +$response = \Lunar\Facades\Payments::driver('opayo')->cart( |
| 82 | + $cart = CartSession::current()->calculate() |
| 83 | +)->withData([ |
| 84 | + 'merchant_key' => $request->get('merchantSessionKey'), |
| 85 | + 'card_identifier' => $request->get('cardToken'), |
| 86 | + 'browserLanguage' => $request->get('browserLanguage'), |
| 87 | + 'challengeWindowSize' => $request->get('challengeWindowSize'), |
| 88 | + 'browserIP' => $request->ip(), |
| 89 | + 'browserAcceptHeader' => $request->header('accept'), |
| 90 | + 'browserUserAgent' => $request->get('browserUserAgent'), |
| 91 | + 'browserJavaEnabled' => $request->get('browserJavaEnabled', false), |
| 92 | + 'browserColorDepth' => $request->get('browserColorDepth'), |
| 93 | + 'browserScreenHeight' => $request->get('browserScreenHeight'), |
| 94 | + 'browserScreenWidth' => $request->get('browserScreenWidth'), |
| 95 | + 'browserTZ' => $request->get('browserTZ'), |
| 96 | + 'status' => 'payment-received', |
| 97 | +])->authorize(); |
| 98 | +``` |
| 99 | + |
| 100 | +When authorizing a charge, you may be required to submit extra authentication in the form of 3DSV2, you can handle this in your payment endpoint. |
| 101 | + |
| 102 | +```php |
| 103 | +if (is_a($response, \Lunar\Opayo\Responses\ThreeDSecureResponse::class)) { |
| 104 | + return response()->json([ |
| 105 | + 'requires_auth' => true, |
| 106 | + 'data' => $response, |
| 107 | + ]); |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +`$response` will contain all the 3DSV2 information from Opayo. |
| 112 | + |
| 113 | +You can find more information about this using the following links: |
| 114 | + |
| 115 | +- [3-D Secure explained](https://www.elavon.co.uk/resource-center/help-with-your-solutions/opayo/fraud-prevention/3D-Secure.html) |
| 116 | +- [3D Secure Transactions](https://developer.elavon.com/products/opayo-direct/v1/3d-secure-transactions) |
| 117 | +- Stack overflow [SagePay 3D Secure V2 Flow](https://stackoverflow.com/questions/65329436/sagepay-3d-secure-v2-flow) |
| 118 | + |
| 119 | +Once you have handled the 3DSV2 response on your storefront, you can then authorize again. |
| 120 | + |
| 121 | +```php |
| 122 | +$response = Payments::driver('opayo')->cart( |
| 123 | + $cart = CartSession::current()->calculate() |
| 124 | +)->withData([ |
| 125 | + 'cres' => $request->get('cres'), |
| 126 | + 'pares' => $request->get('pares'), |
| 127 | + 'transaction_id' => $request->get('transaction_id'), |
| 128 | +])->threedsecure(); |
| 129 | + |
| 130 | +if (! $response->success) { |
| 131 | + abort(401); |
| 132 | +} |
| 133 | + |
| 134 | +``` |
| 135 | + |
| 136 | +### Opayo card tokens |
| 137 | + |
| 138 | +When authenticated users make an order on your store, it can be good to offer the ability to save their card information for future use. Whilst we don't store the actual card details, we can use card tokens which represent the card the user has used before. |
| 139 | + |
| 140 | +> You must have saved payments enabled on your Opayo account because you can use these. |
| 141 | +
|
| 142 | +To save a card, pass in the `saveCard` data key when authorizing a payment. |
| 143 | + |
| 144 | +```php |
| 145 | +$response = \Lunar\Facades\Payments::driver('opayo')->cart( |
| 146 | + $cart = CartSession::current()->calculate() |
| 147 | +)->withData([ |
| 148 | + // ... |
| 149 | + 'saveCard' => true |
| 150 | +])->authorize(); |
| 151 | +``` |
| 152 | + |
| 153 | +Assuming everything went well, there will be a new entry in the `opayo_tokens` table, associated to the authenticated user. You can then display these card representations at checkout for the user to select. The `token` is what replaces the `card_identifier` data key. |
| 154 | + |
| 155 | +```php |
| 156 | +$response = \Lunar\Facades\Payments::driver('opayo')->cart( |
| 157 | + $cart = CartSession::current()->calculate() |
| 158 | +)->withData([ |
| 159 | + // ... |
| 160 | + 'card_identifier' => $request->get('cardToken'), |
| 161 | + 'reusable' => true |
| 162 | +])->authorize(); |
| 163 | +``` |
| 164 | + |
| 165 | +Responses are then handled the same as any other transaction. |
| 166 | + |
| 167 | +## Contributing |
| 168 | + |
| 169 | +Contributions are welcome, if you are thinking of adding a feature, please submit an issue first so we can determine whether it should be included. |
0 commit comments