Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
depsimon committed Aug 27, 2018
0 parents commit 6b10087
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/dist
/.idea
/vendor
/node_modules
package-lock.json
composer.phar
composer.lock
phpunit.xml
.phpunit.result.cache
.DS_Store
Thumbs.db
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "depsimon/nova-stripe-payment-field",
"description": "A Laravel Nova field that shows a Stripe Checkout Button.",
"keywords": [
"laravel",
"nova"
],
"license": "MIT",
"require": {
"php": ">=7.1.0"
},
"autoload": {
"psr-4": {
"Depsimon\\NovaStripePaymentField\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Depsimon\\NovaStripePaymentField\\FieldServiceProvider"
]
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
4 changes: 4 additions & 0 deletions mix-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"/dist/js/field.js": "/dist/js/field.js",
"/dist/css/field.css": "/dist/css/field.css"
}
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"cross-env": "^5.0.0",
"laravel-mix": "^1.0",
"laravel-nova": "^1.0"
},
"dependencies": {
"vue": "^2.5.0"
}
}
56 changes: 56 additions & 0 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<template>
<default-field :field="field">
<template slot="field">
<button v-if="! value" @click.prevent="checkout" class="btn btn-default btn-primary">
<span v-html="field.label"></span>
</button>
<button v-else @click.prevent="cancel" class="btn btn-default btn-danger flex items-center justify-center">
<icon type="delete" class="text-white" style="margin-right: 7px" />
<span v-html="field.cancelLabel"></span></button>
</template>
</default-field>
</template>

<script>
import { FormField } from 'laravel-nova'
export default {
mixins: [FormField],
props: ['resourceName', 'resourceId', 'field'],
data () {
return {
$checkout: null
}
},
mounted () {
this.initStripe()
},
methods: {
cancel() {
this.handleChange(null)
},
initStripe() {
this.$checkout = StripeCheckout.configure({
key: this.field.key,
locale: this.field.locale,
name: this.field.company,
description: this.field.description,
amount: this.field.amount,
currency: this.field.currency,
token: (token) => {
this.handleChange(token.id)
}
})
},
checkout() {
this.$checkout.open()
},
}
}
</script>
7 changes: 7 additions & 0 deletions resources/js/field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Nova.booting((Vue, router) => {
Vue.component('form-nova-stripe-payment-field', require('./components/FormField'));

const script = document.createElement('script');
script.src = 'https://checkout.stripe.com/checkout.js';
document.getElementsByTagName('head')[0].appendChild(script);
})
1 change: 1 addition & 0 deletions resources/sass/field.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Nova Tool CSS
33 changes: 33 additions & 0 deletions src/FieldServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Depsimon\NovaStripePaymentField;

use Laravel\Nova\Nova;
use Laravel\Nova\Events\ServingNova;
use Illuminate\Support\ServiceProvider;

class FieldServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Nova::serving(function (ServingNova $event) {
Nova::script('nova-stripe-payment-field', __DIR__.'/../dist/js/field.js');
Nova::style('nova-stripe-payment-field', __DIR__.'/../dist/css/field.css');
});
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
136 changes: 136 additions & 0 deletions src/NovaStripePaymentField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace Depsimon\NovaStripePaymentField;

use Laravel\Nova\Fields\Field;

class NovaStripePaymentField extends Field
{
/**
* The field's component.
*
* @var string
*/
public $component = 'nova-stripe-payment-field';

/**
* Create a new field.
*
* @param string $name
* @param string|null $attribute
* @param mixed|null $resolveCallback
* @return void
*/
public function __construct($name, $attribute = null, $resolveCallback = null)
{
parent::__construct($name, $attribute, $resolveCallback);

$this->key(config('services.stripe.key'))
->company(config('app.name'))
->locale('auto')
->label('Checkout')
->cancelLabel('Cancel');
}

/**
* Set the Stripe API key.
*
* @param string $key
* @return $this
*/
public function key($key)
{
return $this->withMeta(['key' => $key]);
}

/**
* Set the relative or absolute URL pointing to a square image of your brand or product.
* The recommended minimum size is 128x128px.The supported image types are: .gif, .jpeg, and .png.
*
* @param string $image
* @return $this
*/
public function image($image)
{
return $this->withMeta(['image' => $image]);
}

/**
* Set the name of your company or website.
*
* @param string $company
* @return $this
*/
public function company($company)
{
return $this->withMeta(['company' => $company]);
}

/**
* Set the description of the product or service being purchased.
*
* @param string $description
* @return $this
*/
public function description($description)
{
return $this->withMeta(['description' => $description]);
}

/**
* Set the amount (in cents) that's shown to the user.
* Note that you will still have to explicitly include the amount when you create a charge using the API.
* (You will also need to provide Checkout with a data-currency value to change the default of USD.)
*
* @param string $amount
* @return $this
*/
public function amount($amount)
{
return $this->withMeta(['amount' => $amount]);
}

/**
* Set the currency.
*
* @param string $currency
* @return $this
*/
public function currency($currency)
{
return $this->withMeta(['currency' => $currency]);
}

/**
* Set the locale.
*
* @param string $locale
* @return $this
*/
public function locale($locale)
{
return $this->withMeta(['locale' => $locale]);
}

/**
* Set the label of the checkout button.
*
* @param string $label
* @return $this
*/
public function label($label)
{
return $this->withMeta(['label' => $label]);
}

/**
* Set the label of the cancel button.
*
* @param string $cancelLabel
* @return $this
*/
public function cancelLabel($cancelLabel)
{
return $this->withMeta(['cancelLabel' => $cancelLabel]);
}
}
9 changes: 9 additions & 0 deletions webpack.mix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let mix = require('laravel-mix')

mix.js('resources/js/field.js', 'dist/js')
.sass('resources/sass/field.scss', 'dist/css')
.webpackConfig({
resolve: {
symlinks: false
}
})

0 comments on commit 6b10087

Please sign in to comment.