Skip to content

Instantly share code, notes, and snippets.

@joedooley
Last active June 14, 2019 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joedooley/e5324ac7d02f68b236fcdd38e56520a1 to your computer and use it in GitHub Desktop.
Save joedooley/e5324ac7d02f68b236fcdd38e56520a1 to your computer and use it in GitHub Desktop.
Append a dynamic_field_map field to Gravity Forms payment plugin feed.
<?php
namespace DevDesigns\DevDesignsStripeAchPlaid;
use GFForms;
use GFPaymentAddOn;
use DevDesigns\DevDesignsStripeAchPlaid\Updater\Licensing;
/**
* Include Gravity Forms Payment Add-on Framework
*
* @since 0.0.1
*/
GFForms::include_payment_addon_framework();
/**
* Class DevDesignsStripeAchPlaid()
*
* Primary class to manage the Stripe ACH add-on. Extends
* GFPaymentAddOn() class
*
* @since 0.0.1
*
* @uses GFPaymentAddOn()
*/
class DevDesignsStripeAchPlaid extends GFPaymentAddOn {
/**
* Prepare metadata fields for Product feeds
*
* @return array
* @uses GFAddOn::get_setting()
*
* @used-by DevDesignsStripeAchPlaid::feed_settings_fields()
*
* @since 0.0.1
*
* @used-by DevDesignsStripeAchPlaid::feed_settings_fields()
*/
protected function custom_metadata_fields(): array {
// Define end of Metadata tooltip based on transaction type.
if ( 'subscription' === $this->get_setting( 'transactionType' ) ) {
$info = esc_html__( 'You will see this data when viewing a customer page.', 'gintegrations-stripe-achplaid' );
}
else {
$info = esc_html__( 'You will see this data when viewing a payment page.', 'gintegrations-stripe-achplaid' );
}
// Prepare meta data field.
return [
[
'name' => 'metaData',
'label' => esc_html__( 'Metadata', 'gintegrations-stripe-achplaid' ),
'type' => 'dynamic_field_map',
'limit' => 20,
'exclude_field_types' => 'creditcard',
'tooltip' => '<h6>' . esc_html__( 'Metadata', 'gintegrations-stripe-achplaid' ) . '</h6>'
.
esc_html__( 'You may send custom meta information to Stripe. A maximum of 20 custom keys may be sent. The key name must be 40 characters or less, and the mapped data will be truncated to 500 characters per requirements by Stripe. '
. $info, 'gintegrations-stripe-achplaid' ),
'validation_callback' => [ $this, 'validate_custom_meta' ],
],
];
}
/**
* Validate the custom_meta type field.
*
* @param array $field The field properties. Not used.
*
* @return void
* @uses GFAddOn::set_field_error()
*
* @since 0.0.1
*
* @uses GFAddOn::get_posted_settings()
*/
public function validate_custom_meta( $field ) {
/*
* Number of keys is limited to 20.
* Interface should control this, validating just in case.
* Key names have maximum length of 40 characters.
*/
// Get metadata from posted settings.
$settings = $this->get_posted_settings();
$meta_data = $settings['metaData'];
// If metadata is not defined, return.
if ( empty( $meta_data ) ) {
return;
}
// Get number of metadata items.
$meta_count = count( $meta_data );
// If there are more than 20 metadata keys, set field error.
if ( $meta_count > 20 ) {
$this->set_field_error( [
esc_html__( 'You may only have 20 custom keys.' ),
'gintegrations-stripe-achplaid',
] );
return;
}
// Loop through metadata and check the key name length (custom_key).
foreach ( $meta_data as $meta ) {
if ( empty( $meta['custom_key'] ) && ! empty( $meta['value'] ) ) {
$this->set_field_error( [ 'name' => 'metaData' ], esc_html__( "A field has been mapped to a custom key without a name. Please enter a name for the custom key, remove the metadata item, or return the corresponding drop down to 'Select a Field'.", 'gintegrations-stripe-achplaid' ) );
break;
}
else {
if ( strlen( $meta['custom_key'] ) > 40 ) {
$this->set_field_error( [ 'name' => 'metaData' ], sprintf( esc_html__( 'The name of custom key %s is too long. Please shorten this to 40 characters or less.', 'gintegrations-stripe-achplaid' ), $meta['custom_key'] ) );
break;
}
}
}
}
/**
* Configures the settings which should be rendered on the feed edit page.
*
* @return array The feed settings.
* @since 0.0.1
*
* @uses GFPaymentAddOn::feed_settings_fields()
* @uses DevDesignsStripeAchPlaid::add_additional_transaction_types()
* @uses DevDesignsStripeAchPlaid::product_customer_info_fields()
* @uses DevDesignsStripeAchPlaid::subscription_customer_info_fields()
* @uses DevDesignsStripeAchPlaid::custom_metadata_fields()
* @uses DevDesignsStripeAchPlaid::trial_period_fields()
* @uses DevDesignsStripeAchPlaid::receipt_fields()
* @uses GFAddOn::replace_field()
* @uses GFAddOn::get_setting()
* @uses GFAddOn::add_field_after()
* @uses GFAddOn::remove_field()
* @uses GFAddOn::add_field_before()
*
*/
public function feed_settings_fields(): array {
// Get default payment feed settings fields.
$default_settings = parent::feed_settings_fields();
// Add meta data field.
$default_settings = $this->add_field_after( 'customerInformation', $this->custom_metadata_fields(), $default_settings );
return $default_settings;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment