• I’ve written a function to pass metadata using direct_stripe_before_success_redirection, but can’t get it to accept any variable generated by WordPress function.

    Note that these results all come in Test Mode.

    So, for example, in the below, if I set the variable $recipient to “Joe Blow”, it comes out on the other end on the Stripe dashboard:

    $recipient = 'Joe Blow' ;
    
    $ch = \Stripe\Charge::retrieve($chargeID);
     	$ch->metadata[recipient]=$recipient;
     	$ch->save();

    That will get me

    recipient: Joe Blow

    in the Stripe Payment Metadata section.

    However, if I set $recipient using a function (passing $post_id) to it, I get the payment Token instead of the value of the variable.

    So,

    $recipient = get_the_title( $post_id );
    $ch = \Stripe\Charge::retrieve($chargeID);
    $ch->metadata[recipient]=$recipient;
    $ch->save();

    Produces the following in the Stripe dashboard Metadata section:

    recipient: tok_1Bl7AMD4ac685pxnsV2QmiJ6

    I am able to pass such values via Button ID – the actual button ID, not the $button_id variable) which is how some Stripe users used to do this kind of thing, I believe.

    The same thing happens when I try to pass data via description or fields other than metadata: I can pass a string, but when I use a function, the value that ought to be a string instead is replaced by the Token.

    Any ideas or solutions?

    • This topic was modified 8 years, 4 months ago by CK MacLeod.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Nicolas Figueira

    (@nahuelmahe)

    Hello @ck-macleod,

    Thank you for using Direct Stripe.

    Did you set a breakpoint or var_dumped after the first $recipient when you use a function ?
    Are you sure $post_id and therefore $recipient is defined ?

    Kind regards

    Thread Starter CK MacLeod

    (@ck-macleod)

    Hi, thanks for your reply (and for the great plugin).

    Unfortunately, I no longer have access for debugging purposes to the installation where I installed the custom function. (Actually, that installation no longer exists, as it was a staging site that the client decided to remove. I don’t have access to the live site’s filesystem.)

    Before I wrote you, hoping that you might already have a solution in hand to this problem, I did test to make sure that $post_id and the other four variables were available. I also made sure that I could pass a string to the metadata key-value pair, and in fact pass one of the five variables. I used $button_id because I could also set its content dynamically in functions deploying the Direct Stripe shortcode.

    So, my kludge looks like

    1. Add shortcode via filter function with dynamically updating button_id
    2. Save button_id to Stripe object metadata

    In the latter, as per my initial example, the string “Sample Post” saved as variable $sample_post_title could be passed to Stripe as metadata.

    $post_id, received via the direct_stripe_before_success_redirection action could also be passed through and saved to the Stripe Object.

    But, $sample_post_title as in $sample_post_title = get_the_title( $post_id ); or in any other function based on $post_id failed in the manner indicated, producing the $token over on the Stripe dashboard under metadata, instead of the expected content. As mentioned, it’s also possible to save $button_id as received to the Stripe Object.

    At this point, I’m asking as much out of curiosity and for possible future use of the plugin. Are you able to save dynamic variables to the Stripe Object using your action?

    Simplifying what I was trying to accomplish (which wasn’t really very complicated):

    /**
     * SAVE POST TITLE AS METADATA TO STRIPE OBJECT
     */
    function add_post_title_to_stripe($chargeID, $post_id, $button_id, $user_id, $token) {
    	
    	$title = get_the_title( $post_id );
    	
     	//Retrieve Stripe Charge and update it
     	$ch = \Stripe\Charge::retrieve($chargeID);
     	$ch->metadata[title]=$title;
     	$ch->save();
     
    }
    
    add_action( 'direct_stripe_before_success_redirection', 'add_post_title_to_stripe', 10, 5 );

    Every time I tried anything like the above, I’d get the token over on the Stripe side.

    Plugin Author Nicolas Figueira

    (@nahuelmahe)

    Hello @ck-macleod,

    Thank you again for sharing your concern about Direct Stripe and for the delayed reply.

    Note that the $post_id argument in direct_stripe_before_success_redirection is the ID of the Direct Stripe log for the transaction just processed. The title is the token of the log, the $post_id will help to get all the meta data stored on the logs.

    If you were trying to get data fromthe current page, note that the action happens before the redirection and after the transaction has been completed and not post ID will be attached with get_the_ID();

    That would be really interesting adding hooks to get data from the previous page or a hook afdter the redirection completed.

    Best,

    Thread Starter CK MacLeod

    (@ck-macleod)

    That makes sense, although it doesn’t completely explain all of the behavior (why the functions return the token instead of NULL or FALSE or empty, for example).

    Needless to say, I haven’t examined this matter to the same depth that you have. I’m not sure whether there are other methods conveniently available for passing information to the Stripe Object.

    What I did with $button_id qualifies as a hack, since I’m using the button’s html ID to store data. I’m not sure my explanation above was clear, but I wrote a function that prints the shortcode to post content, with the $button_id set dynamically using the post object via WordPress functions.

    It occurs to me that I could store a long string in $button_id, with elements separated by one or another special character, then explode it as an array, and get the data I wanted that way. So, the html would show something looking like id="Made_Up_Title|1029876432|10" etc.

    Having data attributes to work with would be a lot cleaner. Or maybe you could save the data to a cookie – which might be a little more complicated initially, but would be more correct and potentially more flexible.

    I can’t test these alternatives easily now, since I no longer have access to the installation I was working on, and I don’t have a pressing need for this – although I do see people working with Stripe more often than I used to, so may be returning to this matter again in a practical context. I’ll be curious to see what you come up with!

    • This reply was modified 8 years, 2 months ago by CK MacLeod.
    Thread Starter CK MacLeod

    (@ck-macleod)

    Just thought I’d note that I’ll be working with the plugin again, and in fact am contracted to write a simple add-on plugin that will enable the following:

    -enable/add to automatically in designated post types, with dynamically adjusting elements
    -enable turning on/off of the above for specific posts
    -enable basic customization of stripe payment “launcher” box via setting page

    Doing the above for a single, specific installation is a lot simpler than doing for most/any installations (for a general release plugin), but initial tasks will be similar. At some future point, I may look again into the data transmission issue, and see about using a cookie in the way I described above.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Bug passing metadata direct_stripe_before_success_redirection’ is closed to new replies.