Skip to content

Instantly share code, notes, and snippets.

@imanilchaudhari
Last active April 16, 2019 10:39
Show Gist options
  • Save imanilchaudhari/647be3215f41277636a9687e6b76122c to your computer and use it in GitHub Desktop.
Save imanilchaudhari/647be3215f41277636a9687e6b76122c to your computer and use it in GitHub Desktop.
<?php
public function actionProcess()
{
$customer = Yii::$app->user->identity;
$address = Yii::$app->session->get('address');
$apiContext = Yii::$app->paypal->_apiContext;
// set shipping address details
$shipping_address = new ShippingAddress();
$shipping_address->setRecipientName($address->fullname);
$shipping_address->setLine1($address->address_1);
$shipping_address->setLine2($address->address_2);
$shipping_address->setPostalCode($address->post_code);
$shipping_address->setCity($address->city);
$shipping_address->setState($address->statemodel->paypal_name);
$shipping_address->setCountryCode('IN');
// set payerinfo details
$payerinfo = new PayerInfo();
$payerinfo->setEmail($customer->email);
$payerinfo->setFirstName($customer->first_name);
$payerinfo->setLastName($customer->last_name);
// set payer details
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$payer->setPayerInfo($payerinfo);
$items = [];
$order = Yii::$app->session->get('orders_temp');
$orders_temp = OrdersTemp::find()->where(['order_id' => $order->order_id])->one();
OrderProductsTemp::updateAll(['status_id' => Constant::PAYMENT_ATTEMPTED], 'order_id =' . $orders_temp->order_id);
$total = 0;
foreach ($orders_temp->getOrderProductsTemp()->all() as $opt) {
$total += $opt->unit_price * $opt->quantity;
$item = new Item();
$item->setName($opt->description)->setCurrency('INR')->setQuantity($opt->quantity)->setPrice($opt->unit_price);
$items[] = $item;
}
// set item list details
$itemList = new ItemList();
$itemList->setItems($items);
$itemList->setShippingAddress($shipping_address);
$itemList->setShippingPhoneNumber($address->mobile);
// Lets you specify a payment amount.
// You can also specify additional details
// such as shipping, tax.
$amount = new Amount();
$amount->setCurrency('INR')
->setTotal($total);
// A transaction defines the contract of a
// payment - what is the payment for and who
// is fulfilling it.
$transaction = new \PayPal\Api\Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription('Product Lists')
->setInvoiceNumber(uniqid());
// Set the urls that the buyer must be redirected to after
// payment approval/ cancellation.
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl(Url::to(['/payment/paypal/confirm'], true));
$redirectUrls->setCancelUrl(Url::to(['/payment/paypal/cancel'], true));
// A Payment Resource; create one using
// the above types and intent set to 'sale'
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions([$transaction]);
if (Yii::$app->paypal->config['mode'] == 'sandbox') {
$payment->setExperienceProfileId('XP-NXGL-DQH7-T7LP-F6H3');
} else {
$payment->setExperienceProfileId('XP-3DA8-HAQH-DD9C-BUP2');
}
// For Sample Purposes Only.
$request = clone $payment;
// Create a payment by calling the 'create' method
// passing it a valid apiContext.
// The return object contains the state and the
// url to which the buyer must be redirected to
// for payment approval
try {
$payment->create($apiContext);
$approvalLink = $payment->getApprovalLink();
$approvalUrl = $approvalLink . '&locale.x=en_IN&country.x=IN';
return $this->redirect($approvalUrl);
} catch (\PayPal\Exception\PayPalConnectionException $ex) {
Yii::debug($ex->getMessage(), 'tranx');
Yii::$app->session->setFlash('error', $ex->getMessage());
if ($ex->getData() == 'INSTRUMENT_DECLINED') {
return $this->redirect($approvalUrl);
}
} catch (\Exception $ex) {
Yii::debug($ex->getMessage(), 'tranx');
Yii::$app->session->setFlash('error', $ex->getMessage());
}
return $this->redirect(['/payment/fail']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment