如何将Payatrader支付网关集成到Gravity Forms并实现提交后跳转支付
Hey there! Integrating Payatrader with Gravity Forms to handle post-submission payments is totally doable—here’s a step-by-step breakdown tailored to your needs:
First, make sure your Gravity Forms setup is solid:
- Confirm you’re running the latest version of Gravity Forms, activated and working properly on your WordPress site.
- Build out your target form with all necessary fields (like customer email, payment amount, billing details—whatever Payatrader requires for transactions).
Since Payatrader likely doesn’t have an official Gravity Forms integration, we’ll use custom code to hook into form submissions and redirect users to the payment gateway.
Step 2.1: Add Hidden Fields for Transaction Data (Optional but Smart)
Toss a hidden field into your form to store critical data like:
- Calculated payment amount (use Gravity Forms’ built-in calculation features if needed)
- A unique order ID (you can use the Gravity Forms entry ID or generate a custom one)
This makes it easier to pass consistent data to Payatrader later.
Step 2.2: Hook into gform_after_submission
This Gravity Forms action fires right after a form is successfully submitted. We’ll use it to build the Payatrader payment URL and redirect the user.
Add this code to your WordPress theme’s functions.php file (or a custom plugin, for better maintainability):
// Replace YOUR_FORM_ID with your actual Gravity Form ID add_action('gform_after_submission_YOUR_FORM_ID', 'redirect_to_payatrader_checkout', 10, 2); function redirect_to_payatrader_checkout($entry, $form) { // Pull data from the form entry $payment_amount = rgar($entry, 'FIELD_ID_FOR_AMOUNT'); // Swap with your amount field's ID $customer_email = rgar($entry, 'FIELD_ID_FOR_EMAIL'); // Swap with your email field's ID $order_id = $entry['id']; // Use Gravity Forms entry ID as order ID, or generate your own // Build Payatrader request parameters (refer to their official docs for exact requirements) $payatrader_params = array( 'merchant_id' => 'YOUR_PAYATRADER_MERCHANT_ID', // Replace with your merchant ID 'amount' => $payment_amount, 'currency' => 'USD', // Update to your preferred currency code 'order_id' => $order_id, 'customer_email' => $customer_email, 'return_url' => home_url('/payment-success/'), // Page to redirect after successful payment 'cancel_url' => home_url('/payment-cancel/'), // Page to redirect if payment is canceled // Add any required signature or security parameters here (per Payatrader's docs) ); // Generate the full Payatrader checkout URL $checkout_url = 'https://www.payatrader.com/pay?' . http_build_query($payatrader_params); // Redirect user to Payatrader wp_redirect($checkout_url); exit; }
Important notes:
- Replace all placeholder values (like
YOUR_FORM_ID,FIELD_ID_FOR_AMOUNT) with your actual form/field IDs and merchant credentials. - Double-check Payatrader’s official docs for required parameters—missing or incorrect fields will break the flow.
Step 2.3: Set Up Webhooks for Payment Status Updates
After payment completes, Payatrader will send a callback to your site to confirm the status. You need to handle this to update your Gravity Forms entry.
- In your Payatrader dashboard, set the webhook URL to something like
home_url('/payatrader-webhook/'). - Add this code to handle the webhook in WordPress:
// Register a custom REST endpoint for the webhook add_action('rest_api_init', function () { register_rest_route('payatrader/v1', '/webhook/', array( 'methods' => 'POST', 'callback' => 'handle_payatrader_webhook', 'permission_callback' => '__return_true', // Add stricter permission checks if needed )); }); function handle_payatrader_webhook($request) { // Get the payload from Payatrader $payload = $request->get_body(); $transaction_data = json_decode($payload, true); // Verify the webhook is legitimate (follow Payatrader's signature rules) if (!verify_payatrader_signature($transaction_data)) { return new WP_REST_Response(array('status' => 'invalid'), 403); } // Update the Gravity Forms entry with payment status $order_id = $transaction_data['order_id']; $entry = GFAPI::get_entry($order_id); if ($entry) { // Mark entry as paid (customize this based on your needs) $entry['status'] = 'payment_completed'; GFAPI::update_entry($entry); // Trigger notifications (e.g., send a confirmation email to the user) GFCommon::send_notification($form, $entry, 'user_notification'); } // Send success response to Payatrader return new WP_REST_Response(array('status' => 'success'), 200); } // Implement signature verification per Payatrader's docs function verify_payatrader_signature($data) { $secret_key = 'YOUR_PAYATRADER_SECRET_KEY'; // Example signature logic (replace with Payatrader's actual method) $generated_signature = hash('sha256', $data['order_id'] . $data['amount'] . $secret_key); return $generated_signature === $data['signature']; }
- Critical: The signature verification step is non-negotiable—it prevents fake webhook requests from messing with your data. Follow Payatrader’s exact instructions for this.
Don’t skip this part!
- Submit a test form to confirm you’re redirected to Payatrader with the correct parameters.
- Use Payatrader’s sandbox/test environment to simulate successful and canceled payments.
- Check that the webhook updates your Gravity Forms entry status correctly, and notifications are sent.
Before diving into custom code, quickly search the WordPress plugin directory or Gravity Forms’ third-party extensions marketplace. Sometimes other developers have built free/paid integrations for Payatrader—using a plugin saves you from maintaining custom code long-term.
内容的提问来源于stack exchange,提问作者Marvyn Sue




