aurelien75000 aurelien75000 - 7 months ago 44
PHP Question

Paypal/Laravel can't make a Live payment

I've followed this tutorial to implement payment using Paypal services on my website: https://www.youtube.com/watch?v=q5Xb5r4MUB8

But when i want to flip from SandBox mode to Live mode (which is the real payment) all my transactions goes to the SandBox history (you can check it in your Paypal account).

Here is the code of the function "store()" that do the payment:

public function store(Request $request)
{

// ### CreditCard
$card = Paypalpayment::creditCard();
$card->setType("visa")
->setNumber("Some_Numbers")
->setExpireMonth("05")
->setExpireYear("2017")
->setCvv2("smth")
->setFirstName("MyName")
->setLastName("MyLastName");


$fi = Paypalpayment::fundingInstrument();
$fi->setCreditCard($card);

$payer = Paypalpayment::payer();
$payer->setPaymentMethod("credit_card")
->setFundingInstruments(array($fi));


//Payment Amount
$amount = Paypalpayment::amount();
$amount->setCurrency("EUR")
->setTotal("3");


$transaction = Paypalpayment::transaction();
$transaction->setAmount($amount)
->setDescription("Payment description")
->setInvoiceNumber(uniqid());

// ### Payment
// A Payment Resource; create one using
// the above types and intent as 'sale'

$payment = Paypalpayment::payment();

$payment->setIntent("sale")
->setPayer($payer)
->setTransactions(array($transaction));

try {
$payment->create($this->_apiContext);
} catch (\PPConnectionException $ex) {
return "Exception: " . $ex->getMessage() . PHP_EOL;
exit(1);
}

dd($payment);


}

Answer

Look what $this->_apiContext contains. There is a mode that needs to be set to live to make it work.

If not found, you can create apiContext object as shown here:

$apiContext = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
        'Live ClientId',     // ClientID
        'Live Client Secret'      // ClientSecret
    )
);

$apiContext->setConfig(
  array(
    'mode' => 'live',
    'log.LogEnabled' => true,
    'log.FileName' => 'PayPal.log',
    'log.LogLevel' => 'FINE'
  )
);

and use $apiContext instead when you call $payment->create();