使用PayPal并运行一些ajax请求。第一个ajax请求为我提供了一些需要传递给第二个ajax请求的数据。第一个请求工作正常,我正在获取预批准密钥,然后我想将该预批准密钥传递回wordpress并保存它。我能想到的将预验证键传递回wordpress的唯一方法是运行另一个链接到第一个的ajax请求。
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\\u操作
add_action( \'wp_ajax_talknow_preapproval\', [ $this, \'talknow_preapproval\' ] );
add_action( \'wp_ajax_talknow_save_preapproval\', [ $this, \'talknow_save_preapproval\' ] );
php函数如下
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();
}
}
}
}
php函数仍在进行中,此时已对值进行了硬编码。如果有人能帮忙那就太好了