Added login request

This commit is contained in:
Nedim Uka
2018-06-20 18:03:43 +02:00
parent 4e52521fae
commit 593b445a21
4716 changed files with 1218265 additions and 57 deletions

View File

@@ -0,0 +1,212 @@
/**
* This PaypalExpressCheckout global is included by wp_enqueue_script( 'paypal-express-checkout' );
* It handles communication with Paypal Express checkout and public-api.wordpress.com for the purposes
* of simple-payments module.
*/
/* global paypal */
/* global jQuery */
/* exported PaypalExpressCheckout */
/* jshint unused:false, es3:false, esversion:5 */
var PaypalExpressCheckout = {
primaryCssClassName: 'jetpack-simple-payments',
messageCssClassName: 'jetpack-simple-payments-purchase-message',
wpRestAPIHost: 'https://public-api.wordpress.com',
wpRestAPIVersion: '/wpcom/v2',
getEnvironment: function() {
if ( localStorage && localStorage.getItem && localStorage.getItem( 'simple-payments-env' ) === 'sandbox' ) {
return 'sandbox';
}
return 'production';
},
getCreatePaymentEndpoint: function( blogId ) {
return PaypalExpressCheckout.wpRestAPIHost + PaypalExpressCheckout.wpRestAPIVersion + '/sites/' + blogId + '/simple-payments/paypal/payment';
},
getExecutePaymentEndpoint: function( blogId, paymentId ) {
return PaypalExpressCheckout.wpRestAPIHost + PaypalExpressCheckout.wpRestAPIVersion + '/sites/' + blogId + '/simple-payments/paypal/' + paymentId + '/execute';
},
getNumberOfItems: function( field, enableMultiple ) {
if ( enableMultiple !== '1' ) {
return 1;
}
var numberField = document.getElementById( field );
if ( ! numberField ) {
return 1;
}
var number = Number( numberField.value );
if ( isNaN( number ) ) {
return 1;
}
return number;
},
/**
* Get the DOM element-placeholder used to show message
* about the transaction. If it doesn't exist then the function will create a new one.
*
* @param string domId id of the payment button placeholder
* @return Element the dom element to print the message
*/
getMessageContainer: function( domId ) {
return document.getElementById( domId + '-message-container' );
},
/**
* Show a messange close to the Paypal button.
* Use this function to give feedback to the user according
* to the transaction result.
*
* @param {String} message message to show
* @param {String} domId paypal-button element dom identifier
* @param {Boolean} [error] defines if it's a message error. Not TRUE as default.
*/
showMessage: function( message, domId, isError ) {
var domEl = PaypalExpressCheckout.getMessageContainer( domId );
// set css classes
var cssClasses = PaypalExpressCheckout.messageCssClassName + ' show ';
cssClasses += isError ? 'error' : 'success';
// show message 1s after Paypal popup is closed
setTimeout( function() {
domEl.innerHTML = message;
domEl.setAttribute( 'class', cssClasses );
}, 1000 );
},
showError: function( message, domId ) {
PaypalExpressCheckout.showMessage( message, domId, true );
},
processErrorMessage: function( errorResponse ) {
var error = errorResponse ? errorResponse.responseJSON : null;
var defaultMessage = 'There was an issue processing your payment.';
if ( ! error ) {
return '<p>' + defaultMessage + '</p>';
}
if ( error.additional_errors ) {
var messages = [];
error.additional_errors.forEach( function( additionalError ) {
if ( additionalError.message ) {
messages.push( '<p>' + additionalError.message.toString() + '</p>' );
}
} );
return messages.join('');
}
return '<p>' + ( error.message || defaultMessage ) + '</p>';
},
processSuccessMessage: function( successResponse ) {
var message = successResponse.message;
var defaultMessage = 'Thank you. Your purchase was successful!';
if ( ! message ) {
return '<p>' + defaultMessage + '</p>';
}
return '<p>' + message + '</p>';
},
cleanAndHideMessage: function( domId ) {
var domEl = PaypalExpressCheckout.getMessageContainer( domId );
domEl.setAttribute( 'class', PaypalExpressCheckout.messageCssClassName );
domEl.innerHTML = '';
},
renderButton: function( blogId, buttonId, domId, enableMultiple ) {
var env = PaypalExpressCheckout.getEnvironment();
if ( ! paypal ) {
throw new Error( 'PayPal module is required by PaypalExpressCheckout' );
}
var buttonDomId = domId + '_button';
paypal.Button.render( {
env: env,
commit: true,
style: {
label: 'pay',
fundingicons: true,
shape: 'rect',
color: 'silver'
},
payment: function() {
PaypalExpressCheckout.cleanAndHideMessage( domId );
var payload = {
number: PaypalExpressCheckout.getNumberOfItems( domId + '_number', enableMultiple ),
buttonId: buttonId,
env: env
};
return new paypal.Promise( function( resolve, reject ) {
jQuery.post( PaypalExpressCheckout.getCreatePaymentEndpoint( blogId ), payload )
.done( function( paymentResponse ) {
if ( ! paymentResponse ) {
PaypalExpressCheckout.showError( PaypalExpressCheckout.processErrorMessage(), domId );
return reject( new Error( 'server_error' ) );
}
resolve( paymentResponse.id );
} )
.fail( function( paymentError ) {
var paymentErrorMessage = PaypalExpressCheckout.processErrorMessage( paymentError );
PaypalExpressCheckout.showError( paymentErrorMessage, domId );
var code = paymentError.responseJSON && paymentError.responseJSON.code ?
paymentError.responseJSON.code : 'server_error';
reject( new Error( code ) );
} );
} );
},
onAuthorize: function( onAuthData ) {
var payload = {
buttonId: buttonId,
payerId: onAuthData.payerID,
env: env
};
return new paypal.Promise( function( resolve, reject ) {
jQuery.post( PaypalExpressCheckout.getExecutePaymentEndpoint( blogId, onAuthData.paymentID ), payload )
.done( function( authResponse ) {
if ( ! authResponse ) {
PaypalExpressCheckout.showError( PaypalExpressCheckout.processErrorMessage(), domId );
return reject( new Error( 'server_error' ) );
}
PaypalExpressCheckout.showMessage(
PaypalExpressCheckout.processSuccessMessage( authResponse ),
domId
);
resolve();
} )
.fail( function( authError ) {
var authErrorMessage = PaypalExpressCheckout.processErrorMessage( authError );
PaypalExpressCheckout.showError( authErrorMessage, domId );
var code = authError.responseJSON && authError.responseJSON.code ?
authError.responseJSON.code : 'server_error';
reject( new Error( code ) );
} );
} );
}
}, buttonDomId );
}
};

View File

@@ -0,0 +1,129 @@
.jetpack-simple-payments-wrapper {
margin-bottom: 1.5em;
}
/* Higher specificity in order to reset paragraph style */
body .jetpack-simple-payments-wrapper .jetpack-simple-payments-details p {
margin: 0 0 1.5em;
padding: 0;
}
.jetpack-simple-payments-product {
display: flex;
flex-direction: column;
}
.jetpack-simple-payments-product-image {
flex: 0 0 30%;
margin-bottom: 1.5em;
}
.jetpack-simple-payments-image {
box-sizing: border-box;
min-width: 70px;
padding-top: 100%;
position: relative;
}
/* Higher specificity in order to trump theme's style */
body .jetpack-simple-payments-wrapper .jetpack-simple-payments-product-image .jetpack-simple-payments-image img.size-full {
border: 0;
border-radius: 0;
height: auto;
left: 50%;
margin: 0;
max-height: 100%;
max-width: 100%;
padding: 0;
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
width: auto;
}
.jetpack-simple-payments-title p,
.jetpack-simple-payments-price p {
font-weight: bold;
}
.jetpack-simple-payments-purchase-box {
align-items: flex-start;
display: flex;
}
.jetpack-simple-payments-items {
flex: 0 0 auto;
margin-right: 10px;
}
input[type="number"].jetpack-simple-payments-items-number {
font-size: 16px;
line-height: 1;
max-width: 60px;
padding: 4px 8px;
}
.jetpack-simple-payments-button iframe {
margin: 0;
}
.jetpack-simple-payments-purchase-message {
background-color: rgba(255, 255, 255, 0.7);
border: 2px solid #fff;
border-radius: 2px;
box-shadow: 0 0 0 1px rgba(200, 215, 225, 0.5), 0 1px 2px #e9eff3;
display: none;
margin-bottom: 1.5em;
min-height: 48px;
padding: 1em;
position: relative;
}
.jetpack-simple-payments-purchase-message:before {
font-family: dashicons !important;
font-size: 48px !important;
line-height: 1 !important;
position: absolute;
speak: none;
top: 50%;
left: 0;
transform: translateY(-50%);
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.jetpack-simple-payments-purchase-message.show {
display: block;
}
.jetpack-simple-payments-purchase-message.success:before {
color: #4ab866;
content: "\f147";
}
.jetpack-simple-payments-purchase-message.error:before {
color: #d94f4f;
content: "\f335";
}
/* Higher specificity in order to reset */
body .jetpack-simple-payments-wrapper .jetpack-simple-payments-purchase-message p {
color: #222;
margin: 0 0 0.5em;
padding: 0 0 0 40px;
}
body .jetpack-simple-payments-wrapper .jetpack-simple-payments-purchase-message p:last-child {
margin: 0;
}
@media screen and (min-width: 400px) {
.jetpack-simple-payments-product {
flex-direction: row;
}
.jetpack-simple-payments-product-image + .jetpack-simple-payments-details {
flex-basis: 70%;
padding-left: 1em;
}
}

View File

@@ -0,0 +1,280 @@
<?php
/*
* Simple Payments lets users embed a PayPal button fully integrated with wpcom to sell products on the site.
* This is not a proper module yet, because not all the pieces are in place. Until everything is shipped, it can be turned
* into module that can be enabled/disabled.
*/
class Jetpack_Simple_Payments {
// These have to be under 20 chars because that is CPT limit.
static $post_type_order = 'jp_pay_order';
static $post_type_product = 'jp_pay_product';
static $shortcode = 'simple-payment';
static $css_classname_prefix = 'jetpack-simple-payments';
// Increase this number each time there's a change in CSS or JS to bust cache.
static $version = '0.25';
// Classic singleton pattern:
private static $instance;
private function __construct() {}
static function getInstance() {
if ( ! self::$instance ) {
self::$instance = new self();
self::$instance->register_init_hook();
}
return self::$instance;
}
private function register_scripts() {
/**
* Paypal heavily discourages putting that script in your own server:
* @see https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/add-paypal-button/
*/
wp_register_script( 'paypal-checkout-js', 'https://www.paypalobjects.com/api/checkout.js', array(), null, true );
wp_register_script( 'paypal-express-checkout', plugins_url( '/paypal-express-checkout.js', __FILE__ ),
array( 'jquery', 'paypal-checkout-js' ), self::$version );
}
private function register_init_hook() {
add_action( 'init', array( $this, 'init_hook_action' ) );
}
private function register_shortcode() {
add_shortcode( self::$shortcode, array( $this, 'parse_shortcode' ) );
}
public function init_hook_action() {
add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) );
add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'allow_sync_post_meta' ) );
$this->register_scripts();
$this->register_shortcode();
$this->setup_cpts();
add_filter( 'the_content', array( $this, 'remove_auto_paragraph_from_product_description' ), 0 );
}
function remove_auto_paragraph_from_product_description( $content ) {
if ( get_post_type() === self::$post_type_product ) {
remove_filter( 'the_content', 'wpautop' );
}
return $content;
}
function get_blog_id() {
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
return get_current_blog_id();
}
return Jetpack_Options::get_option( 'id' );
}
function parse_shortcode( $attrs, $content = false ) {
if ( empty( $attrs['id'] ) ) {
return;
}
$product = get_post( $attrs['id'] );
if ( ! $product || is_wp_error( $product ) ) {
return;
}
if ( $product->post_type !== self::$post_type_product || 'trash' === $product->post_status ) {
return;
}
// We allow for overriding the presentation labels
$data = shortcode_atts( array(
'blog_id' => $this->get_blog_id(),
'dom_id' => uniqid( self::$css_classname_prefix . '-' . $product->ID . '_', true ),
'class' => self::$css_classname_prefix . '-' . $product->ID,
'title' => get_the_title( $product ),
'description' => $product->post_content,
'cta' => get_post_meta( $product->ID, 'spay_cta', true ),
'multiple' => get_post_meta( $product->ID, 'spay_multiple', true ) || '0'
), $attrs );
$data['price'] = $this->format_price(
get_post_meta( $product->ID, 'spay_formatted_price', true ),
get_post_meta( $product->ID, 'spay_price', true ),
get_post_meta( $product->ID, 'spay_currency', true ),
$data
);
$data['id'] = $attrs['id'];
if ( ! wp_script_is( 'paypal-express-checkout', 'enqueued' ) ) {
wp_enqueue_script( 'paypal-express-checkout' );
}
if ( ! wp_style_is( 'simple-payments', 'enqueued' ) ) {
wp_enqueue_style( 'simple-payments', plugins_url( 'simple-payments.css', __FILE__ ), array( 'dashicons' ) );
}
wp_add_inline_script( 'paypal-express-checkout', sprintf(
"try{PaypalExpressCheckout.renderButton( '%d', '%d', '%s', '%d' );}catch(e){}",
esc_js( $data['blog_id'] ),
esc_js( $attrs['id'] ),
esc_js( $data['dom_id'] ),
esc_js( $data['multiple'] )
) );
return $this->output_shortcode( $data );
}
function output_shortcode( $data ) {
$items = '';
$css_prefix = self::$css_classname_prefix;
if ( $data['multiple'] ) {
$items="<div class='${css_prefix}-items'>
<input class='${css_prefix}-items-number' type='number' value='1' min='1' id='{$data['dom_id']}_number' />
</div>";
}
$image = "";
if( has_post_thumbnail( $data['id'] ) ) {
$image = "<div class='${css_prefix}-product-image'><div class='${css_prefix}-image'>" . get_the_post_thumbnail( $data['id'], 'full' ) . "</div></div>";
}
return "
<div class='{$data['class']} ${css_prefix}-wrapper'>
<div class='${css_prefix}-product'>
{$image}
<div class='${css_prefix}-details'>
<div class='${css_prefix}-title'><p>{$data['title']}</p></div>
<div class='${css_prefix}-description'><p>{$data['description']}</p></div>
<div class='${css_prefix}-price'><p>{$data['price']}</p></div>
<div class='${css_prefix}-purchase-message' id='{$data['dom_id']}-message-container'></div>
<div class='${css_prefix}-purchase-box'>
{$items}
<div class='${css_prefix}-button' id='{$data['dom_id']}_button'></div>
</div>
</div>
</div>
</div>
";
}
function format_price( $formatted_price, $price, $currency, $all_data ) {
if ( $formatted_price ) {
return $formatted_price;
}
return "$price $currency";
}
/**
* Allows custom post types to be used by REST API.
* @param $post_types
* @see hook 'rest_api_allowed_post_types'
* @return array
*/
function allow_rest_api_types( $post_types ) {
$post_types[] = self::$post_type_order;
$post_types[] = self::$post_type_product;
return $post_types;
}
function allow_sync_post_meta( $post_meta ) {
return array_merge( $post_meta, array(
'spay_paypal_id',
'spay_status',
'spay_product_id',
'spay_quantity',
'spay_price',
'spay_customer_email',
'spay_currency',
'spay_cta',
'spay_email',
'spay_multiple',
'spay_formatted_price',
) );
}
/**
* Sets up the custom post types for the module.
*/
function setup_cpts() {
/*
* ORDER data structure. holds:
* title = customer_name | 4xproduct_name
* excerpt = customer_name + customer contact info + customer notes from paypal form
* metadata:
* spay_paypal_id - paypal id of transaction
* spay_status
* spay_product_id - post_id of bought product
* spay_quantity - quantity of product
* spay_price - item price at the time of purchase
* spay_customer_email - customer email
* ... (WIP)
*/
$order_capabilities = array(
'edit_post' => 'edit_posts',
'read_post' => 'read_private_posts',
'delete_post' => 'delete_posts',
'edit_posts' => 'edit_posts',
'edit_others_posts' => 'edit_others_posts',
'publish_posts' => 'publish_posts',
'read_private_posts' => 'read_private_posts',
);
$order_args = array(
'label' => esc_html__( 'Order', 'jetpack' ),
'description' => esc_html__( 'Simple Payments orders', 'jetpack' ),
'supports' => array( 'custom-fields', 'excerpt' ),
'hierarchical' => false,
'public' => false,
'show_ui' => false,
'show_in_menu' => false,
'show_in_admin_bar' => false,
'show_in_nav_menus' => false,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'rewrite' => false,
'capabilities' => $order_capabilities,
'show_in_rest' => true,
);
register_post_type( self::$post_type_order, $order_args );
/*
* PRODUCT data structure. Holds:
* title - title
* content - description
* thumbnail - image
* metadata:
* spay_price - price
* spay_formatted_price
* spay_currency - currency code
* spay_cta - text with "Buy" or other CTA
* spay_email - paypal email
* spay_multiple - allow for multiple items
* spay_status - status. { enabled | disabled }
*/
$product_capabilities = array(
'edit_post' => 'edit_posts',
'read_post' => 'read_private_posts',
'delete_post' => 'delete_posts',
'edit_posts' => 'edit_posts',
'edit_others_posts' => 'edit_others_posts',
'publish_posts' => 'publish_posts',
'read_private_posts' => 'read_private_posts',
);
$product_args = array(
'label' => esc_html__( 'Product', 'jetpack' ),
'description' => esc_html__( 'Simple Payments products', 'jetpack' ),
'supports' => array( 'title', 'editor','thumbnail', 'custom-fields', 'author' ),
'hierarchical' => false,
'public' => false,
'show_ui' => false,
'show_in_menu' => false,
'show_in_admin_bar' => false,
'show_in_nav_menus' => false,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'rewrite' => false,
'capabilities' => $product_capabilities,
'show_in_rest' => true,
);
register_post_type( self::$post_type_product, $product_args );
}
}
Jetpack_Simple_Payments::getInstance();