Viewing file: PayPalPayment.php (3.28 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\PaymentProvider;
use Exception; use Illuminate\Support\Facades\Log; use Omnipay\Omnipay; use Illuminate\Support\Facades\Crypt;
class PayPalPayment implements PaymentInterface {
public $paymentId; public $request; public $redirect_url; public $error_message; public $return_view; public $will_redirect = false; public $amount; public $type; public $gateway; public $data; public $credentials;
public function __construct() {
}
public function pay(){
}
public function type($type){ $this->type=$type; return $this; } public function credentials($credentials){ $this->credentials=$credentials; return $this; } public function amount($amount){ $this->amount=$amount; return $this; } public function data($data){ $this->data=$data; return $this; }
public function will_redirect() { // TODO: Implement will_redirect() method. return $this->will_redirect; }
public function redirect_url() { // TODO: Implement redirect_url() method. return $this->redirect_url; }
public function return_view() { // TODO: Implement redirect_url() method. return $this->return_view; }
public function error_message() { // TODO: Implement error_message() method. return $this->error_message; }
public function process(){
try { $credentials=$this->credentials;
if(!isset($credentials['is_paypal_enabled']) && $credentials['is_paypal_enabled'] !='on' && !isset($credentials['paypal_client_id']) || !isset($credentials['paypal_secret_key'])){ throw new Exception('Invalid credentials'); $this->error_message = 'Invalid credentials'; }
$paypal_client_id=$credentials['paypal_client_id']; $paypal_client_secret=$credentials['paypal_secret_key'];
$mode = isset($credentials['paypal_mode']) && $credentials['paypal_mode'] && $credentials['paypal_mode'] == 'live' ? 'false' : 'true'; $gateway = Omnipay::create('PayPal_Rest'); $gateway->setClientId($paypal_client_id); $gateway->setSecret($paypal_client_secret); $gateway->setTestMode($mode);
$order=$this->data; $trx_id=Crypt::encryptString($order->trx_id); $response = $gateway->purchase(array( 'amount' => $this->amount, 'currency' => 'USD', 'returnUrl' => route('paypal.payment.success', ['order_id'=>$order->id,'thx_id'=>$trx_id]), 'cancelUrl' => route('paypal.payment.cancel'), ))->send();
if ($response->isRedirect()) { $this->redirect_url = $response->redirect(); $this->will_redirect = true; $this->return_view = null;
// this will automatically forward the customer } else { Log::error($response->getMessage()); $this->error_message = $response->getMessage(); } } catch (\Exception $e) { Log::error($e->getMessage()); $this->error_message = $e->getMessage(); }
}
}
|