Skip to content

Commit

Permalink
Merge pull request #17 from pronamic/16-no-sum-of-multiple-amount-fie…
Browse files Browse the repository at this point in the history
…ldstags-with-the-pronamic_pay_amount-option

Fix for issue #16
  • Loading branch information
remcotolsma authored Dec 18, 2023
2 parents 8e15330 + 76e371f commit 8f26fbd
Show file tree
Hide file tree
Showing 3 changed files with 288 additions and 112 deletions.
10 changes: 6 additions & 4 deletions .wp-env.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
{
"core": null,
"plugins": [
".",
"../../../",
"./tests/wp-env-mail.php",
"pronamic/wp-env-quick-login",
"pronamic/wp-pronamic-pay-test-helper",
"https://downloads.wordpress.org/plugin/contact-form-7.5.6.2.zip",
"https://downloads.wordpress.org/plugin/contact-form-7.zip",
"https://downloads.wordpress.org/plugin/cf7-conditional-fields.zip",
"https://downloads.wordpress.org/plugin/flamingo.zip",
"https://downloads.wordpress.org/plugin/pronamic-client.zip",
"https://downloads.wordpress.org/plugin/pronamic-ideal.zip",
"https://downloads.wordpress.org/plugin/query-monitor.zip",
"https://downloads.wordpress.org/plugin/one-time-login.zip",
"https://downloads.wordpress.org/plugin/wp-plugin-dependencies.zip"
],
"mappings": {
"wp-content/plugins/pronamic-ideal": "../../../",
"wp-content/plugins/pronamic-pay-contact-form-7": "."
},
"config": {
"PRONAMIC_PAY_DEBUG": true
}
Expand Down
143 changes: 35 additions & 108 deletions src/Pronamic.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Pronamic\WordPress\Pay\Core\PaymentMethods;
use Pronamic\WordPress\Pay\Plugin;
use Pronamic\WordPress\Money\Money;
use Pronamic\WordPress\Money\Parser;
use Pronamic\WordPress\Pay\Address;
use Pronamic\WordPress\Pay\Customer;
use Pronamic\WordPress\Pay\ContactName;
Expand Down Expand Up @@ -43,123 +44,52 @@ public static function get_default_gateway() {
}

/**
* Get submission value.
* Get Pronamic payment from Contact Form 7 form.
*
* @param WPCF7_Submission $submission Submission.
* @param string $type Type to search for.
* @return mixed
* @param WPCF7_Submission $submission Contact Form 7 form submission.
* @return Payment|null
*/
public static function get_submission_value( WPCF7_Submission $submission, $type ) {
// phpcs:disable WordPress.Security.NonceVerification.Missing
$result = null;

$prefixed_type = 'pronamic_pay_' . $type;
public static function get_submission_payment( WPCF7_Submission $submission ) {
$form = $submission->get_contact_form();

/**
* Hidden fields may arise when using the "Conditional Fields for Contact Form 7" plugin.
*
* @link https://wordpress.org/plugins/cf7-conditional-fields/
* @link https://github.com/pronamic/wp-pronamic-pay-contact-form-7/commit/83122efa3755f1d4b667aed3e3e7c2ae0f813faa
*/
$hidden_fields = \filter_input( \INPUT_POST, '_wpcf7cf_hidden_group_fields' );
// Gateway.
$gateway = self::get_default_gateway();

if ( ! empty( $hidden_fields ) ) {
$hidden_fields = \json_decode( stripslashes( $hidden_fields ) );
if ( null === $gateway ) {
return null;
}

if ( ! \is_array( $hidden_fields ) ) {
$hidden_fields = [];
}
$submission_helper = new SubmissionHelper( $submission );

// @link https://contactform7.com/tag-syntax/
$tags = WPCF7_FormTagsManager::get_instance()->get_scanned_tags();
// Total.
$total = new Money();
$parser = new Parser();

foreach ( $tags as $tag ) {
// Check if tag base type or name is requested type or tag has requested type as option.
if ( ! \in_array( $tag->basetype, [ $type, $prefixed_type ], true ) && ! $tag->has_option( $prefixed_type ) && $prefixed_type !== $tag->name ) {
continue;
}
$tags = $submission_helper->get_tags_with_basetype_or_name_or_option( 'pronamic_pay_amount' );

// Check if field is not hidden.
if ( \in_array( $tag->name, $hidden_fields, true ) ) {
continue;
}
foreach ( $tags as $tag ) {
$value = $submission_helper->get_value_by_tag( $tag );

// Submission value.
$value = $submission->get_posted_string( $tag->name );
try {
$amount = $parser->parse( $value );

if ( empty( $value ) ) {
$total = $total->add( $amount );
} catch ( \Exception $e ) {
continue;
}

switch ( $type ) {
case 'amount':
/**
* Contact Form 7 concatenates the field option value with user input for free text fields. We
* are only interested in the input value as amount.
*
* @link https://github.com/rocklobster-in/contact-form-7/blob/2cfaa472fa485c6d3366fcdd80701fdaf7f9e425/includes/submission.php#L434-L437
*/
if ( \wpcf7_form_tag_supports( $tag->type, 'selectable-values' ) && $tag->has_option( 'free_text' ) ) {
$values = \WPCF7_USE_PIPE ? $tag->pipes->collect_afters() : $tag->values;

$last_value = end( $values );

if ( \str_starts_with( $value, $last_value . ' ' ) ) {
$value = substr( $value, strlen( $last_value . ' ' ) );
}
}

$value = Tags\AmountTag::parse_value( $value );

// Set parsed value as result or add to existing money result.
if ( null !== $value ) {
$result = ( $result instanceof Money ? $result->add( $value ) : $value );
}

break;
default:
$result = $value;
}

// Prefer tag with option (`pronamic_pay_email`) over tag name match (e.g. `email`).
if ( $tag->has_option( $prefixed_type ) ) {
return $result;
}
}

// phpcs:enable WordPress.Security.NonceVerification.Missing

return $result;
}

/**
* Get Pronamic payment from Contact Form 7 form.
*
* @param WPCF7_Submission $submission Contact Form 7 form submission.
* @return Payment|null
*/
public static function get_submission_payment( WPCF7_Submission $submission ) {
$form = $submission->get_contact_form();

$payment = new Payment();

// Check amount.
$amount = self::get_submission_value( $submission, 'amount' );

if ( null === $amount ) {
if ( $total->is_zero() ) {
return null;
}

// Check gateway.
$gateway = self::get_default_gateway();
// Payment.
$payment = new Payment();

if ( null === $gateway ) {
return null;
}
$payment->set_total_amount( $total );

// Check active payment method.
$payment_method = self::get_submission_value( $submission, 'method' );
$payment_method = $submission_helper->get_value_by_tag_basetype_or_name_or_option( 'pronamic_pay_method' );

if ( ! empty( $payment_method ) ) {
if ( ! PaymentMethods::is_active( $payment_method ) ) {
Expand All @@ -186,9 +116,9 @@ public static function get_submission_payment( WPCF7_Submission $submission ) {
);

// Description.
$description = self::get_submission_value( $submission, 'description' );
$description = $submission_helper->get_value_by_tag_name_or_option( 'pronamic_pay_description' );

if ( null === $description ) {
if ( '' === $description ) {
$description = sprintf(
/* translators: %s: payment number */
__( 'Payment %s', 'pronamic_ideal' ),
Expand All @@ -197,7 +127,7 @@ public static function get_submission_payment( WPCF7_Submission $submission ) {
}

// Payment method.
$issuer = self::get_submission_value( $submission, 'issuer' );
$issuer = $submission_helper->get_value_by_tag_basetype_or_name_or_option( 'pronamic_pay_issuer' );

$payment->title = $title;

Expand All @@ -206,17 +136,14 @@ public static function get_submission_payment( WPCF7_Submission $submission ) {
$payment->set_meta( 'issuer', $issuer );
$payment->set_source( 'contact-form-7' );

// Total amount.
$payment->set_total_amount( new Money( $amount->get_value() ) );

// Contact.
$contact_name = new ContactName();
$contact_name->set_first_name( self::get_submission_value( $submission, 'first_name' ) );
$contact_name->set_last_name( self::get_submission_value( $submission, 'last_name' ) );
$contact_name->set_first_name( $submission_helper->get_value_by_tag_name_or_option( 'pronamic_pay_first_name' ) );
$contact_name->set_last_name( $submission_helper->get_value_by_tag_name_or_option( 'pronamic_pay_last_name' ) );

$customer = new Customer();
$customer->set_name( $contact_name );
$customer->set_email( self::get_submission_value( $submission, 'email' ) );
$customer->set_email( $submission_helper->get_value_by_tag_name_or_option_or_basetype( 'pronamic_pay_email', 'email' ) );

$payment->set_customer( $customer );

Expand All @@ -242,9 +169,9 @@ public static function get_submission_payment( WPCF7_Submission $submission ) {
];

foreach ( $address_fields as $field ) {
$billing_value = self::get_submission_value( $submission, 'billing_address_' . $field );
$shipping_value = self::get_submission_value( $submission, 'shipping_address_' . $field );
$address_value = self::get_submission_value( $submission, 'address_' . $field );
$billing_value = $submission_helper->get_value_by_tag_name_or_option( 'pronamic_pay_billing_address_' . $field );
$shipping_value = $submission_helper->get_value_by_tag_name_or_option( 'pronamic_pay_shipping_address_' . $field );
$address_value = $submission_helper->get_value_by_tag_name_or_option( 'pronamic_pay_address_' . $field );

if ( ! empty( $billing_value ) || ! empty( $address_value ) ) {
$callback = [ $billing_address, 'set_' . $field ];
Expand Down
Loading

0 comments on commit 8f26fbd

Please sign in to comment.