0

I am trying to submit a payment against Paypal Sandbox, using the Omnipay Paypal module. I think that I have everything set up in terms of sandbox account, and my transaction is working if I don't specify a card as payment method. I am correctly redirected to the Paypal sandbox, where I need to enter my test credentials before I can submit my payment. Then I am redirected back to my original web site, where I can store the confirmed payment details.

However, when I want to use a credit card, I get an error message saying : Payee account is invalid.

Here is my controller code :

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Omnipay\Omnipay;
use Omnipay\Common\CreditCard;
use App\Payment;

class PaymentController extends Controller
{

    private $gateway;

    public function __construct()
    {
        $this->gateway = Omnipay::create('PayPal_Rest');
        $this->gateway->setClientId(env('PAYPAL_CLIENT_ID'));
        $this->gateway->setSecret(env('PAYPAL_CLIENT_SECRET'));
        $this->gateway->setTestMode(env('PAYPAL_SANDBOX_MODE'));;
    }

    /**
     * Call a view.
     */
    public function index()
    {
        return view('payment');
    }

    /**
     * Initiate a payment on PayPal.
     *
     * @param  \Illuminate\Http\Request  $request
     */
    public function charge(Request $request)
    {
        if($request->input('submit'))
        {
            $card = new CreditCard(array(
                'number' => '<my test credit card number>',
                'expiryMonth'           => '03',
                'expiryYear'            => '2024',
                'cvv'                   => '<my ccv>',
            ));
            try {
                $response = $this->gateway->purchase(array(
                    'amount'    => $request->input('amount'),
                    'currency'  => env('PAYPAL_CURRENCY'),
                    'returnUrl' => url('success'),
                    'cancelUrl' => url('error'),
                    'card'      => $card
                ))->send();

                if ($response->isRedirect()) {
                    $response->redirect(); // this will automatically forward the customer
                } else {
                    // not successful
                    return $response->getMessage();
                }
            } catch(Exception $e) {
                return $e->getMessage();
            }
        }
    }

    /**
     * Charge a payment and store the transaction.
     *
     * @param  \Illuminate\Http\Request  $request
     */
    public function success(Request $request)
    {
        // Once the transaction has been approved, we need to complete it.
        if ($request->input('paymentId') && $request->input('PayerID'))
        {
            $transaction = $this->gateway->completePurchase(array(
                'payer_id'             => $request->input('PayerID'),
                'transactionReference' => $request->input('paymentId'),
            ));
            $response = $transaction->send();

            if ($response->isSuccessful())
            {
                // The customer has successfully paid.
                $arr_body = $response->getData();

                // Insert transaction data into the database
                $payment = new Payment;
                $payment->payment_id = $arr_body['id'];
                $payment->payer_id = $arr_body['payer']['payer_info']['payer_id'];
                $payment->payer_email = $arr_body['payer']['payer_info']['email'];
                $payment->amount = $arr_body['transactions'][0]['amount']['total'];
                $payment->currency = env('PAYPAL_CURRENCY');
                $payment->payment_status = $arr_body['state'];
                $payment->save();

                return "Payment is successful. Your transaction id is: ". $arr_body['id'];
            } else {
                return $response->getMessage();
            }
        } else {
            return 'Transaction is declined';
        }
    }

    /**
     * Error Handling.
     */
    public function error()
    {
        return 'User cancelled the payment.';
    }
}

Can you see where my problem could be ?

Adam Dulson
  • 283
  • 5
  • 16

0 Answers0