|
1 | 1 | # payfast-php-sdk |
2 | 2 |
|
3 | | -This repository is no longer supported and has been archived. |
| 3 | +The Payfast PHP SDK provides an easy-to-use library for integrating Payfast payments into your project. |
| 4 | +This includes Custom Integration, Onsite Integration and all APIs. |
| 5 | + |
| 6 | +## Requirements |
| 7 | + |
| 8 | +PHP 8.1 and later. |
| 9 | + |
| 10 | +## Documentation |
| 11 | + |
| 12 | +See the [Developer Docs](https://developers.payfast.co.za/docs) |
| 13 | + |
| 14 | +## Composer |
| 15 | + |
| 16 | +You can install the library via [Composer](http://getcomposer.org/). Run the following command: |
| 17 | + |
| 18 | +```bash |
| 19 | +composer require payfast/payfast-php-sdk |
| 20 | +``` |
| 21 | + |
| 22 | +To use the library, use Composer's [autoload](https://getcomposer.org/doc/01-basic-usage.md#autoloading): |
| 23 | + |
| 24 | +```php |
| 25 | +require_once('vendor/autoload.php'); |
| 26 | +``` |
| 27 | + |
| 28 | +## Getting Started |
| 29 | + |
| 30 | +### Custom Integration |
| 31 | + |
| 32 | +Build a checkout form and receive payments securely from the Payfast payment platform. |
| 33 | + |
| 34 | +**NB!** The default value of 'testMode' is true. |
| 35 | + |
| 36 | +See the [Developer Docs](https://developers.payfast.co.za/docs#quickstart) |
| 37 | + |
| 38 | +```php |
| 39 | +try { |
| 40 | + $payfast = new PayfastPayment( |
| 41 | + [ |
| 42 | + 'merchantId' => '10000100', |
| 43 | + 'merchantKey' => '46f0cd694581a', |
| 44 | + 'passPhrase' => 'jt7NOE43FZPn', |
| 45 | + 'testMode' => true |
| 46 | + ] |
| 47 | + ); |
| 48 | + |
| 49 | + $data = [ |
| 50 | + 'amount' => '100.00', |
| 51 | + 'item_name' => 'Order#123' |
| 52 | + ]; |
| 53 | + |
| 54 | + echo $payfast->custom->createFormFields($data, ['value' => 'PAY NOW', 'class' => 'btn']); |
| 55 | +} catch(Exception $e) { |
| 56 | + echo 'There was an exception: '.$e->getMessage(); |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### Onsite Payments |
| 61 | + |
| 62 | +Integrate Payfast’s secure payment engine directly into your checkout page. |
| 63 | + |
| 64 | +**NB!** The default value of 'testMode' is false. Otherwise we get the Exception: "_**There was an exception: Sorry but Onsite is not available in Sandbox mode**_" |
| 65 | + |
| 66 | +See the [Developer Docs](https://developers.payfast.co.za/docs#onsite_payments) |
| 67 | + |
| 68 | +```php |
| 69 | +// Include: <script src="https://www.payfast.co.za/onsite/engine.js"></script> |
| 70 | + |
| 71 | +try { |
| 72 | + $payfast = new PayfastPayment( |
| 73 | + [ |
| 74 | + 'merchantId' => '10000100', |
| 75 | + 'merchantKey' => '46f0cd694581a', |
| 76 | + 'passPhrase' => 'jt7NOE43FZPn', |
| 77 | + 'testMode' => false |
| 78 | + ] |
| 79 | + ); |
| 80 | + |
| 81 | + $data = [ |
| 82 | + 'amount' => '100.00', |
| 83 | + 'item_name' => 'Order#123' |
| 84 | + ]; |
| 85 | + |
| 86 | + // Generate payment identifier |
| 87 | + $identifier = $payfast->onsite->generatePaymentIdentifier($data); |
| 88 | + |
| 89 | + if($identifier!== null){ |
| 90 | + echo '<script type="text/javascript">window.payfast_do_onsite_payment({"uuid":"'.$identifier.'"});</script>'; |
| 91 | + } |
| 92 | +} catch(Exception $e) { |
| 93 | + echo 'There was an exception: '.$e->getMessage(); |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### API |
| 98 | + |
| 99 | +#### Recurring Billing |
| 100 | + |
| 101 | +The Subscription Payments API gives Merchants the ability to interact with subscriptions on their accounts. |
| 102 | + |
| 103 | +**NB!** The default value of 'testMode' is true. |
| 104 | + |
| 105 | +See the [Developer Docs](https://developers.payfast.co.za/api#recurring-billing) |
| 106 | + |
| 107 | +```php |
| 108 | +try { |
| 109 | + $api = new PayfastApi( |
| 110 | + [ |
| 111 | + 'merchantId' => '10018867', |
| 112 | + 'passPhrase' => '2uU_k5q_vRS_', |
| 113 | + 'testMode' => true |
| 114 | + ] |
| 115 | + ); |
| 116 | + |
| 117 | + $fetchArray = $api->subscriptions->fetch('2afa4575-5628-051a-d0ed-4e071b56a7b0'); |
| 118 | + |
| 119 | + $pauseArray = $api->subscriptions->pause('2afa4575-5628-051a-d0ed-4e071b56a7b0', ['cycles' => 1]); |
| 120 | + |
| 121 | + $unpauseArray = $api->subscriptions->unpause('2afa4575-5628-051a-d0ed-4e071b56a7b0'); |
| 122 | + |
| 123 | + $cancelArray = $api->subscriptions->cancel('2afa4575-5628-051a-d0ed-4e071b56a7b0'); |
| 124 | + |
| 125 | + $updateArray = $api->subscriptions->update('2afa4575-5628-051a-d0ed-4e071b56a7b0', ['cycles' => 1]); |
| 126 | + |
| 127 | + $adhocArray = $api->subscriptions->adhoc('290ac9a6-25f1-cce4-5801-67a644068818', ['amount' => 500, 'item_name' => 'Test adhoc']); |
| 128 | + |
| 129 | +} catch(Exception $e) { |
| 130 | + echo 'There was an exception: '.$e->getMessage(); |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +#### Update card |
| 135 | + |
| 136 | +The update card endpoint allows you to provide buyers with a link to update their card details on a Recurring Billing subscription or Tokenization charges. |
| 137 | + |
| 138 | +**NB!** The default value of 'testMode' is false. Otherwise we get the Exception: "_**There was an exception: Sorry but Onsite is not available in Sandbox mode**_" |
| 139 | + |
| 140 | +See the [Developer Docs](https://developers.payfast.co.za/docs#recurring_card_update) |
| 141 | + |
| 142 | +```php |
| 143 | +try { |
| 144 | + $payfast = new PayfastPayment( |
| 145 | + [ |
| 146 | + 'merchantId' => '10000100', |
| 147 | + 'merchantKey' => '46f0cd694581a', |
| 148 | + 'passPhrase' => 'jt7NOE43FZPn', |
| 149 | + 'testMode' => false |
| 150 | + ] |
| 151 | + ); |
| 152 | + |
| 153 | + echo $payfast->custom->createCardUpdateLink('2afa4575-5628-051a-d0ed-4e071b56a7b0', 'https://www.example.com/return', 'Update your card', ['target' => '_blank']); |
| 154 | + |
| 155 | +} catch(Exception $e) { |
| 156 | + echo 'There was an exception: '.$e->getMessage(); |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +#### Transaction History |
| 161 | + |
| 162 | +The transaction history API gives Merchants the ability to interact with their Payfast account. |
| 163 | + |
| 164 | +**NB!** The default value of 'testMode' is true. |
| 165 | + |
| 166 | +See the [Developer Docs](https://developers.payfast.co.za/api#transaction-history) |
| 167 | + |
| 168 | +```php |
| 169 | +try { |
| 170 | + $api = new PayfastApi( |
| 171 | + [ |
| 172 | + 'merchantId' => '10018867', |
| 173 | + 'passPhrase' => '2uU_k5q_vRS_', |
| 174 | + 'testMode' => true |
| 175 | + ] |
| 176 | + ); |
| 177 | + |
| 178 | + $rangeArray = $api->transactionHistory->range(['from' => '2020-08-01', 'to' => '2020-08-07', 'offset' => 0, 'limit' => 1000]); |
| 179 | + |
| 180 | + $dailyArray = $api->transactionHistory->daily(['date' => '2020-08-07', 'offset' => 0, 'limit' => 1000]); |
| 181 | + |
| 182 | + $weeklyArray = $api->transactionHistory->weekly(['date' => '2020-08-07', 'offset' => 0, 'limit' => 1000]); |
| 183 | + |
| 184 | + $monthlyArray = $api->transactionHistory->monthly(['date' => '2020-08', 'offset' => 0, 'limit' => 1000]); |
| 185 | + |
| 186 | +} catch(Exception $e) { |
| 187 | + echo 'There was an exception: '.$e->getMessage(); |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +#### Credit card transaction query |
| 192 | + |
| 193 | +The credit card transaction query API gives Merchants the ability to query credit card transactions. |
| 194 | + |
| 195 | +**NB!** The default value of 'testMode' is true. |
| 196 | + |
| 197 | +See the [Developer Docs](https://developers.payfast.co.za/api#credit-card-transactions) |
| 198 | + |
| 199 | +```php |
| 200 | +try { |
| 201 | + $api = new PayfastApi( |
| 202 | + [ |
| 203 | + 'merchantId' => '10018867', |
| 204 | + 'passPhrase' => '2uU_k5q_vRS_', |
| 205 | + 'testMode' => true |
| 206 | + ] |
| 207 | + ); |
| 208 | + |
| 209 | + $creditCardArray = $api->creditCardTransactions->fetch('1124148'); |
| 210 | + |
| 211 | +} catch(Exception $e) { |
| 212 | + echo 'There was an exception: '.$e->getMessage(); |
| 213 | +} |
| 214 | +``` |
| 215 | + |
| 216 | +#### Refunds |
| 217 | + |
| 218 | +The Refunds API Providing gives Merchants the ability to perform refunds on their account. |
| 219 | + |
| 220 | +**NB!** The default value of 'testMode' is false. Otherwise we get the Exception: "_**There was an exception: Sorry but Onsite is not available in Sandbox mode**_" |
| 221 | + |
| 222 | +See the [Developer Docs](https://developers.payfast.co.za/api#refunds) |
| 223 | + |
| 224 | +```php |
| 225 | +try { |
| 226 | + $api = new PayfastApi( |
| 227 | + [ |
| 228 | + 'merchantId' => '10018867', |
| 229 | + 'passPhrase' => '2uU_k5q_vRS_', |
| 230 | + 'testMode' => false |
| 231 | + ] |
| 232 | + ); |
| 233 | + |
| 234 | + $refundFetchArray = $api->refunds->fetch('dc0521d3-55fe-269b-fa00-b647310d760f'); |
| 235 | + |
| 236 | + $refundCreateArray = $api->refunds->create('dc0521d3-55fe-269b-fa00-b647310d760f', ['amount' => 50, 'reason' => 'Product returned', 'acc_type' => 'savings']); |
| 237 | + |
| 238 | +} catch(Exception $e) { |
| 239 | + echo 'There was an exception: '.$e->getMessage(); |
| 240 | +} |
| 241 | +``` |
0 commit comments