{"id":341,"date":"2025-08-27T20:03:16","date_gmt":"2025-08-27T20:03:16","guid":{"rendered":"https:\/\/1v0.net\/blog\/?p=341"},"modified":"2025-08-27T20:03:18","modified_gmt":"2025-08-27T20:03:18","slug":"paypal-integration-in-laravel-step-by-step","status":"publish","type":"post","link":"https:\/\/1v0.net\/blog\/paypal-integration-in-laravel-step-by-step\/","title":{"rendered":"PayPal Integration in Laravel (Step by Step)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>PayPal Integration in Laravel (Step by Step)<\/strong><\/h2>\n\n\n\n<p>PayPal remains one of the most widely used payment gateways globally. Integrating it into your Laravel app allows you to accept payments from millions of users. In this guide, we\u2019ll set up PayPal SDK, configure API credentials, create routes for checkout, handle webhooks for payment confirmation, and build a simple Blade UI to process PayPal transactions.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1 &#8211; Install PayPal SDK<\/strong><\/h2>\n\n\n\n<p>Use Composer to install the official PayPal REST SDK.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\">composer require paypal\/rest-api-sdk-php<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This package provides the classes you need to create payment requests, handle approvals, and confirm completed payments via PayPal APIs.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2 &#8211; Configure PayPal Credentials<\/strong><\/h2>\n\n\n\n<p>Add your sandbox or live API credentials to <code>.env<\/code>:<\/p>\n\n\n<!-- DomainException(0): Unknown language: \"dotenv\" -->PAYPAL_CLIENT_ID=your-sandbox-client-id\nPAYPAL_SECRET=your-sandbox-secret\nPAYPAL_MODE=sandbox   # or live\n\n\n<p>Keep two sets of credentials: <code>sandbox<\/code> for testing and <code>live<\/code> for production. Never commit real secrets to your repository.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3 &#8211; Create PayPal Service<\/strong><\/h2>\n\n\n\n<p>Encapsulate PayPal logic in a service class for cleaner controllers.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ app\/Services\/PayPalService.php<\/span>\n<span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">App<\/span>\\<span class=\"hljs-title\">Services<\/span>;\n\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">PayPal<\/span>\\<span class=\"hljs-title\">Rest<\/span>\\<span class=\"hljs-title\">ApiContext<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">PayPal<\/span>\\<span class=\"hljs-title\">Auth<\/span>\\<span class=\"hljs-title\">OAuthTokenCredential<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">PayPal<\/span>\\<span class=\"hljs-title\">Api<\/span>\\<span class=\"hljs-title\">Amount<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">PayPal<\/span>\\<span class=\"hljs-title\">Api<\/span>\\<span class=\"hljs-title\">Payer<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">PayPal<\/span>\\<span class=\"hljs-title\">Api<\/span>\\<span class=\"hljs-title\">Payment<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">PayPal<\/span>\\<span class=\"hljs-title\">Api<\/span>\\<span class=\"hljs-title\">PaymentExecution<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">PayPal<\/span>\\<span class=\"hljs-title\">Api<\/span>\\<span class=\"hljs-title\">RedirectUrls<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">PayPal<\/span>\\<span class=\"hljs-title\">Api<\/span>\\<span class=\"hljs-title\">Transaction<\/span>;\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">PayPalService<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">protected<\/span> $apiContext;\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">()<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">$this<\/span>-&gt;apiContext = <span class=\"hljs-keyword\">new<\/span> ApiContext(\n            <span class=\"hljs-keyword\">new<\/span> OAuthTokenCredential(\n                config(<span class=\"hljs-string\">'services.paypal.client_id'<\/span>),\n                config(<span class=\"hljs-string\">'services.paypal.secret'<\/span>)\n            )\n        );\n        <span class=\"hljs-keyword\">$this<\/span>-&gt;apiContext-&gt;setConfig(&#91;\n            <span class=\"hljs-string\">'mode'<\/span> =&gt; config(<span class=\"hljs-string\">'services.paypal.mode'<\/span>, <span class=\"hljs-string\">'sandbox'<\/span>),\n        ]);\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">createPayment<\/span><span class=\"hljs-params\">($amount, $currency = <span class=\"hljs-string\">'USD'<\/span>)<\/span>\n    <\/span>{\n        $payer = <span class=\"hljs-keyword\">new<\/span> Payer();\n        $payer-&gt;setPaymentMethod(<span class=\"hljs-string\">'paypal'<\/span>);\n\n        $amountObj = <span class=\"hljs-keyword\">new<\/span> Amount();\n        $amountObj-&gt;setTotal($amount)-&gt;setCurrency($currency);\n\n        $transaction = <span class=\"hljs-keyword\">new<\/span> Transaction();\n        $transaction-&gt;setAmount($amountObj)-&gt;setDescription(<span class=\"hljs-string\">\"Order Payment\"<\/span>);\n\n        $redirectUrls = <span class=\"hljs-keyword\">new<\/span> RedirectUrls();\n        $redirectUrls-&gt;setReturnUrl(url(<span class=\"hljs-string\">'\/paypal\/success'<\/span>))\n                     -&gt;setCancelUrl(url(<span class=\"hljs-string\">'\/paypal\/cancel'<\/span>));\n\n        $payment = <span class=\"hljs-keyword\">new<\/span> Payment();\n        $payment-&gt;setIntent(<span class=\"hljs-string\">'sale'<\/span>)\n                -&gt;setPayer($payer)\n                -&gt;setTransactions(&#91;$transaction])\n                -&gt;setRedirectUrls($redirectUrls);\n\n        <span class=\"hljs-keyword\">return<\/span> $payment-&gt;create(<span class=\"hljs-keyword\">$this<\/span>-&gt;apiContext);\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">executePayment<\/span><span class=\"hljs-params\">($paymentId, $payerId)<\/span>\n    <\/span>{\n        $payment = Payment::get($paymentId, <span class=\"hljs-keyword\">$this<\/span>-&gt;apiContext);\n        $execution = <span class=\"hljs-keyword\">new<\/span> PaymentExecution();\n        $execution-&gt;setPayerId($payerId);\n\n        <span class=\"hljs-keyword\">return<\/span> $payment-&gt;execute($execution, <span class=\"hljs-keyword\">$this<\/span>-&gt;apiContext);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This service handles both creating a payment (with redirect URLs) and executing it after PayPal approves the transaction. Wrapping logic in a service keeps controllers lean.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4 &#8211; Checkout Controller<\/strong><\/h2>\n\n\n\n<p>Create a controller to start the payment and handle PayPal redirects.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ app\/Http\/Controllers\/PayPalController.php<\/span>\n<span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">App<\/span>\\<span class=\"hljs-title\">Http<\/span>\\<span class=\"hljs-title\">Controllers<\/span>;\n\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Illuminate<\/span>\\<span class=\"hljs-title\">Http<\/span>\\<span class=\"hljs-title\">Request<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">App<\/span>\\<span class=\"hljs-title\">Services<\/span>\\<span class=\"hljs-title\">PayPalService<\/span>;\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">PayPalController<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Controller<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">protected<\/span> $paypal;\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">(PayPalService $paypal)<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">$this<\/span>-&gt;paypal = $paypal;\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">checkout<\/span><span class=\"hljs-params\">()<\/span>\n    <\/span>{\n        $payment = <span class=\"hljs-keyword\">$this<\/span>-&gt;paypal-&gt;createPayment(<span class=\"hljs-number\">19.99<\/span>);\n        <span class=\"hljs-keyword\">return<\/span> redirect($payment-&gt;getApprovalLink());\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">success<\/span><span class=\"hljs-params\">(Request $request)<\/span>\n    <\/span>{\n        $paymentId = $request-&gt;query(<span class=\"hljs-string\">'paymentId'<\/span>);\n        $payerId = $request-&gt;query(<span class=\"hljs-string\">'PayerID'<\/span>);\n\n        $result = <span class=\"hljs-keyword\">$this<\/span>-&gt;paypal-&gt;executePayment($paymentId, $payerId);\n\n        <span class=\"hljs-comment\">\/\/ Update DB: mark order as paid<\/span>\n\n        <span class=\"hljs-keyword\">return<\/span> view(<span class=\"hljs-string\">'paypal.success'<\/span>, &#91;<span class=\"hljs-string\">'result'<\/span> =&gt; $result]);\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">cancel<\/span><span class=\"hljs-params\">()<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> view(<span class=\"hljs-string\">'paypal.cancel'<\/span>);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>checkout<\/code> method redirects users to PayPal. After approval, PayPal returns to <code>success<\/code>, where we execute the payment and mark the order as paid. The <code>cancel<\/code> method simply handles user cancellations.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5 &#8211; Routes<\/strong><\/h2>\n\n\n\n<p>Add routes to connect the flow.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ routes\/web.php<\/span>\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">App<\/span>\\<span class=\"hljs-title\">Http<\/span>\\<span class=\"hljs-title\">Controllers<\/span>\\<span class=\"hljs-title\">PayPalController<\/span>;\n\nRoute::get(<span class=\"hljs-string\">'\/paypal\/checkout'<\/span>, &#91;PayPalController::class, <span class=\"hljs-string\">'checkout'<\/span>]);\nRoute::get(<span class=\"hljs-string\">'\/paypal\/success'<\/span>, &#91;PayPalController::class, <span class=\"hljs-string\">'success'<\/span>]);\nRoute::get(<span class=\"hljs-string\">'\/paypal\/cancel'<\/span>, &#91;PayPalController::class, <span class=\"hljs-string\">'cancel'<\/span>]);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>These routes cover the full PayPal checkout cycle: start, success, and cancel. In production, secure them with auth checks to link payments to logged-in users.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6 &#8211; UI: PayPal Button<\/strong><\/h2>\n\n\n\n<p>You can use PayPal\u2019s JS SDK to render a checkout button instead of a simple link.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-comment\">&lt;!-- resources\/views\/paypal\/checkout.blade.php --&gt;<\/span>\n@extends('layouts.app')\n\n@section('content')\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"container\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Pay with PayPal<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"paypal-button-container\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/www.paypal.com\/sdk\/js?client-id={{ config('services.paypal.client_id') }}&amp;currency=USD\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span>&gt;<\/span><span class=\"javascript\">\npaypal.Buttons({\n  <span class=\"hljs-attr\">createOrder<\/span>: <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n    <span class=\"hljs-keyword\">return<\/span> fetch(<span class=\"hljs-string\">'\/paypal\/checkout'<\/span>)\n      .then(<span class=\"hljs-function\"><span class=\"hljs-params\">res<\/span> =&gt;<\/span> res.text());\n  },\n  <span class=\"hljs-attr\">onApprove<\/span>: <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\">data, actions<\/span>) <\/span>{\n    <span class=\"hljs-built_in\">window<\/span>.location.href = <span class=\"hljs-string\">'\/paypal\/success?paymentId='<\/span>+data.paymentID+<span class=\"hljs-string\">'&amp;PayerID='<\/span>+data.payerID;\n  }\n}).render(<span class=\"hljs-string\">'#paypal-button-container'<\/span>);\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n@endsection<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This example renders PayPal\u2019s official button on your page. Clicking it starts the checkout, and PayPal handles authentication and redirects. Always test in sandbox before going live.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>You\u2019ve successfully integrated PayPal into Laravel. You installed the SDK, configured credentials, built a service for clean code, handled checkout, and tested payments with a UI button. With this flow, you can accept PayPal transactions securely and expand your app\u2019s payment options.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">What\u2019s Next<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"\/blog\/how-to-build-a-multi-auth-api-with-laravel-sanctum\">How to Build a Multi-Auth API with Laravel &amp; Sanctum<\/a><\/li>\n<li><a href=\"\/blog\/using-laravel-passport-for-advanced-api-authentication\">Using Laravel Passport for Advanced API Authentication<\/a><\/li>\n<li><a href=\"\/blog\/integrating-laravel-with-third-party-apis-mail-sms-payment\">Integrating Laravel with Third-Party APIs (Mail, SMS, Payment)<\/a><\/li>\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>PayPal Integration in Laravel (Step by Step) PayPal remains one of the most widely used payment gateways globally. Integrating it into your Laravel app allows you to accept payments from millions of users. In this guide, we\u2019ll set up PayPal SDK, configure API credentials, create routes for checkout, handle webhooks for payment confirmation, and build [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":345,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[25,51,53],"class_list":["post-341","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-api","tag-payments","tag-paypal"],"_links":{"self":[{"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/posts\/341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/comments?post=341"}],"version-history":[{"count":1,"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/posts\/341\/revisions"}],"predecessor-version":[{"id":344,"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/posts\/341\/revisions\/344"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/media\/345"}],"wp:attachment":[{"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/media?parent=341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/categories?post=341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/1v0.net\/blog\/wp-json\/wp\/v2\/tags?post=341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}