1

I need to add "something" to the Woocommerce payfast plugin to enable it to process split payments, as per the Payfast website it would look like this:

{ "split_payment" : { "merchant_id":10000105, "percentage":10, "amount":500, "min":100, "max":100000 } }

and it is submitted by form like this:

<input type="hidden" name="setup" value='{ "split_payment" : {
"merchant_id":10000105,
"percentage":10,
"min":100,
"max":100000}}' > 

I have looked at the class-wc-gateway-payfast.php file and cannot see where this could be "injected" and I have looked for the html form and cannot find that either .... My gut tells me this is simple yet I cannot get my head around it :

The Payfast link giving me the instructions can be found here (in case I am not reading it properly) : https://developers.payfast.co.za/documentation/?html#direct-request-method

Am I not correct in my thinking that it must go here :

/**
 * Generate the PayFast button link.
 *
 * @since 1.0.0
 */
public function generate_payfast_form( $order_id ) {
    $order         = wc_get_order( $order_id );
    // Construct variables for post
    $this->data_to_send = array(
        // Merchant details
        'merchant_id'      => $this->merchant_id,
        'merchant_key'     => $this->merchant_key,
        'return_url'       => $this->get_return_url( $order ),
        'cancel_url'       => $order->get_cancel_order_url(),
        'notify_url'       => $this->response_url,

        // Billing details
        'name_first'       => self::get_order_prop( $order, 'billing_first_name' ),
        'name_last'        => self::get_order_prop( $order, 'billing_last_name' ),
        'email_address'    => self::get_order_prop( $order, 'billing_email' ),

        // Item details
        'm_payment_id'     => ltrim( $order->get_order_number(), _x( '#', 'hash before order number', 'woocommerce-gateway-payfast' ) ),
        'amount'           => $order->get_total(),
        'item_name'        => get_bloginfo( 'name' ) . ' - ' . $order->get_order_number(),
        /* translators: 1: blog info name */
        'item_description' => sprintf( __( 'New order from %s', 'woocommerce-gateway-payfast' ), get_bloginfo( 'name' ) ),

        // Custom strings
        'custom_str1'      => self::get_order_prop( $order, 'order_key' ),
        'custom_str2'      => 'WooCommerce/' . WC_VERSION . '; ' . get_site_url(),
        'custom_str3'      => self::get_order_prop( $order, 'id' ),
        'source'           => 'WooCommerce-Free-Plugin',
    );
kashalo
  • 3,442
  • 2
  • 11
  • 28

1 Answers1

1

The form gets generated based on the associative array. You would have to add the following to the array for split payments:

'setup' => json_encode(['split_payment' => ['merchant_id' => 10000105, 'percentage'=>10, 'min' => 100, 'max' => 100000]]),

The json_encode() function is used to convert the PHP multidimensional array into a JSON object. More information on the json_encode() function can be found here.

The complete array will be as follows:

$this->data_to_send = array(
    // Merchant details
    'merchant_id'      => $this->merchant_id,
    'merchant_key'     => $this->merchant_key,
    'return_url'       => $this->get_return_url( $order ),
    'cancel_url'       => $order->get_cancel_order_url(),
    'notify_url'       => $this->response_url,

    // Billing details
    'name_first'       => self::get_order_prop( $order, 'billing_first_name' ),
    'name_last'        => self::get_order_prop( $order, 'billing_last_name' ),
    'email_address'    => self::get_order_prop( $order, 'billing_email' ),

    // Item details
    'm_payment_id'     => ltrim( $order->get_order_number(), _x( '#', 'hash before order number', 'woocommerce-gateway-payfast' ) ),
    'amount'           => $order->get_total(),
    'item_name'        => get_bloginfo( 'name' ) . ' - ' . $order->get_order_number(),
    /* translators: 1: blog info name */
    'item_description' => sprintf( __( 'New order from %s', 'woocommerce-gateway-payfast' ), get_bloginfo( 'name' ) ),

    //Split Payment
    'setup' => json_encode(['split_payment' => ['merchant_id' => 10000105, 'percentage'=>10, 'min' => 100, 'max' => 100000]]),

    // Custom strings
    'custom_str1'      => self::get_order_prop( $order, 'order_key' ),
    'custom_str2'      => 'WooCommerce/' . WC_VERSION . '; ' . get_site_url(),
    'custom_str3'      => self::get_order_prop( $order, 'id' ),
    'source'           => 'WooCommerce-Free-Plugin',
);
Daniel_ZA
  • 574
  • 11
  • 26