This commit is contained in:
2026-07-02 15:54:39 -06:00
commit 9883323161
17470 changed files with 4470592 additions and 0 deletions
@@ -0,0 +1,247 @@
/**
* This PaypalExpressCheckout global is included by wp_enqueue_script( 'jetpack-paypal-express-checkout' );
* It handles communication with Paypal Express checkout and public-api.wordpress.com for the purposes
* of simple-payments module.
*/
/* global paypal */
/* exported PaypalExpressCheckout */
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',
shape: 'rect',
color: 'silver',
size: 'responsive',
fundingicons: true,
},
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
);
},
};
@@ -0,0 +1,172 @@
.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 {
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: 700;
}
.jetpack-simple-payments-purchase-box {
align-items: flex-start;
display: flex;
}
.jetpack-simple-payments-button {
max-width: 340px;
width: 100%;
}
.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;
}
input[type="number"].jetpack-simple-payments-items-number::-webkit-inner-spin-button,
input[type="number"].jetpack-simple-payments-items-number::-webkit-outer-spin-button {
opacity: 1;
}
.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;
}
.jetpack-simple-payments-description {
white-space: pre-wrap;
}
@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;
}
}
.is-email .jetpack-simple-payments-product {
display: table;
width: 100%;
}
.is-email .jetpack-simple-payments-product-image {
display: table-cell;
width: 30%;
vertical-align: top;
}
.is-email .jetpack-simple-payments-image {
padding-top: 0;
}
.is-email .jetpack-simple-payments-image figure {
margin: 0;
}
.jetpack-simple-payments-details {
width: 100%;
}
.is-email .jetpack-simple-payments-product-image + .jetpack-simple-payments-details {
display: table-cell;
width: 70%;
}
@@ -0,0 +1,273 @@
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* 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.
*
* @package automattic/jetpack
*/
use Automattic\Jetpack\Paypal_Payments\Simple_Payments as PayPal_Simple_Payments;
if ( ! defined( 'ABSPATH' ) ) {
exit( 0 );
}
/**
* Jetpack_Simple_Payments
*/
class Jetpack_Simple_Payments {
// These have to be under 20 chars because that is CPT limit.
/**
* Post type order.
*
* @var string
*/
public static $post_type_order = 'jp_pay_order';
/**
* Post type product.
*
* @var string
*/
public static $post_type_product = 'jp_pay_product';
/**
* Define simple payment shortcode.
*
* @var string
*/
public static $shortcode = 'simple-payment';
/**
* Define simple payment CSS prefix.
*
* @var string
*/
public static $css_classname_prefix = 'jetpack-simple-payments';
/**
* Which plan the user is on.
*
* @var string value_bundle or jetpack_premium
*/
public static $required_plan;
/**
* Instance of the class.
*
* @var Jetpack_Simple_Payments
*/
private static $instance;
/**
* Construction function.
*/
private function __construct() {}
/**
* Original singleton.
*
* @todo Remove this when nothing calles getInstance anymore.
*/
public static function getInstance() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
return self::get_instance();
}
/**
* Create instance of class.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
self::$instance->init_hook_action();
self::$required_plan = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? 'value_bundle' : 'jetpack_premium';
}
return self::$instance;
}
/**
* Actions that are run on init.
*/
public function init_hook_action() {
return PayPal_Simple_Payments::get_instance()->init_hook_action();
}
/**
* Enqueue the static assets needed in the frontend.
*/
public function enqueue_frontend_assets() {
return PayPal_Simple_Payments::get_instance()->enqueue_frontend_assets();
}
/**
* Add an inline script for setting up the PayPal checkout button.
*
* @param int $id Product ID.
* @param int $dom_id ID of the DOM element with the purchase message.
* @param boolean $is_multiple Whether multiple items of the same product can be purchased.
*/
public function setup_paypal_checkout_button( $id, $dom_id, $is_multiple ) {
return PayPal_Simple_Payments::get_instance()->setup_paypal_checkout_button( $id, $dom_id, $is_multiple );
}
/**
* Remove auto paragraph from product description.
*
* @param string $content - the content of the post.
*/
public function remove_auto_paragraph_from_product_description( $content ) {
return PayPal_Simple_Payments::get_instance()->remove_auto_paragraph_from_product_description( $content );
}
/** Return the blog ID */
public function get_blog_id() {
return PayPal_Simple_Payments::get_instance()->get_blog_id();
}
/**
* Used to check whether Simple Payments are enabled for given site.
*
* @return bool True if Simple Payments are enabled, false otherwise.
*/
public function is_enabled_jetpack_simple_payments() {
return PayPal_Simple_Payments::is_enabled_jetpack_simple_payments();
}
/**
* Creates the content from a shortcode
*
* @param array $attrs Shortcode attributes.
* @param mixed $content unused.
*
* @return string|void
*/
public function parse_shortcode( $attrs, $content = false ) {
return PayPal_Simple_Payments::get_instance()->parse_shortcode( $attrs, $content );
}
/**
* Output an admin warning if user can't use Pay with PayPal.
*
* @param array $data unused.
*/
public function output_admin_warning( $data ) {
return PayPal_Simple_Payments::get_instance()->output_admin_warning( $data );
}
/**
* Get the HTML output to use as PayPal purchase box.
*
* @param string $dom_id ID of the DOM element with the purchase message.
* @param boolean $is_multiple Whether multiple items of the same product can be purchased.
*
* @return string
*/
public function output_purchase_box( $dom_id, $is_multiple ) {
return PayPal_Simple_Payments::get_instance()->output_purchase_box( $dom_id, $is_multiple );
}
/**
* Get the HTML output to replace the `simple-payments` shortcode.
*
* @param array $data Product data.
* @return string
*/
public function output_shortcode( $data ) {
return PayPal_Simple_Payments::get_instance()->output_shortcode( $data );
}
/**
* Allows custom post types to be used by REST API.
*
* @param array $post_types - the allows post types.
* @see hook 'rest_api_allowed_post_types'
* @return array
*/
public function allow_rest_api_types( $post_types ) {
return PayPal_Simple_Payments::get_instance()->allow_rest_api_types( $post_types );
}
/**
* Merge $post_meta with additional meta information.
*
* @param array $post_meta - the post's meta information.
*/
public function allow_sync_post_meta( $post_meta ) {
return PayPal_Simple_Payments::get_instance()->allow_sync_post_meta( $post_meta );
}
/**
* Enable Simple payments custom meta values for access through the REST API.
* Field's value will be exposed on a .meta key in the endpoint response,
* and WordPress will handle setting up the callbacks for reading and writing
* to that meta key.
*
* @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/
*/
public function register_meta_fields_in_rest_api() {
return PayPal_Simple_Payments::get_instance()->register_meta_fields_in_rest_api();
}
/**
* Sanitize three-character ISO-4217 Simple payments currency
*
* List has to be in sync with list at the block's client side and widget's backend side:
*
* @param array $currency - list of currencies.
* @link https://github.com/Automattic/jetpack/blob/31efa189ad223c0eb7ad085ac0650a23facf9ef5/extensions/blocks/simple-payments/constants.js#L9-L39
* @link https://github.com/Automattic/jetpack/blob/31efa189ad223c0eb7ad085ac0650a23facf9ef5/modules/widgets/simple-payments.php#L19-L44
*
* Currencies should be supported by PayPal:
* @link https://developer.paypal.com/docs/api/reference/currency-codes/
*
* Indian Rupee (INR) not supported because at the time of the creation of this file
* because it's limited to in-country PayPal India accounts only.
* Discussion: https://github.com/Automattic/wp-calypso/pull/28236
*/
public static function sanitize_currency( $currency ) {
return PayPal_Simple_Payments::sanitize_currency( $currency );
}
/**
* Sanitize price:
*
* Positive integers and floats
* Supports two decimal places.
* Maximum length: 10.
*
* See `price` from PayPal docs:
*
* @link https://developer.paypal.com/docs/api/orders/v1/#definition-item
*
* @param string $price - the price we want to sanitize.
* @return null|string
*/
public static function sanitize_price( $price ) {
return PayPal_Simple_Payments::sanitize_price( $price );
}
/**
* Sets up the custom post types for the module.
*/
public function setup_cpts() {
return PayPal_Simple_Payments::get_instance()->setup_cpts();
}
/**
* Validate the block attributes
*
* @param array $attrs The block attributes, expected to contain:
* * email - an email address.
* * price - a float between 0.01 and 9999999999.99.
* * productId - the ID of the product being paid for.
*
* @return bool
*/
public function is_valid( $attrs ) {
return PayPal_Simple_Payments::get_instance()->is_valid( $attrs );
}
}
PayPal_Simple_Payments::get_instance();