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\Paypal\Payment\SmartButton;
|
||||
use PayPalCheckoutSdk\Orders\OrdersGetRequest;
|
||||
|
||||
class Order
|
||||
{
|
||||
|
|
@ -15,33 +14,23 @@ class Order
|
|||
$order = $refund->order;
|
||||
|
||||
if ($order->payment->method === 'paypal_smart_button') {
|
||||
/* getting smart button instance */
|
||||
$smartButton = new SmartButton;
|
||||
|
||||
try {
|
||||
$paypalOrderID = $order->payment->additional['orderID'];
|
||||
/* getting paypal oder id */
|
||||
$paypalOrderID = $order->payment->additional['orderID'];
|
||||
|
||||
$smartButton = new SmartButton();
|
||||
$client = $smartButton->client();
|
||||
|
||||
/* 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);
|
||||
}
|
||||
/* getting capture id by paypal order id */
|
||||
$captureID = $smartButton->getCaptureId($paypalOrderID);
|
||||
|
||||
/* 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\Sales\Repositories\OrderRepository;
|
||||
use Webkul\Sales\Repositories\InvoiceRepository;
|
||||
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
|
||||
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
|
||||
|
||||
class SmartButtonController extends Controller
|
||||
{
|
||||
/**
|
||||
* SmartButton object
|
||||
* SmartButton $smartButton
|
||||
*
|
||||
* @var \Webkul\Paypal\Payment\SmartButton
|
||||
*/
|
||||
protected $smartButtonClient;
|
||||
protected $smartButton;
|
||||
|
||||
/**
|
||||
* OrderRepository object
|
||||
* OrderRepository $orderRepository
|
||||
*
|
||||
* @var \Webkul\Sales\Repositories\OrderRepository
|
||||
*/
|
||||
protected $orderRepository;
|
||||
|
||||
/**
|
||||
* InvoiceRepository object
|
||||
* InvoiceRepository $invoiceRepository
|
||||
*
|
||||
* @var \Webkul\Sales\Repositories\InvoiceRepository
|
||||
*/
|
||||
|
|
@ -35,21 +33,22 @@ class SmartButtonController extends Controller
|
|||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @param \Webkul\Paypal\Payment\SmartButton $smartButton
|
||||
* @param \Webkul\Attribute\Repositories\OrderRepository $orderRepository
|
||||
* @param \Webkul\Sales\Repositories\InvoiceRepository $invoiceRepository
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(
|
||||
SmartButton $smartButtonClient,
|
||||
SmartButton $smartButton,
|
||||
OrderRepository $orderRepository,
|
||||
InvoiceRepository $invoiceRepository
|
||||
)
|
||||
{
|
||||
$this->smartButton = $smartButton;
|
||||
|
||||
$this->orderRepository = $orderRepository;
|
||||
|
||||
$this->invoiceRepository = $invoiceRepository;
|
||||
|
||||
$this->smartButtonClient = $smartButtonClient->client();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -57,24 +56,19 @@ class SmartButtonController extends Controller
|
|||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function createOrder(OrdersCreateRequest $request)
|
||||
public function createOrder()
|
||||
{
|
||||
$request->prefer('return=representation');
|
||||
$request->body = $this->buildRequestBody();
|
||||
$response = $this->smartButtonClient->execute($request);
|
||||
return response()->json($response);
|
||||
return response()->json($this->smartButton->createOrder($this->buildRequestBody()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Capturing order.
|
||||
* Capturing paypal order after approval.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function captureOrder()
|
||||
{
|
||||
$request = new OrdersCaptureRequest(request()->input('orderData.orderID'));
|
||||
$request->prefer('return=representation');
|
||||
$this->smartButtonClient->execute($request);
|
||||
$this->smartButton->captureOrder(request()->input('orderData.orderID'));
|
||||
return $this->saveOrder();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,10 @@ namespace Webkul\Paypal\Payment;
|
|||
|
||||
use PayPalCheckoutSdk\Core\PayPalHttpClient;
|
||||
use PayPalCheckoutSdk\Core\SandboxEnvironment;
|
||||
use PayPalCheckoutSdk\Orders\OrdersGetRequest;
|
||||
use PayPalCheckoutSdk\Core\ProductionEnvironment;
|
||||
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
|
||||
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
|
||||
use PayPalCheckoutSdk\Payments\CapturesRefundRequest;
|
||||
|
||||
class SmartButton extends Paypal
|
||||
|
|
@ -38,6 +41,81 @@ class SmartButton extends Paypal
|
|||
$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
|
||||
*
|
||||
|
|
@ -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.
|
||||
* This sample uses SandboxEnvironment. In production, use LiveEnvironment.
|
||||
*
|
||||
* @return PayPalCheckoutSdk\Core\SandboxEnvironment|PayPalCheckoutSdk\Core\ProductionEnvironment
|
||||
*/
|
||||
protected function environment()
|
||||
{
|
||||
|
|
@ -87,6 +144,8 @@ class SmartButton extends Paypal
|
|||
|
||||
/**
|
||||
* Initialize properties.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function initialize()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue