Refactoring Done
This commit is contained in:
parent
f5c3501aab
commit
4d8a6cf0ed
|
|
@ -4,7 +4,6 @@ namespace Webkul\Admin\Listeners;
|
||||||
|
|
||||||
use Webkul\Admin\Traits\Mails;
|
use Webkul\Admin\Traits\Mails;
|
||||||
use Webkul\Paypal\Payment\SmartButton;
|
use Webkul\Paypal\Payment\SmartButton;
|
||||||
use PayPalCheckoutSdk\Orders\OrdersGetRequest;
|
|
||||||
|
|
||||||
class Order
|
class Order
|
||||||
{
|
{
|
||||||
|
|
@ -15,33 +14,23 @@ class Order
|
||||||
$order = $refund->order;
|
$order = $refund->order;
|
||||||
|
|
||||||
if ($order->payment->method === 'paypal_smart_button') {
|
if ($order->payment->method === 'paypal_smart_button') {
|
||||||
|
/* getting smart button instance */
|
||||||
|
$smartButton = new SmartButton;
|
||||||
|
|
||||||
try {
|
/* getting paypal oder id */
|
||||||
$paypalOrderID = $order->payment->additional['orderID'];
|
$paypalOrderID = $order->payment->additional['orderID'];
|
||||||
|
|
||||||
$smartButton = new SmartButton();
|
/* getting capture id by paypal order id */
|
||||||
$client = $smartButton->client();
|
$captureID = $smartButton->getCaptureId($paypalOrderID);
|
||||||
|
|
||||||
/* get order */
|
|
||||||
$paypalOrderDetails = $client->execute(new OrdersGetRequest($paypalOrderID));
|
|
||||||
$captureID = $paypalOrderDetails->result->purchase_units[0]->payments->captures[0]->id;
|
|
||||||
|
|
||||||
/* refunding order */
|
|
||||||
$smartButton->refundOrder($captureID, [
|
|
||||||
'amount' =>
|
|
||||||
[
|
|
||||||
'value' => $refund->grand_total,
|
|
||||||
'currency_code' => $refund->order_currency_code
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
/* reporting error */
|
|
||||||
report($e);
|
|
||||||
|
|
||||||
/* now aborting whole process */
|
|
||||||
abort(500);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/* now refunding order on the basis of capture id and refund data */
|
||||||
|
$smartButton->refundOrder($captureID, [
|
||||||
|
'amount' =>
|
||||||
|
[
|
||||||
|
'value' => $refund->grand_total,
|
||||||
|
'currency_code' => $refund->order_currency_code
|
||||||
|
]
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,27 +6,25 @@ use Webkul\Checkout\Facades\Cart;
|
||||||
use Webkul\Paypal\Payment\SmartButton;
|
use Webkul\Paypal\Payment\SmartButton;
|
||||||
use Webkul\Sales\Repositories\OrderRepository;
|
use Webkul\Sales\Repositories\OrderRepository;
|
||||||
use Webkul\Sales\Repositories\InvoiceRepository;
|
use Webkul\Sales\Repositories\InvoiceRepository;
|
||||||
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
|
|
||||||
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
|
|
||||||
|
|
||||||
class SmartButtonController extends Controller
|
class SmartButtonController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* SmartButton object
|
* SmartButton $smartButton
|
||||||
*
|
*
|
||||||
* @var \Webkul\Paypal\Payment\SmartButton
|
* @var \Webkul\Paypal\Payment\SmartButton
|
||||||
*/
|
*/
|
||||||
protected $smartButtonClient;
|
protected $smartButton;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OrderRepository object
|
* OrderRepository $orderRepository
|
||||||
*
|
*
|
||||||
* @var \Webkul\Sales\Repositories\OrderRepository
|
* @var \Webkul\Sales\Repositories\OrderRepository
|
||||||
*/
|
*/
|
||||||
protected $orderRepository;
|
protected $orderRepository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* InvoiceRepository object
|
* InvoiceRepository $invoiceRepository
|
||||||
*
|
*
|
||||||
* @var \Webkul\Sales\Repositories\InvoiceRepository
|
* @var \Webkul\Sales\Repositories\InvoiceRepository
|
||||||
*/
|
*/
|
||||||
|
|
@ -35,21 +33,22 @@ class SmartButtonController extends Controller
|
||||||
/**
|
/**
|
||||||
* Create a new controller instance.
|
* Create a new controller instance.
|
||||||
*
|
*
|
||||||
|
* @param \Webkul\Paypal\Payment\SmartButton $smartButton
|
||||||
* @param \Webkul\Attribute\Repositories\OrderRepository $orderRepository
|
* @param \Webkul\Attribute\Repositories\OrderRepository $orderRepository
|
||||||
* @param \Webkul\Sales\Repositories\InvoiceRepository $invoiceRepository
|
* @param \Webkul\Sales\Repositories\InvoiceRepository $invoiceRepository
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
SmartButton $smartButtonClient,
|
SmartButton $smartButton,
|
||||||
OrderRepository $orderRepository,
|
OrderRepository $orderRepository,
|
||||||
InvoiceRepository $invoiceRepository
|
InvoiceRepository $invoiceRepository
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
$this->smartButton = $smartButton;
|
||||||
|
|
||||||
$this->orderRepository = $orderRepository;
|
$this->orderRepository = $orderRepository;
|
||||||
|
|
||||||
$this->invoiceRepository = $invoiceRepository;
|
$this->invoiceRepository = $invoiceRepository;
|
||||||
|
|
||||||
$this->smartButtonClient = $smartButtonClient->client();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -57,24 +56,19 @@ class SmartButtonController extends Controller
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Http\JsonResponse
|
* @return \Illuminate\Http\JsonResponse
|
||||||
*/
|
*/
|
||||||
public function createOrder(OrdersCreateRequest $request)
|
public function createOrder()
|
||||||
{
|
{
|
||||||
$request->prefer('return=representation');
|
return response()->json($this->smartButton->createOrder($this->buildRequestBody()));
|
||||||
$request->body = $this->buildRequestBody();
|
|
||||||
$response = $this->smartButtonClient->execute($request);
|
|
||||||
return response()->json($response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Capturing order.
|
* Capturing paypal order after approval.
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function captureOrder()
|
public function captureOrder()
|
||||||
{
|
{
|
||||||
$request = new OrdersCaptureRequest(request()->input('orderData.orderID'));
|
$this->smartButton->captureOrder(request()->input('orderData.orderID'));
|
||||||
$request->prefer('return=representation');
|
|
||||||
$this->smartButtonClient->execute($request);
|
|
||||||
return $this->saveOrder();
|
return $this->saveOrder();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,10 @@ namespace Webkul\Paypal\Payment;
|
||||||
|
|
||||||
use PayPalCheckoutSdk\Core\PayPalHttpClient;
|
use PayPalCheckoutSdk\Core\PayPalHttpClient;
|
||||||
use PayPalCheckoutSdk\Core\SandboxEnvironment;
|
use PayPalCheckoutSdk\Core\SandboxEnvironment;
|
||||||
|
use PayPalCheckoutSdk\Orders\OrdersGetRequest;
|
||||||
use PayPalCheckoutSdk\Core\ProductionEnvironment;
|
use PayPalCheckoutSdk\Core\ProductionEnvironment;
|
||||||
|
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
|
||||||
|
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
|
||||||
use PayPalCheckoutSdk\Payments\CapturesRefundRequest;
|
use PayPalCheckoutSdk\Payments\CapturesRefundRequest;
|
||||||
|
|
||||||
class SmartButton extends Paypal
|
class SmartButton extends Paypal
|
||||||
|
|
@ -38,6 +41,81 @@ class SmartButton extends Paypal
|
||||||
$this->initialize();
|
$this->initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns PayPal HTTP client instance with environment that has access
|
||||||
|
* credentials context. Use this instance to invoke PayPal APIs, provided the
|
||||||
|
* credentials have access.
|
||||||
|
*
|
||||||
|
* @return PayPalCheckoutSdk\Core\PayPalHttpClient
|
||||||
|
*/
|
||||||
|
public function client()
|
||||||
|
{
|
||||||
|
return new PayPalHttpClient($this->environment());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create order for approval of client.
|
||||||
|
*
|
||||||
|
* @param array $body
|
||||||
|
* @return HttpResponse
|
||||||
|
*/
|
||||||
|
public function createOrder($body)
|
||||||
|
{
|
||||||
|
$request = new OrdersCreateRequest;
|
||||||
|
$request->prefer('return=representation');
|
||||||
|
$request->body = $body;
|
||||||
|
return $this->client()->execute($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Capture order after approval.
|
||||||
|
*
|
||||||
|
* @param string $orderId
|
||||||
|
* @return HttpResponse
|
||||||
|
*/
|
||||||
|
public function captureOrder($orderId)
|
||||||
|
{
|
||||||
|
$request = new OrdersCaptureRequest($orderId);
|
||||||
|
$request->prefer('return=representation');
|
||||||
|
$this->client()->execute($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get order details.
|
||||||
|
*
|
||||||
|
* @param string $orderId
|
||||||
|
* @return HttpResponse
|
||||||
|
*/
|
||||||
|
public function getOrder($orderId)
|
||||||
|
{
|
||||||
|
return $this->client()->execute(new OrdersGetRequest($orderId));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get capture id.
|
||||||
|
*
|
||||||
|
* @param string $orderId
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getCaptureId($orderId)
|
||||||
|
{
|
||||||
|
$paypalOrderDetails = $this->getOrder($orderId);
|
||||||
|
return $paypalOrderDetails->result->purchase_units[0]->payments->captures[0]->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refund order.
|
||||||
|
*
|
||||||
|
* @return HttpResponse
|
||||||
|
*/
|
||||||
|
public function refundOrder($captureId, $body = [])
|
||||||
|
{
|
||||||
|
$request = new CapturesRefundRequest($captureId);
|
||||||
|
$request->body = $body;
|
||||||
|
return $this->client()->execute($request);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return paypal redirect url
|
* Return paypal redirect url
|
||||||
*
|
*
|
||||||
|
|
@ -47,32 +125,11 @@ class SmartButton extends Paypal
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns PayPal HTTP client instance with environment that has access
|
|
||||||
* credentials context. Use this instance to invoke PayPal APIs, provided the
|
|
||||||
* credentials have access.
|
|
||||||
*/
|
|
||||||
public function client()
|
|
||||||
{
|
|
||||||
return new PayPalHttpClient($this->environment());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Refund order.
|
|
||||||
*/
|
|
||||||
public function refundOrder($captureId, $body = [])
|
|
||||||
{
|
|
||||||
/* generating request */
|
|
||||||
$request = new CapturesRefundRequest($captureId);
|
|
||||||
$request->body = $body;
|
|
||||||
|
|
||||||
/* requesting refund */
|
|
||||||
return $this->client()->execute($request);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set up and return PayPal PHP SDK environment with PayPal access credentials.
|
* Set up and return PayPal PHP SDK environment with PayPal access credentials.
|
||||||
* This sample uses SandboxEnvironment. In production, use LiveEnvironment.
|
* This sample uses SandboxEnvironment. In production, use LiveEnvironment.
|
||||||
|
*
|
||||||
|
* @return PayPalCheckoutSdk\Core\SandboxEnvironment|PayPalCheckoutSdk\Core\ProductionEnvironment
|
||||||
*/
|
*/
|
||||||
protected function environment()
|
protected function environment()
|
||||||
{
|
{
|
||||||
|
|
@ -87,6 +144,8 @@ class SmartButton extends Paypal
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize properties.
|
* Initialize properties.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function initialize()
|
protected function initialize()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue