Worldpay Payment Gateway - Passing Data Dynamically

571 Views Asked by At

I'm trying to create a payment gateway to work with WorldPay, so I'm basically hacking a PayPal gateway to achieve the result.

I've got everything connecting to the WorldPay test page and passing the data correctly, except for one field - the amount.

I need the amount to be passed dynamically, but WorldPay only provide instructions for a hard-coded amount.

I've had a look at a couple of other posts on here talking about integrations with WorldPay, but there's nothing that seems to help.

I'm building this locally so don't have any links for you unfortunately.

The pages I've already looked at here are: WORLDPAY Integration using php Validating payment amounts with WorldPay

I think this is the code that I need to tinker with to get the needed result:

I'd really appreciate it if someone can tell me what I'm doing wrong, or point me in the right direction.

Thanks in advance for your time and help.

/**
 * Retreive the paypal vars needed to send to the gatway to proceed with payment
 * @param EM_Booking $EM_Booking
 */
function get_paypal_vars($EM_Booking){
    global $wp_rewrite, $EM_Notices;
    $notify_url = $this->get_payment_return_url();
    $paypal_vars = array(
        'instId' => '211616',
        'testMode' => '100',
        'business' => get_option('em_'. $this->gateway . "_email" ), 
        'authMode' => 'A',
        'upload' => 1,
        'amount' => $price(),
        'currency' => get_option('dbem_bookings_currency', 'GBP'),
        'desc' => 'Description Goes Here',
        'accId1' => '211616',
        'cartId' => 'temp123',
        'notify_url' =>$notify_url,
        'charset' => 'UTF-8'
    );
    if( get_option('em_'. $this->gateway . "_lc" ) ){
        $paypal_vars['lc'] = get_option('em_'. $this->gateway . "_lc" );
    }
    //tax is added regardless of whether included in ticket price, otherwise we can't calculate post/pre tax discounts
    if( $EM_Booking->get_price_taxes() > 0 ){ 
        $paypal_vars['tax_cart'] = round($EM_Booking->get_price_taxes(), 2);
    }
    if( get_option('em_'. $this->gateway . "_return" ) != "" ){
        $paypal_vars['return'] = get_option('em_'. $this->gateway . "_return" );
    }
    if( get_option('em_'. $this->gateway . "_cancel_return" ) != "" ){
        $paypal_vars['cancel_return'] = get_option('em_'. $this->gateway . "_cancel_return" );
    }
    if( get_option('em_'. $this->gateway . "_format_logo" ) !== false ){
        $paypal_vars['cpp_logo_image'] = get_option('em_'. $this->gateway . "_format_logo" );
    }
    if( get_option('em_'. $this->gateway . "_border_color" ) !== false ){
        $paypal_vars['cpp_cart_border_color'] = get_option('em_'. $this->gateway . "_format_border" );
    }
    $count = 1;
    foreach( $EM_Booking->get_tickets_bookings()->tickets_bookings as $EM_Ticket_Booking ){ /* @var $EM_Ticket_Booking EM_Ticket_Booking */
        //divide price by spaces for per-ticket price
        //we divide this way rather than by $EM_Ticket because that can be changed by user in future, yet $EM_Ticket_Booking will change if booking itself is saved.

        $price = $EM_Ticket_Booking->get_price() / $EM_Ticket_Booking->get_spaces();
        if( $price > 0 ){
            $paypal_vars['item_name_'.$count] = wp_kses_data($EM_Ticket_Booking->get_ticket()->name);
            $paypal_vars['quantity_'.$count] = $EM_Ticket_Booking->get_spaces();
            $paypal_vars['amount_'.$count] = round($price,2);
            $count++;
        }
    }
    //calculate discounts, if any:
    $discount = $EM_Booking->get_price_discounts_amount('pre') + $EM_Booking->get_price_discounts_amount('post');
    if( $discount > 0 ){
        $paypal_vars['discount_amount_cart'] = $discount;
    }
    return apply_filters('em_gateway_paypal_get_paypal_vars', $paypal_vars, $EM_Booking, $this);
}
1

There are 1 best solutions below

0
On

Got it sorted.

Not sure if it'll apply to anyone in the future, but changing the line from:

'amount' => $price(),

to

'amount' => $EM_Booking->get_price(),

did the job nicely!