Added paypal smart payment button method

This commit is contained in:
jitendra 2020-09-23 16:40:21 +05:30
parent b6001fbc31
commit 8b23fa74ab
25 changed files with 595 additions and 93 deletions

View File

@ -1354,7 +1354,10 @@ return [
'instructions' => 'Instructions',
'custom-scripts' => 'Custom Scripts',
'custom-css' => 'Custom CSS',
'custom-javascript' => 'Custom Javascript'
'custom-javascript' => 'Custom Javascript',
'paypal-smart-button' => 'Paypal Smart Button',
'client-id' => 'Client Id',
'client-id-info' => 'Use "sb" for testing.'
]
]
];

View File

@ -1337,7 +1337,10 @@ return [
'instructions' => 'Instructions',
'custom-scripts' => 'Custom Scripts',
'custom-css' => 'Custom CSS',
'custom-javascript' => 'Custom Javascript'
'custom-javascript' => 'Custom Javascript',
'paypal-smart-button' => 'Paypal Smart Button',
'client-id' => 'Client Id',
'client-id-info' => 'Use "sb" for testing.'
],
],
];

View File

@ -1359,7 +1359,10 @@ return [
'instructions' => 'Instructions',
'custom-scripts' => 'Custom Scripts',
'custom-css' => 'Custom CSS',
'custom-javascript' => 'Custom Javascript'
'custom-javascript' => 'Custom Javascript',
'paypal-smart-button' => 'Paypal Smart Button',
'client-id' => 'Client Id',
'client-id-info' => 'Use "sb" for testing.'
]
]
];

View File

@ -1319,7 +1319,10 @@ return [
'instructions' => 'Instructions',
'custom-scripts' => 'Custom Scripts',
'custom-css' => 'Custom CSS',
'custom-javascript' => 'Custom Javascript'
'custom-javascript' => 'Custom Javascript',
'paypal-smart-button' => 'Paypal Smart Button',
'client-id' => 'Client Id',
'client-id-info' => 'Use "sb" for testing.'
]
]
];

View File

@ -1355,7 +1355,10 @@ return [
'instructions' => 'Instructions',
'custom-scripts' => 'Custom Scripts',
'custom-css' => 'Custom CSS',
'custom-javascript' => 'Custom Javascript'
'custom-javascript' => 'Custom Javascript',
'paypal-smart-button' => 'Paypal Smart Button',
'client-id' => 'Client Id',
'client-id-info' => 'Use "sb" for testing.'
]
]
];

View File

@ -1360,7 +1360,10 @@ return [
'instructions' => 'Instructions',
'custom-scripts' => 'Custom Scripts',
'custom-css' => 'Custom CSS',
'custom-javascript' => 'Custom Javascript'
'custom-javascript' => 'Custom Javascript',
'paypal-smart-button' => 'Paypal Smart Button',
'client-id' => 'Client Id',
'client-id-info' => 'Use "sb" for testing.'
]
]
];

View File

@ -1355,7 +1355,10 @@ return [
'instructions' => 'Instructions',
'custom-scripts' => 'Custom Scripts',
'custom-css' => 'Custom CSS',
'custom-javascript' => 'Custom Javascript'
'custom-javascript' => 'Custom Javascript',
'paypal-smart-button' => 'Paypal Smart Button',
'client-id' => 'Client Id',
'client-id-info' => 'Use "sb" for testing.'
]
]
];

View File

@ -1344,7 +1344,10 @@ return [
'instructions' => 'Instructions',
'custom-scripts' => 'Custom Scripts',
'custom-css' => 'Custom CSS',
'custom-javascript' => 'Custom Javascript'
'custom-javascript' => 'Custom Javascript',
'paypal-smart-button' => 'Paypal Smart Button',
'client-id' => 'Client Id',
'client-id-info' => 'Use "sb" for testing.'
]
]
];

View File

@ -1358,7 +1358,10 @@ return [
'instructions' => 'Instructions',
'custom-scripts' => 'Custom Scripts',
'custom-css' => 'Custom CSS',
'custom-javascript' => 'Custom Javascript'
'custom-javascript' => 'Custom Javascript',
'paypal-smart-button' => 'Paypal Smart Button',
'client-id' => 'Client Id',
'client-id-info' => 'Use "sb" for testing.'
]
]
];

View File

@ -1341,7 +1341,10 @@ return [
'instructions' => 'Instructions',
'custom-scripts' => 'Custom Scripts',
'custom-css' => 'Custom CSS',
'custom-javascript' => 'Custom Javascript'
'custom-javascript' => 'Custom Javascript',
'paypal-smart-button' => 'Paypal Smart Button',
'client-id' => 'Client Id',
'client-id-info' => 'Use "sb" for testing.'
]
]
];

View File

@ -16,16 +16,5 @@ return [
'class' => 'Webkul\Payment\Payment\MoneyTransfer',
'active' => true,
'sort' => 2,
],
'paypal_standard' => [
'code' => 'paypal_standard',
'title' => 'Paypal Standard',
'description' => 'Paypal Standard',
'class' => 'Webkul\Paypal\Payment\Standard',
'sandbox' => true,
'active' => true,
'business_account' => 'test@webkul.com',
'sort' => 3,
]
];

View File

@ -113,64 +113,5 @@ return [
],
]
]
], [
'key' => 'sales.paymentmethods.paypal_standard',
'name' => 'admin::app.admin.system.paypal-standard',
'sort' => 3,
'fields' => [
[
'name' => 'title',
'title' => 'admin::app.admin.system.title',
'type' => 'text',
'validation' => 'required',
'channel_based' => false,
'locale_based' => true,
], [
'name' => 'description',
'title' => 'admin::app.admin.system.description',
'type' => 'textarea',
'channel_based' => false,
'locale_based' => true,
], [
'name' => 'business_account',
'title' => 'admin::app.admin.system.business-account',
'type' => 'select',
'type' => 'text',
'validation' => 'required',
], [
'name' => 'active',
'title' => 'admin::app.admin.system.status',
'type' => 'boolean',
'validation' => 'required',
'channel_based' => false,
'locale_based' => true
], [
'name' => 'sandbox',
'title' => 'admin::app.admin.system.sandbox',
'type' => 'boolean',
'validation' => 'required',
'channel_based' => false,
'locale_based' => true,
], [
'name' => 'sort',
'title' => 'admin::app.admin.system.sort_order',
'type' => 'select',
'options' => [
[
'title' => '1',
'value' => 1,
], [
'title' => '2',
'value' => 2,
], [
'title' => '3',
'value' => 3,
], [
'title' => '4',
'value' => 4,
],
],
]
]
]
];

View File

@ -44,6 +44,7 @@ class PaymentServiceProvider extends ServiceProvider
return new Payment();
});
}
/**
* Register package config.
*

View File

@ -0,0 +1,23 @@
<?php
return [
'paypal_standard' => [
'code' => 'paypal_standard',
'title' => 'Paypal Standard',
'description' => 'Paypal Standard',
'class' => 'Webkul\Paypal\Payment\Standard',
'sandbox' => true,
'active' => true,
'business_account' => 'test@webkul.com',
'sort' => 3,
],
'paypal_smart_button' => [
'code' => 'paypal_smart_button',
'title' => 'Paypal Smart Button',
'description' => 'Paypal Smart Button',
'class' => 'Webkul\Paypal\Payment\SmartButton',
'sandbox' => true,
'active' => true,
'sort' => 4,
]
];

View File

@ -0,0 +1,117 @@
<?php
return [
[
'key' => 'sales.paymentmethods.paypal_standard',
'name' => 'admin::app.admin.system.paypal-standard',
'sort' => 3,
'fields' => [
[
'name' => 'title',
'title' => 'admin::app.admin.system.title',
'type' => 'text',
'validation' => 'required',
'channel_based' => false,
'locale_based' => true,
], [
'name' => 'description',
'title' => 'admin::app.admin.system.description',
'type' => 'textarea',
'channel_based' => false,
'locale_based' => true,
], [
'name' => 'business_account',
'title' => 'admin::app.admin.system.business-account',
'type' => 'select',
'type' => 'text',
'validation' => 'required',
], [
'name' => 'active',
'title' => 'admin::app.admin.system.status',
'type' => 'boolean',
'validation' => 'required',
'channel_based' => false,
'locale_based' => true
], [
'name' => 'sandbox',
'title' => 'admin::app.admin.system.sandbox',
'type' => 'boolean',
'validation' => 'required',
'channel_based' => false,
'locale_based' => true,
], [
'name' => 'sort',
'title' => 'admin::app.admin.system.sort_order',
'type' => 'select',
'options' => [
[
'title' => '1',
'value' => 1,
], [
'title' => '2',
'value' => 2,
], [
'title' => '3',
'value' => 3,
], [
'title' => '4',
'value' => 4,
],
],
]
]
], [
'key' => 'sales.paymentmethods.paypal_smart_button',
'name' => 'admin::app.admin.system.paypal-smart-button',
'sort' => 3,
'fields' => [
[
'name' => 'title',
'title' => 'admin::app.admin.system.title',
'type' => 'text',
'validation' => 'required',
'channel_based' => false,
'locale_based' => true,
], [
'name' => 'description',
'title' => 'admin::app.admin.system.description',
'type' => 'textarea',
'channel_based' => false,
'locale_based' => true,
], [
'name' => 'client_id',
'title' => 'admin::app.admin.system.client-id',
'info' => 'admin::app.admin.system.client-id-info',
'type' => 'select',
'type' => 'text',
'validation' => 'required',
], [
'name' => 'active',
'title' => 'admin::app.admin.system.status',
'type' => 'boolean',
'validation' => 'required',
'channel_based' => false,
'locale_based' => true
], [
'name' => 'sort',
'title' => 'admin::app.admin.system.sort_order',
'type' => 'select',
'options' => [
[
'title' => '1',
'value' => 1,
], [
'title' => '2',
'value' => 2,
], [
'title' => '3',
'value' => 3,
], [
'title' => '4',
'value' => 4,
],
],
]
]
]
];

View File

@ -118,9 +118,7 @@ class Ipn
*/
protected function prepareInvoiceData()
{
$invoiceData = [
"order_id" => $this->order->id,
];
$invoiceData = ["order_id" => $this->order->id,];
foreach ($this->order->items as $item) {
$invoiceData['invoice']['items'][$item->id] = $item->qty_to_invoice;

View File

@ -0,0 +1,268 @@
<?php
namespace Webkul\Paypal\Http\Controllers;
use Webkul\Checkout\Facades\Cart;
use Webkul\Sales\Repositories\OrderRepository;
use Webkul\Sales\Repositories\InvoiceRepository;
class SmartButtonController extends Controller
{
/**
* OrderRepository object
*
* @var \Webkul\Sales\Repositories\OrderRepository
*/
protected $orderRepository;
/**
* InvoiceRepository object
*
* @var \Webkul\Sales\Repositories\InvoiceRepository
*/
protected $invoiceRepository;
/**
* Create a new controller instance.
*
* @param \Webkul\Attribute\Repositories\OrderRepository $orderRepository
* @param \Webkul\Sales\Repositories\InvoiceRepository $invoiceRepository
* @return void
*/
public function __construct(
OrderRepository $orderRepository,
InvoiceRepository $invoiceRepository
)
{
$this->orderRepository = $orderRepository;
$this->invoiceRepository = $invoiceRepository;
}
/**
* Success payment
*
* @return \Illuminate\Http\JsonResponse
*/
public function details()
{
$cart = Cart::getCart();
$billingAddressLines = $this->getAddressLines($cart->billing_address->address1);
$data = [
'intent' => 'CAPTURE',
'payer' => [
'name' => [
'given_name' => $cart->billing_address->first_name,
'surname' => $cart->billing_address->last_name,
],
'address' => [
'address_line_1' => current($billingAddressLines),
'address_line_2' => last($billingAddressLines),
'admin_area_2' => $cart->billing_address->city,
'admin_area_1' => $cart->billing_address->state,
'postal_code' => $cart->billing_address->postcode,
'country_code' => $cart->billing_address->country,
],
'email_address' => $cart->billing_address->email,
'phone' => [
'phone_type' => 'MOBILE',
'phone_number' => [
'national_number' => $cart->billing_address->phone,
],
],
],
'application_context' => [
'shipping_preference' => 'NO_SHIPPING'
],
'purchase_units' => [
[
'amount' => [
'value' => (float) $cart->sub_total + $cart->tax_total + ($cart->selected_shipping_rate ? $cart->selected_shipping_rate->price : 0) - $cart->discount_amount,
'currency_code' => $cart->cart_currency_code,
'breakdown' => [
'item_total' => [
'currency_code' => $cart->cart_currency_code,
'value' => (float) $cart->sub_total,
],
'shipping' => [
'currency_code' => $cart->cart_currency_code,
'value' => (float) ($cart->selected_shipping_rate ? $cart->selected_shipping_rate->price : 0),
],
'tax_total' => [
'currency_code' => $cart->cart_currency_code,
'value' => (float) $cart->tax_total,
],
'discount' => [
'currency_code' => $cart->cart_currency_code,
'value' => (float) $cart->discount_amount,
],
],
],
'items' => $this->getLineItems($cart),
],
]
];
if ($cart->haveStockableItems() && $cart->shipping_address) {
$shippingAddressLines = $this->getAddressLines($cart->shipping_address->address1);
$data['purchase_units'][0] = array_merge($data['purchase_units'][0], [
'shipping' => [
'address' => [
'address_line_1' => current($billingAddressLines),
'address_line_2' => last($billingAddressLines),
'admin_area_2' => $cart->shipping_address->city,
'admin_area_1' => $cart->shipping_address->state,
'postal_code' => $cart->shipping_address->postcode,
'country_code' => $cart->shipping_address->country,
],
],
]);
}
return $data;
}
/**
* Return cart items
*
* @param string $cart
* @return array
*/
public function getLineItems($cart)
{
$lineItems = [];
foreach ($cart->items as $item) {
$lineItems[] = [
'unit_amount' => [
'currency_code' => $cart->cart_currency_code,
'value' => (float) $item->price,
],
'quantity' => $item->quantity,
'name' => $item->name,
'sku' => $item->sku,
];
}
return $lineItems;
}
/**
* Return convert multiple address lines into 2 address lines
*
* @param string $address
* @return array
*/
public function getAddressLines($address)
{
$address = explode(PHP_EOL, $address, 2);
$addressLines = [current($address)];
if (isset($address[1])) {
$addressLines[] = str_replace(["\r\n", "\r", "\n"], ' ', last($address));
} else {
$addressLines[] = '';
}
return $addressLines;
}
/**
* Save order
*
* @return \Illuminate\Http\Response
*/
public function saveOrder()
{
if (Cart::hasError()) {
return response()->json(['redirect_url' => route('shop.checkout.cart.index')], 403);
}
try {
Cart::collectTotals();
$this->validateOrder();
$cart = Cart::getCart();
$order = $this->orderRepository->create(Cart::prepareDataForOrder());
$this->orderRepository->update(['status' => 'processing'], $order->id);
if ($order->canInvoice()) {
$invoice = $this->invoiceRepository->create($this->prepareInvoiceData($order));
}
Cart::deActivateCart();
session()->flash('order', $order);
return response()->json([
'success' => true,
]);
} catch (\Exception $e) {
session()->flash('error', trans('shop::app.common.error'));
throw $e;
}
}
/**
* Prepares order's invoice data for creation
*
* @param \Webkul\Sales\Models\Order $order
* @return array
*/
protected function prepareInvoiceData($order)
{
$invoiceData = ["order_id" => $order->id,];
foreach ($order->items as $item) {
$invoiceData['invoice']['items'][$item->id] = $item->qty_to_invoice;
}
return $invoiceData;
}
/**
* Validate order before creation
*
* @return void|\Exception
*/
public function validateOrder()
{
$cart = Cart::getCart();
if ($cart->haveStockableItems() && ! $cart->shipping_address) {
throw new \Exception(trans('Please check shipping address.'));
}
if (! $cart->billing_address) {
throw new \Exception(trans('Please check billing address.'));
}
if ($cart->haveStockableItems() && ! $cart->selected_shipping_rate) {
throw new \Exception(trans('Please specify shipping method.'));
}
if (! $cart->payment) {
throw new \Exception(trans('Please specify payment method.'));
}
}
}

View File

@ -9,6 +9,12 @@ Route::group(['middleware' => ['web']], function () {
Route::get('/cancel', 'Webkul\Paypal\Http\Controllers\StandardController@cancel')->name('paypal.standard.cancel');
});
Route::prefix('paypal/smart-button')->group(function () {
Route::get('/details', 'Webkul\Paypal\Http\Controllers\SmartButtonController@details')->name('paypal.smart_button.details');
Route::post('/save-order', 'Webkul\Paypal\Http\Controllers\SmartButtonController@saveOrder')->name('paypal.smart_button.save_order');
});
});
Route::get('paypal/standard/ipn', 'Webkul\Paypal\Http\Controllers\StandardController@ipn')->name('paypal.standard.ipn');

View File

@ -0,0 +1,22 @@
<?php
namespace Webkul\Paypal\Payment;
class SmartButton extends Paypal
{
/**
* Payment method code
*
* @var string
*/
protected $code = 'paypal_smart_button';
/**
* Return paypal redirect url
*
* @return string
*/
public function getRedirectUrl()
{
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace Webkul\Paypal\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Event;
use Webkul\Theme\ViewRenderEventManager;
class EventServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
Event::listen('bagisto.shop.layout.body.after', static function(ViewRenderEventManager $viewRenderEventManager) {
$viewRenderEventManager->addTemplate('paypal::checkout.onepage.paypal-smart-button');
});
}
}

View File

@ -16,5 +16,33 @@ class PaypalServiceProvider extends ServiceProvider
include __DIR__ . '/../Http/routes.php';
$this->loadViewsFrom(__DIR__ . '/../Resources/views', 'paypal');
$this->app->register(EventServiceProvider::class);
}
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->registerConfig();
}
/**
* Register package config.
*
* @return void
*/
protected function registerConfig()
{
$this->mergeConfigFrom(
dirname(__DIR__) . '/Config/paymentmethods.php', 'paymentmethods'
);
$this->mergeConfigFrom(
dirname(__DIR__) . '/Config/system.php', 'core'
);
}
}

View File

@ -0,0 +1,52 @@
<script src="https://www.paypal.com/sdk/js?client-id={{core()->getConfigData('sales.paymentmethods.paypal_smart_button.client_id')}}"></script>
<script>
eventBus.$on('after-payment-method-selected', function(payment) {
if (payment.method != 'paypal_smart_button') {
$('.paypal-buttons').remove();
return;
}
var options = {
style: {
layout: 'vertical',
shape: 'rect',
},
enableStandardCardFields: false,
createOrder: function(data, actions) {
return window.axios.get("{{ route('paypal.smart_button.details') }}")
.then(function(response) {
return actions.order.create(response.data);
})
.catch(function (error) {})
},
// Finalize the transaction
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
return window.axios.post("{{ route('paypal.smart_button.save_order') }}", {
'_token': "{{ csrf_token() }}",
'data' : details
})
.then(function(response) {
if (response.data.success) {
if (response.data.redirect_url) {
window.location.href = response.data.redirect_url;
} else {
window.location.href = "{{ route('shop.checkout.success') }}";
}
}
})
.catch(function (error) {
window.location.href = "{{ route('shop.checkout.cart.index') }}";
})
});
}
};
paypal.Buttons(options).render(".paypal-button-container");
});
</script>

View File

@ -88,9 +88,11 @@
</review-section>
<div class="button-group">
<button type="button" class="btn btn-lg btn-primary" @click="placeOrder()" :disabled="disable_button" id="checkout-place-order-button">
<button type="button" class="btn btn-lg btn-primary" @click="placeOrder()" :disabled="disable_button" id="checkout-place-order-button" v-if="selected_payment_method.method != 'paypal_smart_button'">
{{ __('shop::app.checkout.onepage.place-order') }}
</button>
<div class="paypal-button-container"></div>
</div>
</div>
</div>
@ -482,7 +484,7 @@
methodSelected: function() {
this.$emit('onShippingMethodSelected', this.selected_shipping_method)
eventBus.$emit('after-shipping-method-selected');
eventBus.$emit('after-shipping-method-selected', this.selected_shipping_method);
}
}
})
@ -535,7 +537,7 @@
methodSelected: function() {
this.$emit('onPaymentMethodSelected', this.payment)
eventBus.$emit('after-payment-method-selected');
eventBus.$emit('after-payment-method-selected', this.payment);
}
}
})

View File

@ -97,15 +97,14 @@
@elseif ($warning = session('warning'))
window.flashMessages = [{'type': 'alert-warning', 'message': "{{ $warning }}" }];
@elseif ($error = session('error'))
window.flashMessages = [{'type': 'alert-error', 'message': "{{ $error }}" }
];
window.flashMessages = [{'type': 'alert-error', 'message': "{{ $error }}" }];
@elseif ($info = session('info'))
window.flashMessages = [{'type': 'alert-info', 'message': "{{ $info }}" }
];
window.flashMessages = [{'type': 'alert-info', 'message': "{{ $info }}" }];
@endif
window.serverErrors = [];
@if(isset($errors))
@if (isset($errors))
@if (count($errors))
window.serverErrors = @json($errors->getMessages());
@endif

View File

@ -67,6 +67,7 @@
class="theme-btn"
@click="placeOrder()"
:disabled="!isPlaceOrderEnabled"
v-if="selected_payment_method.method != 'paypal_smart_button'"
id="checkout-place-order-button">
{{ __('shop::app.checkout.onepage.place-order') }}
</button>
@ -78,6 +79,8 @@
<div class="col-lg-4 col-md-12 offset-lg-1 order-summary-container top pt0">
<summary-section :key="summeryComponentKey"></summary-section>
<div class="paypal-button-container mt10"></div>
</div>
</div>
</div>
@ -584,7 +587,7 @@
this.$emit('onShippingMethodSelected', this.selected_shipping_method)
eventBus.$emit('after-shipping-method-selected');
eventBus.$emit('after-shipping-method-selected', this.selected_shipping_method);
}
}
})
@ -630,7 +633,7 @@
this.$emit('onPaymentMethodSelected', this.payment)
eventBus.$emit('after-payment-method-selected');
eventBus.$emit('after-payment-method-selected', this.payment);
}
}
})