PAYPAL WORKING

This commit is contained in:
asus
2024-02-24 16:57:31 +01:00
parent 745f5ebf91
commit 61b3f9f2f5
10 changed files with 300 additions and 236 deletions

View File

@@ -42,9 +42,6 @@ add_action('template_redirect', 'check_paypal_request');
function paypal_shortcode_content()
{
$fipfcard_paypal = new PLGNTLS_class();
/*
<input type="hidden" name="custom" value="5678" />
*/
$pp_sdk_currency = "EUR";
$pp_sdk_debug = "true";
@@ -57,13 +54,28 @@ function paypal_shortcode_content()
// $pp_sdk_attributes="src='$pp_sdk_src'";
// $pp_sdk_html_script="<script $pp_sdk_attributes></script>";
return $fipfcard_paypal->add_to_front(
$added_to_front = $fipfcard_paypal->add_to_front(
array(
$pp_sdk_src,
"js/paypal/paypal.js",
"html/paypal/paypal.html",
),
);
return $added_to_front;
}
/**
* the js file paypal.js needs to be imported as a module to use import
* @see https://developer.wordpress.org/reference/hooks/script_loader_tag/
*/
add_filter( 'script_loader_tag', 'add_id_to_script', 10, 3 );
function add_id_to_script( $tag, $handle, $src ) {
if ( $handle === 'PLGNTLS_paypal_js' ) {
$tag = '<script type="module" src="' . esc_url( $src ) . '" ></script>';
}
return $tag;
}

View File

@@ -13,20 +13,44 @@ if (!defined('ABSPATH')) {
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
*/
function handle_orders_request($request_data) {
try {
// Extract cart information from request body
$cart = $request_data['cart'];
try {
// Extract cart information from request body
$cart = $request_data['cart'];
// Process the order and get the response
$order_response = create_order($cart);
// Process the order and get the response
$order_response = create_order($cart);
// Return response
return new WP_REST_Response($order_response['json_response'], $order_response['http_status_code']);
} catch (Exception $e) {
// Handle errors
error_log('Failed to create order: ' . $e->getMessage());
return new WP_Error('500', 'Failed to create order.', array('status' => 500));
}
error_log("in handle_orders_request, order_response");
error_log(json_encode($order_response));
$user_id = get_current_user_id(); // Get the ID of the current user
$transaction_id = $order_response['json_response']->id;
$purchase_date = current_time('mysql');
$user_meta = get_user_meta($user_id);
error_log("in handle_orders_request, user_id");
error_log($user_id);
error_log("in handle_orders_request, user_meta");
error_log(json_encode($user_meta));
error_log("in handle_orders_request, transaction_id");
error_log($transaction_id);
error_log("in handle_orders_request, purchase_date");
error_log($purchase_date);
// Store purchase-related information as user meta
//add_user_meta($user_id, 'transaction_id', $transaction_id);
//add_user_meta($user_id, 'purchase_date', $purchase_date);
// Return response
return new WP_REST_Response($order_response['json_response'], $order_response['http_status_code']);
} catch (Exception $e) {
// Handle errors
error_log('Failed to create order: ' . $e->getMessage());
return new WP_Error('500', 'Failed to create order.', array('status' => 500));
}
}

View File

@@ -9,20 +9,20 @@ if (!defined('ABSPATH')) {
function handle_orders_capture_request($request) {
$order_id = $request['orderID'];
error_log("order_id");
error_log("in handle_orders_capture_request, order_id");
error_log($order_id);
try {
// Implement captureOrder function logic here
// Make sure you implement captureOrder function similar to the Node.js code
$response_data = captureOrder($order_id);
$response_data = null;
$http_status_code = $response_data['httpStatusCode'];
$json_response = $response_data['jsonResponse'];
$response_data = capture_order($order_id);
$http_status_code = $response_data['http_status_code'];
$json_response = $response_data['json_response'];
return new WP_REST_Response($json_response, $http_status_code);
} catch (Exception $e) {
}
catch (Exception $e) {
error_log('Failed to capture order: ' . $e->getMessage());
return new WP_REST_Response(array('error' => 'Failed to capture order.'), 500);
}
@@ -33,7 +33,7 @@ function handle_orders_capture_request($request) {
* Capture payment for the created order to complete the transaction.
* @see https://developer.paypal.com/docs/api/orders/v2/#orders_capture
*/
function captureOrder($orderID) {
function capture_order($orderID) {
$access_token = generate_access_token();
$url = PAYPAL_API_BASE_URL . '/v2/checkout/orders/' . $orderID . '/capture';
@@ -63,7 +63,7 @@ function captureOrder($orderID) {
curl_close($ch);
// in utils
return handle_response(response);
return handle_response($response);
};