Laravel paypal integration, switching from sandbox to live
dragos_cimpean
Contributor
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on
Jan-11-2019
05:31 AM
This file is in:
routes/api.php
<?php use PayPal\Api\Item; use PayPal\Api\Payer; use PayPal\Api\Amount; use PayPal\Api\Details; use PayPal\Api\Payment; use PayPal\Api\ItemList; use PayPal\Api\WebProfile; use PayPal\Api\InputFields; use PayPal\Api\Transaction; use Illuminate\Http\Request; use PayPal\Api\RedirectUrls; use PayPal\Api\PaymentExecution; use App\Backer; use App\Project; /* |-------------------------------------------------------------------------- | API Routes |-------------------------------------------------------------------------- | | Here is where you can register API routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | is assigned the "api" middleware group. Enjoy building your API! | */ Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); }); Route::post('create-payment', function () { $apiContext = new \PayPal\Rest\ApiContext( new \PayPal\Auth\OAuthTokenCredential( '<client-id>', // ClientID '<client-secret>' // ClientSecret ) ); $payer = new Payer(); $payer->setPaymentMethod("paypal"); $item1 = new Item(); $item1->setName($_POST['product_title']) ->setCurrency('USD') ->setQuantity(1) ->setSku($_POST['product_id']) // Similar to `item_number` in Classic API ->setPrice($_POST['product_price']); $itemList = new ItemList(); $itemList->setItems(array($item1)); $details = new Details(); $details->setShipping(0) ->setTax(0) ->setSubtotal($_POST['product_price']); $amount = new Amount(); $amount->setCurrency("USD") ->setTotal($_POST['product_price']) ->setDetails($details); $transaction = new Transaction(); $transaction->setAmount($amount) ->setItemList($itemList) ->setDescription("Payment description") ->setInvoiceNumber(uniqid()); $redirectUrls = new RedirectUrls(); $redirectUrls->setReturnUrl("https://locodor.com") ->setCancelUrl("https://locodor.com"); // Add NO SHIPPING OPTION $inputFields = new InputFields(); $inputFields->setNoShipping(1); $webProfile = new WebProfile(); $webProfile->setName('test' . uniqid())->setInputFields($inputFields); $webProfileId = $webProfile->create($apiContext)->getId(); $payment = new Payment(); $payment->setExperienceProfileId($webProfileId); // no shipping $payment->setIntent("sale") ->setPayer($payer) ->setRedirectUrls($redirectUrls) ->setTransactions(array($transaction)); try { $payment->create($apiContext); } catch (Exception $ex) { echo $ex; exit(1); } return $payment; }); Route::post('execute-payment', function (Request $request) { $apiContext = new \PayPal\Rest\ApiContext( new \PayPal\Auth\OAuthTokenCredential( '<client-id>, // ClientID '<client-secret>' // ClientSecret ) ); $paymentId = $request->paymentID; $payment = Payment::get($paymentId, $apiContext); $execution = new PaymentExecution(); $execution->setPayerId($request->payerID); // $transaction = new Transaction(); // $amount = new Amount(); // $details = new Details(); // $details->setShipping(2.2) // ->setTax(1.3) // ->setSubtotal(17.50); // $amount->setCurrency('USD'); // $amount->setTotal(21); // $amount->setDetails($details); // $transaction->setAmount($amount); // $execution->addTransaction($transaction); try { $result = $payment->execute($execution, $apiContext); $project = Project::find($request->project_id); $project->current_amount += $request->product_price; $project->funding_percent = round($project->current_amount / $project->project_goal * 100); $project->save(); $backer = new Backer; $backer->user_id = $request->auth_id; $backer->level_id = $request->product_id; $backer->project_id = $request->project_id; $backer->amount = $request->product_price; $backer->save(); } catch (Exception $ex) { echo $ex; exit(1); } return $result; });
and the blade view template is
<div id="paypal-top-button-{{ $level->id }}"></div> <script> paypal.Button.render({ env: 'production', // Or 'production' style: { size: 'medium', color: 'gold', shape: 'pill' }, // Set up the payment: // 1. Add a payment callback payment: function(data, actions) { // 2. Make a request to your server {{--document.getElementById("product-{{ $level->id }}").submit();--}} return actions.request.post('/api/create-payment',{ product_id: {{ $level->id }}, project_id: {{ $project->id }}, product_price: {{ $level->price }}, product_title: "{{ $level->title }}", }) .then(function(res) { // 3. Return res.id from the response return res.id; }); }, // Execute the payment: // 1. Add an onAuthorize callback onAuthorize: function(data, actions) { // 2. Make a request to your server return actions.request.post('/api/execute-payment', { paymentID: data.paymentID, payerID: data.payerID, auth_id: @if(Auth::check()) {{ Auth::id() }}, @else 0, @endif product_id: {{ $level->id }}, project_id: {{ $project->id }}, product_price: {{ $level->price }}, product_title: "{{ $level->title }}", }) .then(function(res) { alert('Your order has been received. Thank you for your purchase!'); }); } }, '#paypal-top-button-{{ $level->id }}'); </script>
The problem that i'm having is when i switch from the sandbox client id and secrete to the live ones, it dosent work anymore.
After switch to live, if I click the button it displayes the popup that is loading and suddenly it dissapears.
I would greatly need your help :).
Labels:
- Labels:
-
Payments REST APIs
0 REPLIES 0
Haven't Found your Answer?
It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.
Related Content
- Using Paypal Smart Buttons for checkout sometimes fails with Debit/ Credit Cards Form in Braintree Client-side Integration (JS, iOS, Android SDKs)
- Sandbox account not generating IPNs in Sandbox Environment
- I met the 422 response, and tries a lot solutions in the community but still not work in REST APIs
- Simple Paypal button integration results in 404 error in Sandbox Environment
- Enable PayPal Phone Number Request on sandbox account in Sandbox Environment