From e455460aebff9d9df26f3c4df8cf803454086223 Mon Sep 17 00:00:00 2001 From: Josh Marshall <josh@jmarshall.com.au> Date: Fri, 24 Nov 2023 12:03:25 +1000 Subject: [PATCH] first commit --- README.md | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..cb259df --- /dev/null +++ b/README.md @@ -0,0 +1,92 @@ +# PayPal REST Payment Module + +The Payum extension to purchase through PayPal using REST API + +## Install and Use + +To install, it's easiest to use composer: + + composer require cognito/payum_paypal_rest + +### Build the config + +```php +<?php + +use Payum\Core\PayumBuilder; +use Payum\Core\GatewayFactoryInterface; + +$defaultConfig = []; + +$payum = (new PayumBuilder) + ->addGatewayFactory('paypal_rest', function(array $config, GatewayFactoryInterface $coreGatewayFactory) { + return new \Cognito\PayumPayPalRest\PayPalRestGatewayFactory($config, $coreGatewayFactory); + }) + + ->addGateway('paypal_rest', [ + 'factory' => 'paypal_rest', + 'publishable_key' => 'Your Public Key', + 'secret_key' => 'Your Private Key', + 'img_url' => 'https://path/to/logo/image.jpg', + 'img_2_url' => 'https://path/to/logo/pay_by_image.jpg', + ]) + + ->getPayum() +; +``` + +### Request payment + +```php +<?php + +use Payum\Core\Request\Capture; + +$storage = $payum->getStorage(\Payum\Core\Model\Payment::class); +$request = [ + 'invoice_id' => 100, +]; + +$payment = $storage->create(); +$payment->setNumber(uniqid()); +$payment->setCurrencyCode($currency); +$payment->setTotalAmount(100); // Total cents +$payment->setDescription(substr($description, 0, 45)); +$payment->setDetails([ +]); +$storage->setInternalDetails($payment, $request); + +$captureToken = $payum->getTokenFactory()->createCaptureToken('paypal_rest', $payment, 'done.php'); +$url = $captureToken->getTargetUrl(); +header("Location: " . $url); +die(); +``` + +### Check it worked + +```php +<?php +/** @var \Payum\Core\Model\Token $token */ +$token = $payum->getHttpRequestVerifier()->verify($request); +$gateway = $payum->getGateway($token->getGatewayName()); + +/** @var \Payum\Core\Storage\IdentityInterface $identity **/ +$identity = $token->getDetails(); +$model = $payum->getStorage($identity->getClass())->find($identity); +$gateway->execute($status = new GetHumanStatus($model)); + +/** @var \Payum\Core\Request\GetHumanStatus $status */ + +// using shortcut +if ($status->isNew() || $status->isCaptured() || $status->isAuthorized()) { + // success +} elseif ($status->isPending()) { + // most likely success, but you have to wait for a push notification. +} elseif ($status->isFailed() || $status->isCanceled()) { + // the payment has failed or user canceled it. +} +``` + +## License + +Payum PayPal Rest is released under the [MIT License](LICENSE).