|
| 1 | +# Payment Method: Paypal |
| 2 | + |
| 3 | +## Create payment |
| 4 | + |
| 5 | +```php |
| 6 | + |
| 7 | +$credentials = new Credentials(); |
| 8 | + |
| 9 | +$credentials->setEnv("sandbox"); //sandbox OR live |
| 10 | +$credentials->setApiKey(""); //Your paypal Api Key |
| 11 | +$credentials->setSecretKey(""); //Your paypal Api Key |
| 12 | +$credentials->setLogEnabled(false); |
| 13 | +//if logEnabled set is true uncoment next line |
| 14 | +//$credentials->setLogPath(__DIR__ . "/log/paypal_log.log"); |
| 15 | + |
| 16 | +$attributes = new Attributes(); |
| 17 | + |
| 18 | +$attributes->setAmount("10"); |
| 19 | +$attributes->setCurrency("USD"); |
| 20 | +$attributes->setDescription("Order Amount"); |
| 21 | +$attributes->setProcessUrl("/dist/process.php"); // Payment process page |
| 22 | +$attributes->setBackUrl("/back.php"); |
| 23 | + |
| 24 | +$payment = Pay::createPayment(Pay::PM_PAYPAL, $credentials, $attributes); |
| 25 | + |
| 26 | +if ($payment) { |
| 27 | + $payment_id = $payment->getId(); |
| 28 | + $url = $payment->getId(); |
| 29 | + //redirect to url |
| 30 | +} else { |
| 31 | + // "Payment creation failed |
| 32 | +} |
| 33 | + |
| 34 | +``` |
| 35 | + |
| 36 | +## Process payment |
| 37 | + |
| 38 | +```php |
| 39 | +$pay = new Pay(); |
| 40 | + |
| 41 | +$credentials = new Credentials(); |
| 42 | + |
| 43 | +$credentials->setEnv(""); |
| 44 | +$credentials->setApiKey(""); |
| 45 | +$credentials->setSecretKey(""); |
| 46 | +$credentials->setLogEnabled(false); |
| 47 | +$credentials->setLogPath(__DIR__ . "/log/paypal_log.log"); |
| 48 | + |
| 49 | +$attributes = new Attributes(); |
| 50 | +$attributes->setAcceptOnlyVerifiedAccounts(false); |
| 51 | + |
| 52 | +$payment = Pay::processPayment(Pay::PM_PAYPAL, $credentials, $attributes); |
| 53 | + |
| 54 | +if($payment){ |
| 55 | + if($payment->getStatus() === "approved"){ |
| 56 | + //payment is confirmed |
| 57 | + }elseif($payment->getStatus() === "canceled"){ |
| 58 | + //payment is canceled |
| 59 | + }else($payment->getStatus() === "failed"){ |
| 60 | + //payment is failed |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +``` |
0 commit comments