How can I send the shipping address details when paying with PayPal using Omnipay?
I'm getting a given shipping address from a form. I need to transfer this data to the payment system and then extract it for writing to the database. Please tell me how to do this, I'm new to programming.
class PaymentController extends BaseController { private $gateway; protected $paymentService;
public function __construct(PaymentService $service)
{
$this->paymentService = $service;
$this->gateway = Omnipay::create('PayPal_Rest');
$this->gateway->setClientId(env('PAYPAL_CLIENT_ID'));
$this->gateway->setSecret(env('PAYPAL_CLIENT_SECRET'));
$this->gateway->setTestMode(true);
}
public function checkout(Request $objRequest)
{
$payment_method = $objRequest->input('payment_method');
if ($payment_method == 'PayPal') {
$deliveryAddress = [
'recipient_name' => $objRequest->input('name'),
'line1' => $objRequest->input('address_line_1'),
'line2' => $objRequest->input('address_line_2') ?? null,
'city' => $objRequest->input('city'),
'state' => $objRequest->input('state'),
'postal_code' => $objRequest->input('zip'),
'country_code' => $objRequest->input('country_code'),
];
$objHelper = new CartHelper();
$objCart = $objHelper->getObjCart(
$objRequest->capture()->getSchemeAndHttpHost(),
$objRequest->capture()->userAgent(),
$objRequest->capture()->ip(),
auth()->user()
);
$totalAmount = $objHelper->getTotalCartValue($objCart);
try {
$response = $this->gateway->purchase(array(
'amount' => $totalAmount,
'currency' => env('PAYPAL_CURRENCY'),
'returnUrl' => url('/paypal/success'),
'cancelUrl' => url('/paypal/error'),
))->send();
if ($response->isRedirect()) {
$response->redirect();
} else {
$strErrorMess['message'] = $response->getMessage();
return view('products.errors', $strErrorMess);
}
} catch (Exception $th) {
$strErrorMess['message'] = $th->getMessage();
return view('products.errors', $strErrorMess);
}
} else if ($payment_method == 'Stripe') {
dd($objRequest->all());
}
}
public function successPayPal(Request $objRequest) { $objUser = auth()->user(); if ($objRequest->input('paymentId') && $objRequest->input('PayerID')) { $transaction = $this->gateway->completePurchase(array( 'PayerID' => $objRequest->input('PayerID'), 'transactionReference' => $objRequest->input('paymentId') ));
$response = $transaction->send();
if ($response->isSuccessful()) {
$arrDataPayment = $response->getData();
dd($arrDataPayment);
$objHelper = new CartHelper();
$objCart = $objHelper->getObjCart(
$objRequest->capture()->getSchemeAndHttpHost(),
$objRequest->capture()->userAgent(),
$objRequest->capture()->ip(),
auth()->user()
);
$addNotePayment = $this->paymentService->addNotePaymentPayPal(
$arrDataPayment,
$objCart
);
$stringD['message'] = "Your payment has been successfully processed. Wait for information about the delivery of goods.";
return view('products.success')->with($stringD);
} else {
$strErrorMess['message'] = $response->getMessage();
return view('products.errors', $strErrorMess);
}
} else {
$strErrorMess['message'] = "Payment declined!";
return view('products.errors', $strErrorMess);
}
}
public function errorPayPal()
{
$strErrorMess['message'] = "User declined the payment!";
return view('products.errors', $strErrorMess);
}
Please or to participate in this conversation.