Instantly share code, notes, and snippets.
Created
February 2, 2023 22:25
-
Save theMugician/6b591a1659f8591723e8cba10a0148c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public static function instant_quote_minneapolis_authorizenet_after_submission() { | |
add_action( 'gform_pre_submission', function( $form, $entry ) { | |
if ( $form['id'] !== self::$gravity_forms['instant_quote_minneapolis']['form_id'] ) { | |
return; | |
} | |
$fields = self::$gravity_forms['instant_quote_minneapolis']['fields']; | |
$input_zip_code = 'input_' . $fields['zip_code']; | |
$input_email = 'input_' . $fields['email']; | |
$input_first_name = 'input_' . $fields['first_name']; | |
$input_last_name = 'input_' . $fields['last_name']; | |
$input_address = 'input_' . $fields['address']; | |
$input_franchise_location = 'input_' . $fields['franchise_location']; | |
$input_phone = 'input_' . $fields['phone']; | |
$input_amount = 'input_' . $fields['amount']; | |
$hosted_payment_return_options = array( | |
'showReceipt' => true, | |
'url' => get_the_permalink() . '?status=1', | |
'urlText' => 'Continue', | |
'cancelUrl' => get_the_permalink() . '?status=0', | |
'cancelUrlText' => 'Cancel', | |
); | |
$authorizenet_object = array( | |
'getHostedPaymentPageRequest' => array( | |
'merchantAuthentication' => array( | |
'name' => 'xxxxxxxxxxxxxx', | |
'transactionKey' => 'xxxxxxxxxxxxxxxxxxxxx', | |
), | |
'transactionRequest' => array( | |
'transactionType' => 'authOnlyTransaction', | |
'amount' => $_POST[$input_amount], | |
'profile' => array( | |
'customerProfileId' => '123456789', | |
), | |
'customer' => array( | |
'email' => $_POST[$input_email], | |
), | |
'billTo' => array( | |
'firstName' => $_POST[$input_first_name], | |
'lastName' => $_POST[$input_last_name], | |
'company' => 'Maid Brigade', | |
'address' => $_POST[$input_address], | |
'city' => $_POST[$input_franchise_location], | |
'state' => 'MN', | |
'zip' => $_POST[$input_zip_code], | |
'country' => 'USA', | |
'phoneNumber' => $_POST[$phone], | |
), | |
), | |
'hostedPaymentSettings' => array( | |
'setting' => array( | |
0 => array( | |
'settingName' => 'hostedPaymentReturnOptions', | |
'settingValue' => json_encode( $hosted_payment_return_options ), | |
), | |
1 => array( | |
'settingName' => 'hostedPaymentButtonOptions', | |
'settingValue' => json_encode( array( | |
'text' => 'Pay', | |
) ), | |
), | |
2 => array( | |
'settingName' => 'hostedPaymentStyleOptions', | |
'settingValue' => json_encode( array( | |
'bgColor' => 'blue', | |
) ), | |
), | |
3 => array( | |
'settingName' => 'hostedPaymentPaymentOptions', | |
'settingValue' => json_encode( array( | |
'cardCodeRequired' => false, | |
'showCreditCard' => true, | |
'showBankAccount' => false, | |
) ), | |
), | |
4 => array( | |
'settingName' => 'hostedPaymentSecurityOptions', | |
'settingValue' => json_encode( array( | |
'captcha' => false, | |
) ), | |
), | |
5 => array( | |
'settingName' => 'hostedPaymentShippingAddressOptions', | |
'settingValue' => json_encode( array( | |
'show' => true, | |
'required' => false, | |
) ), | |
), | |
6 => array( | |
'settingName' => 'hostedPaymentBillingAddressOptions', | |
'settingValue' => json_encode( array( | |
'show' => true, | |
'required' => false, | |
) ), | |
), | |
7 => array( | |
'settingName' => 'hostedPaymentCustomerOptions', | |
'settingValue' => json_encode( array( | |
'showEmail' => false, | |
'requiredEmail' => false, | |
'addPaymentProfile' => true, | |
) ), | |
), | |
8 => array( | |
'settingName' => 'hostedPaymentOrderOptions', | |
'settingValue' => json_encode( array( | |
'show' => true, | |
'merchantName' => 'Maid Brigade.', | |
) ), | |
), | |
9 => array( | |
'settingName' => 'hostedPaymentIFrameCommunicatorUrl', | |
'settingValue' => json_encode( array( | |
'url' => 'https://mysite.com/iFrameCommunicator.html', | |
) ), | |
), | |
), | |
), | |
), | |
); | |
$endpoint = 'https://api.authorize.net/xml/v1/request.api'; | |
$arguments = array( | |
'body' => json_encode( $authorizenet_object ), | |
'headers' => array( | |
'Content-Type' => 'application/json', | |
) | |
); | |
$response = wp_remote_post( $endpoint, $arguments ); | |
if ( ! is_wp_error( $response ) ) { | |
$body = json_decode( $body, true ); | |
$token = $body['token']; | |
if ( $token ) { // The reponse is correct and I recieve the correct token | |
$token_response = wp_remote_post( | |
'https://accept.authorize.net/payment/payment', | |
array( | |
'body' => json_encode( array( | |
'token' => $token | |
) ), | |
'headers' => array( | |
'Content-Type' => 'application/json', | |
), | |
) | |
); | |
// How do I then redirect with the token? | |
if ( ! is_wp_error( $token_response ) ) { | |
header( 'Location: https://accept.authorize.net/payment/payment' ); // I get Missing or invalid token. | |
var_dump($token_response); // The status is ok and 200 when I check | |
} | |
} | |
} else { | |
$error_message = $response->get_error_message(); | |
throw new Exception( $error_message ); | |
} | |
}, 10, 4 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment