Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
1,454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.64 KB | None | 0 0
  1. <?php
  2. namespace App\Http\Controllers;
  3.  
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Input;
  6. use PayPal\Api\Amount;
  7. use PayPal\Api\Details;
  8. use PayPal\Api\Item;
  9. use PayPal\Api\ItemList;
  10. use PayPal\Api\Payer;
  11. use PayPal\Api\Payee;
  12. use PayPal\Api\Payment;
  13. use PayPal\Api\PaymentExecution;
  14. use PayPal\Api\RedirectUrls;
  15. use PayPal\Api\Transaction;
  16. use PayPal\Auth\OAuthTokenCredential;
  17. use PayPal\Rest\ApiContext;
  18. use Redirect;
  19. use Session;
  20. use URL;
  21. use App\Invoice;
  22. use App\Order;
  23. use App\Suborder;
  24.  
  25. use Cart;
  26. use Auth;
  27.  
  28. class TestController extends Controller
  29. {
  30.     private $_api_context;
  31.     /**
  32.      * Create a new controller instance.
  33.      *
  34.      * @return void
  35.      */
  36.     public function __construct()
  37.     {
  38.         /** PayPal api context **/
  39.         $paypal_conf = \Config::get('paypal');
  40.         $this->_api_context = new ApiContext(new OAuthTokenCredential(
  41.                 $paypal_conf['client_id'],
  42.                 $paypal_conf['secret'])
  43.         );
  44.         $this->_api_context->setConfig($paypal_conf['settings']);
  45.     }
  46.     public function index()
  47.     {
  48.         return view('webstore.test');
  49.     }
  50.  
  51.     public function payWithpaypal(Request $request)
  52.     {
  53.         # We set the payment method to PayPal
  54.        $payer = new Payer();
  55.         $payer->setPaymentMethod('paypal');
  56.  
  57.         $items[] = [
  58.             'name' => 'Item 1',
  59.             'quantity' => 1,
  60.             'currency' => 'USD',
  61.             'price' => '20'
  62.         ];
  63.  
  64.         $item_1 = new Item();
  65.         $item_1->setName('Item 1')
  66.             ->setCurrency('USD')
  67.             ->setQuantity(1)
  68.             ->setPrice(5);
  69.  
  70.         $item_2 = new Item();
  71.         $item_2->setName('Item 2')
  72.             ->setCurrency('USD')
  73.             ->setQuantity(1)
  74.             ->setPrice(5);
  75.  
  76.         $item_list = new ItemList();
  77.         $item_list->setItems(array($item_1, $item_2));
  78.  
  79.         $amount = new Amount();
  80.         $amount->setCurrency('USD')
  81.             ->setTotal(10);
  82.  
  83.         $transaction = new Transaction();
  84.         $transaction->setAmount($amount);
  85.  
  86.         $payee = new Payee();
  87.         $payee->setEmail("tenant@test.com");
  88.         $transaction->setPayee($payee)
  89.             ->setItemList($item_list)
  90.             ->setDescription('Your transaction description');
  91.  
  92.         $redirect_urls = new RedirectUrls();
  93.         $redirect_urls->setReturnUrl(URL::to('status')) /** Specify return URL **/
  94.         ->setCancelUrl(URL::to('status'));
  95.  
  96.         $payment = new Payment();
  97.         $payment->setIntent('Sale')
  98.             ->setPayer($payer)
  99.             ->setRedirectUrls($redirect_urls)
  100.             ->setTransactions(array($transaction));
  101.  
  102.         try {
  103.             $payment->create($this->_api_context);
  104.         } catch (\PayPal\Exception\PPConnectionException $ex) {
  105.             if (\Config::get('app.debug')) {
  106.                 \Session::put('error', 'Connection timeout');
  107.                 return Redirect::to('/');
  108.             } else {
  109.                 \Session::put('error', 'Some error occur, sorry for inconvenient');
  110.                 return Redirect::to('/');
  111.             }
  112.         }
  113.         foreach ($payment->getLinks() as $link) {
  114.             if ($link->getRel() == 'approval_url') {
  115.                 $redirect_url = $link->getHref();
  116.                 break;
  117.             }
  118.         }
  119.         /** add payment ID to session **/
  120.         Session::put('paypal_payment_id', $payment->getId());
  121.         if (isset($redirect_url)) {
  122.             /** redirect to paypal **/
  123.             return Redirect::away($redirect_url);
  124.         }
  125.         \Session::put('error', 'Unknown error occurred');
  126.         return Redirect::to('/');
  127.     }
  128.     public function getPaymentStatus()
  129.     {
  130.         /** Get the payment ID before session clear **/
  131.         $payment_id = Session::get('paypal_payment_id');
  132.         /** clear the session payment ID **/
  133.         Session::forget('paypal_payment_id');
  134.         if (empty(Input::get('PayerID')) || empty(Input::get('token'))) {
  135.             \Session::put('error', 'Payment failed');
  136.             return Redirect::to('/');
  137.         }
  138.         $payment = Payment::get($payment_id, $this->_api_context);
  139.         $execution = new PaymentExecution();
  140.         $execution->setPayerId(Input::get('PayerID'));
  141.         /**Execute the payment **/
  142.         $result = $payment->execute($execution, $this->_api_context);
  143.         if ($result->getState() == 'approved') {
  144.  
  145.             /* Save order in database */
  146.             \Session::put('success', 'Payment success');
  147.             return Redirect::to('/');
  148.         }
  149.         \Session::put('error', 'Payment failed');
  150.         return Redirect::to('/');
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement