0

I've been reading around and cannot find a solution that works for the requirement I have. I need to dynamically add values to the 'add' part of this array depending on conditions. I know that you cannot put any if statements inside the array itself.

The correct syntax (from the documentation) is:

$subResult = $gateway->subscription()->create([
    'paymentMethodToken' => 'the_token',
    'planId' => 'thePlanId',
    'addOns' => [
        'add' => [
            [
                'inheritedFromId' => 'addon1',
                'amount' => '10'
            ],
            [
                'inheritedFromId' => 'addon2',
                'amount' => '10'
            ]
        ]
    ]
]);

From what I had read on a similar question on SO, I tried the following (where $addon1 and $addon2 would be the conditions set earlier in the code)

$addon1 = true;
$addon2 = true;
$subResult = $gateway->subscription()->create([
 'paymentMethodToken' => 'the_token',
    'planId' => 'thePlanId',
    'addOns' => [
        'add' => [
                    ($addon1 ? array(
                        [
                        'inheritedFromId' => 'productAddon1Id',
                        'amount' => '10'
                        ]) : false),
                    ($addon2 ? array(
                        [
                        'inheritedFromId' => 'productAddon2Id',
                        'amount' => '10'
                        ]) : false)
                ]
        ]

]);

But I get back a Warning: XMLWriter::startElement(): Invalid Element Name so I suspect that it does not like the structure and the code fails with a fatal error (interestingly, if I only set the first $addon to true it still comes up with the warning, but does actually work. With two it fails).

Is there another way to do this or did I get the syntax wrong?

I cannot hardcode all the possibilities due to the amount of possible product combinations.

Would appreciate and help. Thank you.

2
  • Looks messy to me, why not construct the addons separately and add them later? You have an extra array() call in there now for some reason and do you want a a bunch of false values in there if the addon don't exist? Commented Jun 11, 2018 at 13:27
  • Sounds like this is an XML issue (invalid schema?), not the use of the ternarys. Commented Jun 11, 2018 at 13:36

4 Answers 4

2

Don't try to do everything at once.

$add = [];
if( $addon1)
  $add[] = ['inheritedFromId'=>.......];
if( $addon2)
  .....

$subResult = $gateway->subscription()->create([
  'paymentMethodToken' => 'the_token',
  'planId' => 'thePlanId',
  'addOns' => [
    'add' => $add
  ]
]);
Sign up to request clarification or add additional context in comments.

Comments

1

you can put if statements in array declarations, it's called ternary operations:

$myArray['key'] = ($foo == 'bar' ? 1 : 2);

this is the basis of how to use.

1 Comment

@Progrock to an extent yes, but the OP put's I know that you cannot put any if statements inside the array itself. - my answer is showing that it is indeed possible to do this and can be done, which would be a solution to the problem :)
1

You're using the array() syntax together with the short array syntax ([]). See the manual. This means that e.g. your first element in add would be an array within an array. Perhaps that's why the XML error is occurring? Better would be:

        'add' => [
                    ($addon1 ? 
                        [
                        'inheritedFromId' => 'productAddon1Id',
                        'amount' => '10'
                        ] : null),
                    ($addon2 ?
                        [
                        'inheritedFromId' => 'productAddon2Id',
                        'amount' => '10'
                        ] : null)
                ]
        ]

3 Comments

Thank you, this as the easiest for me to understand and worked as expected. Thanks for taking the time to respond.
actually it only works if all the conditions are true, if one is false, it fails with Fatal error: Uncaught exception 'InvalidArgumentException' with message 'invalid keys: addOns[add], addOns[add]'
I've edited it to return null instead of false, might be worth a try. If not I recommend Niet the Dark Absol's solution.
0

Your syntax is fine. you are creating array with this structure

array (size=3)   
'paymentMethodToken' => string 'the_token' 
   (length=9) 
   'planId' => string 'thePlanId' 
   (length=9)   
   'addOns' => 
    array (size=1)
      'add' => 
        array (size=2)
          0 => 
            array (size=1)
              ...
          1 => 
            array (size=1)
              ...

My quess is that $gateway->subscription()->create() is does not like this structure of your array. Maybe the 'false' as value or the numeric keys. Check what does it expect and try again with new structure of the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.