working with PayPal and running some ajax requests. The first ajax request gives me some data that I need to pass to the second ajax request. The first request is working fine, and I am getting the preapproval key, I then want to pass that preaproval key back to wordpress and save it. The only way I can think of to pass the preaproval key back to wordpress is to run another ajax request chained off the first.
JS
var settings = {
"url": url,
"method": "POST",
"data": {
'action': 'talknow_preapproval',
'user_id': user_id,
'therapist_id': therapist_id,
'cost_per_session': cost_per_session
},
"headers": {},
error: function( jqXHR, textStatus, errorThrown ) {
alert( 'An error occurred... ' );
window.console.log(
'status code ' + jqXHR.status + ', the error thrown was ' + errorThrown + ', the response text: ' + jqXHR.responseText + ', text status: ' + textStatus
);
window.console.log( 'full jqXHR object:' + jqXHR );
},
success: function( response ) {
console.info( 'ajax request successful' );
var result = JSON.parse(response);
//check if the result has errors, if yes deal with them
if( typeof result.error !== 'undefined' && result.error.length > 0 ) {
alert('Looks there was a problem launching the video, please contact support');
console.warn('There was an error: ');
console.log(result.error);
}
//console.log(result);
if( result.preapprovalKey ) {
var ppUrl = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_ap-preapproval&preapprovalkey=' + encodeURIComponent(result.preapprovalKey);
console.info('we are redirecting you to paypal' + ppUrl);
//window.location.replace(ppUrl); // take user to paypal so that they can enter their card details and approve
}
}
}
$.ajax( settings ).done( function( response ) {
console.info('ajax request done');
} ).then(function(response) {
var result = JSON.parse(response);
var saveSettings = {
"url": url,
"method": "POST",
"data": {
'action': 'talknow_save_preapproval',
'user_id': user_id,
'therapist_id': therapist_id,
'cost_per_session': cost_per_session,
'preapproval_key': result.preapproval_key
},
"headers": {},
error: function( jqXHR, textStatus, errorThrown ) {
alert( 'An error occurred... ' );
window.console.log(
'status code ' + jqXHR.status + ', the error thrown was ' + errorThrown + ', the response text: ' + jqXHR.responseText + ', text status: ' + textStatus
);
window.console.log( 'full jqXHR object:' + jqXHR );
},
success: function( response ) {
console.info('save preapproval ajax request success');
console.log( response );
}
}
$.ajax( saveSettings ).done( function( response ) {
console.log( response );
console.info('save preapproval ajax request done');
} );
});
wp add_action
add_action( 'wp_ajax_talknow_preapproval', [ $this, 'talknow_preapproval' ] );
add_action( 'wp_ajax_talknow_save_preapproval', [ $this, 'talknow_save_preapproval' ] );
The php functions are below
public function talknow_preapproval() {
if( 'POST' !== $_SERVER[ 'REQUEST_METHOD' ] ) {
wp_die( __( 'oh no, something has gone very wrong' ) );
} else {
$user_id = isset( $_POST[ 'user_id' ] ) ? $_POST[ 'user_id' ] : '';
$therapist_id = isset( $_POST[ 'therapist_id' ] ) ? $_POST[ 'therapist_id' ] : '';
$cost_per_session = isset( $_POST[ 'cost_per_session' ] ) ? $_POST[ 'cost_per_session' ] : '';
if( !empty( $user_id ) && !empty( $therapist_id ) && !empty( $cost_per_session ) ) {
$user_id = intval( $user_id );
$therapist_id = intval( $therapist_id );
$cost_per_session = (float) $cost_per_session;
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_URL => "https://svcs.sandbox.paypal.com/AdaptivePayments/Preapproval", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => "cancelUrl=redacted_for_security¤cyCode=GBP&endingDate=2017-05-10&maxAmountPerPayment=50.00&maxNumberOfPayments=1&maxTotalAmountOfAllPayments=50.00&pinType=NOT_REQUIRED&requestEnvelope.errorLanguage=en_US&returnUrl=redacted_for_security%2F&startingDate=2017-05-09",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache", "content-type: application/x-www-form-urlencoded", "x-paypal-application-id: APP-80W284485P519543T", "x-paypal-request-data-format: NV", "x-paypal-response-data-format: JSON", "x-paypal-security-password: redacted_for_security", "x-paypal-security-signature: redacted_for_security", "x-paypal-security-userid: redacted_for_security",
),
) );
$response = curl_exec( $curl );
$err = curl_error( $curl );
curl_close( $curl );
if( $err ) {
echo "cURL Error #:" . $err;
die();
} else {
echo $response;
die();
}
} else {
_e( 'There was a problem with the preapproval process, please contact support' );
die();
}
}
}
public function talknow_save_preapproval() {
if( 'POST' !== $_SERVER[ 'REQUEST_METHOD' ] ) {
wp_die( __( 'oh no, something has gone very wrong' ) );
} else {
$user_id = isset( $_POST[ 'user_id' ] ) ? $_POST[ 'user_id' ] : '';
$therapist_id = isset( $_POST[ 'therapist_id' ] ) ? $_POST[ 'therapist_id' ] : '';
$cost_per_session = isset( $_POST[ 'cost_per_session' ] ) ? $_POST[ 'cost_per_session' ] : '';
$preapproval_key = isset( $_POST[ 'preapproval_key' ] ) ? $_POST[ 'preapproval_key' ] : '';
if( ! empty( $user_id ) && ! empty( $therapist_id ) && ! empty( $cost_per_session ) && ! empty( $preapproval_key ) ) {
$user_id = intval( $user_id );
$therapist_id = intval( $therapist_id );
$cost_per_session = (float) $cost_per_session;
$preapproval_key = sanitize_text_field( $preapproval_key );
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_URL => "https://svcs.sandbox.paypal.com/AdaptivePayments/PreapprovalDetails",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "preapprovalKey=PA-6U047065P9760690U&requestEnvelope.errorLanguage=en_US",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache", "content-type: application/x-www-form-urlencoded", "x-paypal-application-id: APP-80W284485P519543T", "x-paypal-request-data-format: NV", "x-paypal-response-data-format: JSON", "x-paypal-security-password: redacted_for_security", "x-paypal-security-signature: redacted_for_security", "x-paypal-security-userid: redacted_for_security",
),
) );
$response = curl_exec( $curl );
$err = curl_error( $curl );
curl_close( $curl );
if( $err ) {
echo "cURL Error #:" . $err;
die();
} else {
echo $response;
die();
}
}
}
}
The php functions are still work in progress, values have been hardcoded at this point. If anyone can help that would be great
$_POST['preapproval_key']
should have been$_POST['preapprovalKey']
, both me and my buddy missed it..*sigh