500 INTERNAL_SERVICE_ERROR when I get Sale object

EvansKim
New Community Member


 

 Here is my code.

<?php
namespace App\Modules\Admin\Models\PaymentGateway;

use App\Mail\ErrorAlarm;
use App\Modules\Admin\Models\Order;
use App\Modules\Admin\Models\Payment;
use Illuminate\Http\Request;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Payer;
use PayPal\Api\PaymentExecution;
use PayPal\Api\RedirectUrls;
use PayPal\Api\RefundRequest;
use PayPal\Api\Sale;
use PayPal\Api\Transaction;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Exception\PayPalConnectionException;

class PaypalGateway implements PaymentGatewayInterface
{
protected $config;
public function __construct()
{
if( env("APP_ENV") == "production" ){
$client_id = config('services.paypal.client_id');
$secret = config('services.paypal.client_secret');
$EndPoint = 'https://api.paypal.com';
$mode = 'live';
}else{
$client_id = config('services.paypal_sandbox.client_id');
$secret = config('services.paypal_sandbox.client_secret');
$EndPoint = 'https://api.sandbox.paypal.com';
$mode = 'sandbox';
}
$apiSetup = new \PayPal\Rest\ApiContext(
new OAuthTokenCredential(
$client_id,
$secret
)
);
$apiSetup->setConfig(array(
'mode' => $mode,
'service.EndPoint' => $EndPoint,
'log.LogEnabled' => true,
'log.FileName' => storage_path('logs/paypal.log'),
'log.LogLevel' => 'DEBUG'
));
$this->config = $apiSetup;
}

/**
* @param Order $order
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function redirect(Order $order)
{
$payer = new Payer();
$payer->setPaymentMethod('paypal');

$amount = new Amount();
$amount->setCurrency('USD');
$amount->setTotal( $order->unpaid_total );

// This is the simple way,
// you can alternatively describe everything in the order separately;
// Reference the PayPal PHP REST SDK for details.

$details = new Details();
$details->setShipping( $order->shipping_cost );
$details->setTax( $order->tax_amount );
$details->setSubtotal( $order->products_amount - $order->discount );
$amount->setDetails( $details );

$transaction = new Transaction();
$transaction->setAmount( $amount );
$transaction->setInvoiceNumber( $order->order_code );
$transaction->setDescription( 'Candleworks Home Fragrance DIY' );

$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl( route("shop.order.payment.callback") );
$redirectUrls->setCancelUrl( route("shop.order.show", $order->id) );

$payment = new \PayPal\Api\Payment();
$payment->setIntent( 'sale' );
$payment->setPayer( $payer );
$payment->setRedirectUrls( $redirectUrls );
$payment->setTransactions( array($transaction) );

$response = $payment->create( $this->config );
$redirectUrl = $response->getApprovalLink();

\Session::put(['payment_order_id'=> $order->id]);

return redirect( $redirectUrl );
}

/**
* @param Request $request
* @param Order $order
* @return \Illuminate\Http\RedirectResponse
*/
public function callback(Request $request, Order $order)
{
$id = $request->input('paymentId');
$payment = \PayPal\Api\Payment::get($id, $this->config );

$payer_id = $request->input('PayerID');

$execution = new PaymentExecution();
$execution->setPayerId($payer_id);

try{
$resultPayment = $payment->execute( $execution, $this->config );
}catch ( PayPalConnectionException $e ){
$json = json_decode( $e->getData() );
alert()->error("Please try later. Paypal Server occurs some error.".$json->message ,"Try again");

return redirect()->route("shop.order.show", $order->id);
}

if( $resultPayment->state == "approved" ) {
$transections = $resultPayment->getTransactions();

// Create purchase record in the database
$order_code = $transections[0]->invoice_number;
$related = $transections[0]->getRelatedResources();
$sale = $related[0]->getSale();
$saleId = $sale->getId();
Payment::create([
'order_id' => $order->id,
'type' => 'paypal',
'stripe_id' => $payer_id,
'transaction_code' => $saleId,
'amount' => $transections[0]->getAmount()->getTotal(),
'memo' => 'Paypal Payment',
]);
alert()->success("Payment Complete.", "Thank you");
}
return redirect()->route("shop.order.show", $order->id);
}

/**
* @param Payment $payment
* @return bool
*/
public function refund(Payment $payment)
{
$sale = Sale::get($payment->transaction_code, $this->config);

dump($sale);

}
}

when i call function refund(), this error occur. I have no idea. Please help me.

[20-11-2017 16:22:18] PayPal\Core\PayPalHttpConnection : INFO: POST https://api.sandbox.paypal.com/v1/oauth2/token
[20-11-2017 16:22:23] PayPal\Core\PayPalHttpConnection : DEBUG: Request Headers    : POST /v1/oauth2/token HTTP/1.1, Host: api.sandbox.paypal.com, User-Agent: PayPalSDK/PayPal-PHP-SDK 1.12.0 (platform-ver=7.1.8; bit=32; os=Windows_NT_10.0; machine=i586; crypto-lib-ver=1.0.2k; curl=7.54.1), Authorization: Basic QVFscFF3R0tXclhJR3MyeXVSbzdwUzBrbklkUU56M1lpM3ZGek4xcFVlbkZaNzdyajVsSHlzUGZfdzhiY2NsTmdIY0tOUXhZc0V3d3gtQkU6RUduTFlpMmNaNTBrTFEzdnU3TmozdHIwSWZqVlNzR3hmMXFkVUV6Um9QWUNQLUFpMnZ5cFpiUkMxd3pEckpaanRZeVEycUVaS2FkNENUazE=, Accept: */*, Content-Length: 29, Content-Type: application/x-www-form-urlencoded, , 
[20-11-2017 16:22:23] PayPal\Core\PayPalHttpConnection : DEBUG: Request Data      : grant_type=client_credentials
--------------------------------------------------------------------------------------------------------------------------------

[20-11-2017 16:22:23] PayPal\Core\PayPalHttpConnection : INFO: Response Status     : 200
[20-11-2017 16:22:23] PayPal\Core\PayPalHttpConnection : DEBUG: Response Headers   : Date: Mon, 20 Nov 2017 07:22:19 GMT, Server: Apache, paypal-debug-id: 77188554ae35, X-PAYPAL-TOKEN-SERVICE: IAAS, HTTP_X_PP_AZ_LOCATOR: sandbox.slc, Paypal-Debug-Id: 77188554ae35, Set-Cookie: X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00:00:01 GMT, Vary: Authorization, Content-Length: 550, Connection: close, Content-Type: application/json, 
[20-11-2017 16:22:23] PayPal\Core\PayPalHttpConnection : DEBUG: Response Data  : {"scope":"https://uri.paypal.com/services/subscriptions https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card https://uri.paypal.com/services/applications/webhooks openid https://uri.paypal.com/payments/payouts https://api.paypal.com/v1/vault/credit-card/.*","nonce":"2017-11-20T07:01:41Zf0xYkUvI5oXXj5wm0lZv6GSc6Me6xGNxNQMDyignZNI","access_token":"A21AAF59yHvnI6RyQg_PEd9EkKpOX0PXBCt_QXyK_V53d0XYDkv9gZ7DdC5goNN2XOoixvbfuHA4-upxBsIjtXTk5mCH3fTzA","token_type":"Bearer","app_id":"APP-80W284485P519543T","expires_in":31158}

================================================================================================================================

[20-11-2017 16:22:23] PayPal\Core\PayPalHttpConnection : INFO: GET https://api.sandbox.paypal.com/v1/payments/sale/2VW53071YA416114V
[20-11-2017 16:22:24] PayPal\Core\PayPalHttpConnection : DEBUG: Request Headers    : GET /v1/payments/sale/2VW53071YA416114V HTTP/1.1, Host: api.sandbox.paypal.com, Accept: */*, Content-Type: application/json, User-Agent: PayPalSDK/PayPal-PHP-SDK 1.12.0 (platform-ver=7.1.8; bit=32; os=Windows_NT_10.0; machine=i586; crypto-lib-ver=1.0.2k; curl=7.54.1), Authorization: Bearer A21AAF59yHvnI6RyQg_PEd9EkKpOX0PXBCt_QXyK_V53d0XYDkv9gZ7DdC5goNN2XOoixvbfuHA4-upxBsIjtXTk5mCH3fTzA, , 
[20-11-2017 16:22:24] PayPal\Core\PayPalHttpConnection : DEBUG: No Request Payload
--------------------------------------------------------------------------------------------------------------------------------

[20-11-2017 16:22:24] PayPal\Core\PayPalHttpConnection : INFO: Response Status     : 500
[20-11-2017 16:22:24] PayPal\Core\PayPalHttpConnection : DEBUG: Response Headers   : Date: Mon, 20 Nov 2017 07:22:24 GMT, Server: Apache, paypal-debug-id: 7e86cba31f260, Content-Language: *, X-SLR-RETRY: 500, X-SLR-RETRY-API: /v1/payments/sale/{id}, Connection: close, HTTP_X_PP_AZ_LOCATOR: sandbox.slc, Paypal-Debug-Id: 7e86cba31f260, Set-Cookie: X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00:00:01 GMT, Vary: Authorization, Content-Length: 187, Content-Type: application/json, 
[20-11-2017 16:22:24] PayPal\Core\PayPalHttpConnection : ERROR: Got Http response code 500 when accessing https://api.sandbox.paypal.com/v1/payments/sale/2VW53071YA416114V. {"name":"INTERNAL_SERVICE_ERROR","message":"An internal service error has occurred","information_link":"https://developer.paypal.com/docs/api/payments/#errors","debug_id":"7e86cba31f260"}
[20-11-2017 16:22:24] PayPal\Core\PayPalHttpConnection : DEBUG: 

================================================================================================================================
Login to Me Too
0 REPLIES 0

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.