Messaging
Created 7 years ago by jcastillotx

So I am doing an API response on a form and if the response comes back without an approval have to send to the form to tell the user. I have placed in

// Was it a approval if ($response['resptext'] != 'Approval') {

        $messages = app(MessageBag::class);
        $messages->warning('This is an error message.');

        return;
    }

I get both the success and error. How can I stop the success from coming up.

jcastillotx  —  7 years ago

I am using the FormHandler to process the form and API:

$url      = 'https://fts.cardconnect.com:6443/cardconnect/rest';
    $user     = 'testing';
    $password = 'testing123';

    if ( ! $builder->canSave() ) {
        return;
    }

    // Set up the API
    $client = new CardConnectRestClient( $url, $user, $password );

    // Send a request
    $request = array(
        'merchid'   => '496160873888',
        'accttyppe' => $builder->getFormValue( 'card' ),
        'account'   => $builder->getFormValue( 'cc' ),
        'expiry'    => $builder->getFormValue( 'exp_m' ) . $builder->getFormValue( 'exp_y' ),
        'cvv2'      => $builder->getFormValue( 'ccv' ),
        'amount'    => $builder->getFormValue( 'donation' ) . '.00',
        'currency'  => "USD",
        'orderid'   => 'DN' . sprintf( '%05d', rand( 0, 99999 ) ),
        'name'      => $builder->getFormValue( 'first_name' ) . ' ' . $builder->getFormValue( 'last_name' ),
        'street'    => $builder->getFormValue( 'address' ),
        'city'      => $builder->getFormValue( 'city' ),
        'region'    => $builder->getFormValue( 'state' ),
        'country'   => "US",
        'postal'    => $builder->getFormValue( 'zipcode' ),
        'tokenize'  => "Y",
    );

    // Grab the response
    $response = $client->authorizeTransaction( $request );

    // Add to the form
    $builder->setFormValue( 'token', $response['token'] );
    $builder->setFormValue( 'retref', $response['retref'] );
    $builder->setFormValue( 'response', $response['resptext'] );

    // Was it a approval
    if ($response['resptext'] != 'Approval') {

        return;
    }

    // Save the Form
    $builder->saveForm();
jcastillotx  —  7 years ago

It saves fine in the back just need to pass the error message from $response['resptext']

ryanthompson  —  7 years ago

It might be more reliable to set the values directly on the entry:

$entry = $builder->getFormEntry();

$entry->token = $response['token'];

You can inject the MessageBag from Streams Platform and add an error message that way OR you can add field error to the form through the builder in order to manually cause an error state on a form field.

$builder->addFormError('field_slug', 'The error description.');

Then the field field_slug will have be highlighted as an error. Otherwise the message bag just flashes the message.

jcastillotx  —  7 years ago

I tried that and was giving me an error. Let me try again.

jcastillotx  —  7 years ago

Ok so I changed out to entry but the error message doesn't show on the front.

jcastillotx  —  7 years ago
/**
 * @param \Anomaly\Streams\Platform\Ui\Form\FormBuilder $builder
 */
public function handle( FormBuilder $builder ) {

    $url      = 'https://fts.cardconnect.com:6443/cardconnect/rest';
    $user     = 'testing';
    $password = 'testing123';

    if ( ! $builder->canSave() ) {
        return;
    }

    // Set up the API
    $client = new CardConnectRestClient( $url, $user, $password );

    // Send a request
    $request = array(
        'merchid'   => '496160873888',
        'accttyppe' => $builder->getFormValue( 'card' ),
        'account'   => $builder->getFormValue( 'cc' ),
        'expiry'    => $builder->getFormValue( 'exp_m' ) . $builder->getFormValue( 'exp_y' ),
        'cvv2'      => $builder->getFormValue( 'ccv' ),
        'amount'    => $builder->getFormValue( 'donation' ) . '.00',
        'currency'  => "USD",
        'orderid'   => 'DN' . sprintf( '%05d', rand( 0, 99999 ) ),
        'name'      => $builder->getFormValue( 'first_name' ) . ' ' . $builder->getFormValue( 'last_name' ),
        'street'    => $builder->getFormValue( 'address' ),
        'city'      => $builder->getFormValue( 'city' ),
        'region'    => $builder->getFormValue( 'state' ),
        'country'   => "US",
        'postal'    => $builder->getFormValue( 'zipcode' ),
        'tokenize'  => "Y",
    );

    // Grab the response
    $response = $client->authorizeTransaction( $request );

    $entry = $builder->getFormEntry();

    $entry->token    = $response['token'];
    $entry->response = $response['resptext'];
    $entry->retref   = $response['retref'];

    // Add to the form
    //$builder->setFormValue( 'token', $response['token'] );
    //$builder->setFormValue( 'retref', $response['retref'] );
    //$builder->setFormValue( 'response', $response['resptext'] );

    // Was it a approval
    if ($response['resptext'] != 'Approval') {

        $builder->addFormError('response', $response['resptext']);
        return;
    }

    // Save the Form
    $builder->saveForm();

}
jcastillotx  —  7 years ago

Ok so I am using the form handler so it won't update with $entry->field_name because it has not saved the form.

ryanthompson  —  7 years ago

Try setting $builder->setOption('success_message', false);.

Be sure to do it BEFORE running saveForm.

jcastillotx  —  7 years ago

thanks

jcastillotx  —  7 years ago