Thứ Năm, 24 tháng 4, 2014

PayPal Express Checkout [Creating a Simple Store]

PayPal Express Checkout allows sellertreceive credit card / PayPal payments on their websites bredirecting them tPayPal Express Checkout for securweb payment and returning them back to the merchant's website once the transaction is completed.

The workflow is explained as follows:

1.       Customer on the Shopping Cart page chooses to pay by PayPal Express Checkout;
the merchancalls the SetExpressCheckout API call and gets the paymentoken.
2.       Using the Paymentoken, the customer is redirected to the PayPal Express Checkout login page; here the customer can enter his/her PayPal login information or get a nePayPal account.
3.       On the next page, the customer is presented with a Review option to review the payment information before proceeding to continue the checkout with the merchant.
4.       Now the customer is redirected back to the merchant page; the merchant then calls the GetExpressCheckoutDetails API call and gets the customer information. The customer reviews the order and confirms the order. The merchant then completes the paymenrequest using the DoExpressCheckoutPayment API call.
5.       The customer is shown the transaction results along with the order summary.


Preform the following stepfor accepting payments using PayPal:

1.       Now add a button on the Shopping Cart page (optionally with Checkout by PayPal Image). This button should link to the paypalExpressCheckoutAction() function.
2.       Add a method in the storcontroller which will be used tgenerate the PayPal request:
protected function getPaypalRequest()
{
$config  = $this->getServiceLocator()->get('config');
$paypalConfig = new \SpeckPaypal\Element\Config(
$config['speck-paypal-api']);

$adapter = new \Zend\Http\Client\Adapter\Curl();
$adapter->setOptions(array( 'curloptions' => array(
CURLOPT_SSL_VERIFYPEER => false,
)
));

$client = new \Zend\Http\Client;
$client->setMethod('POST');
$client->setAdapter($adapter);

$paypalRequest = new \SpeckPaypal\Service\Request;
$paypalRequest->setClient($client);
$paypalRequest->setConfig($paypalConfig);

return $paypalRequest;
}


3.       Modify the paypalExpressCheckoutAction() function to send the order information tPayPal and redirect the user tPayPal Express Checkout:
public function paypalExpressCheckoutAction()
{
$request = $this->getRequest();
$orderId = $request->getPost()->get('orderId');

$orderTable = $this->getServiceLocator()-
>get('StoreOrdersTable');
$order = $orderTable->getOrder($orderId);

$paypalRequest = $this->getPaypalRequest();

$paymentDetails = new \SpeckPaypal\Element\PaymentDetails (array('amt' => $order->total
));
$express = new \SpeckPaypal\Request\SetExpressCheckout( array('paymentDetails' => $paymentDetails)
);

$express->setReturnUrl(
$express->setCancelUrl(

// Send Order information to PayPal
$response = $paypalRequest->send($express);
$token = $response->getToken();

$paypalSession = new \Zend\Session\Container('paypal');
$paypalSession->tokenId = $token;
$paypalSession->orderId = $orderId;


// Redirect user to PayPal Express Checkout
$this->redirect()->toUrl('https://www.sandbox.paypal.com/ webscr?cmd=_express-checkout&token=' . $token);
}


4.       Add a method to handle successful payment from Express Checkout— paymentConfirmAction(); this method will capture the payment information from PayPal, confirm the payment, and then update the orderstatus in our system using the code as shown in the following list:
        Capture payment information from PayPal:
// To capture Payer Information from PayPal
$paypalSession = new \Zend\Session\Container('paypal');
$paypalRequest = $this->getPaypalRequest();

$expressCheckoutInfo =
new \SpeckPaypal\Request\ GetExpressCheckoutDetails();
$expressCheckoutInfo->setToken($paypalSession->tokenId);
$response = $paypalRequest->send($expressCheckoutInfo);

        Confirm order with PayPal:
//To capture express payment
$orderTable = $this->getServiceLocator()-
>get('StoreOrdersTable');
$order = $orderTable->getOrder($paypalSession->orderId);
$paymentDetails = new \SpeckPaypal\Element\ PaymentDetails(array(
'amt' => $order->total
));

$token = $response->getToken();
$payerId = $response->getPayerId();

$captureExpress = new \SpeckPaypal\Request\ DoExpressCheckoutPayment(
array(
'token'             => $token, 'payerId'    => $payerId, 'paymentDetails'    => $paymentDetails
));
$confirmPaymentResponse = $paypalRequest-
>send($captureExpress);

        Save order with updated shipping/billing information:
//To Save Order Information
$order->first_name = $response->getFirstName();
$order->last_name = $response->getLastName();
$order->ship_to_street = $response->getShipToStreet();


$order->ship_to_city = $response->getShipToCity();
$order->ship_to_state = $response->getShipToState();
$order->ship_to_zip = $response->getShipToZip();
$order->email = $response->getEmail();
$order->store_order_id = $paypalSession->orderId;
$order->status = 'completed';
$orderTable->saveOrder($order);

5.     Finally add a method to handle failed payment from Express Checkout—
paymentCancelAction():
public function paymentCancelAction()
{
$paypalSession = new \Zend\Session\Container('paypal');

$storeOrdersTG = $this->getServiceLocator()
->get('StoreOrdersTableGateway');
$storeOrdersTG->update(
array('status' => 'cancelled'), array('id' => $paypalSession->orderId));
$paypalSession->orderId = NULL;
$paypalSession->tokenId = NULL;
$view = new ViewModel(); return $view;
}

6.       Now log in thttps://developer.paypal.com again.
7.       Generate a new sandbox account of type PERSONAL.
8.       Now access the store and trto purchase using the newly created Sandbox account.
The final store should look like the following screenshot:


After choosing the checkout from the Shopping Cart page, you will be redirected tthe Pay with mPayPal accounlogin page as shown in the following screenshot:


A screenshot of the PayPal Express Checkout's order reviewing page is shown in the following screenshot; this page is used treview the payment that is being made to the merchant from the customer's PayPal account:


Once the order is successfully placed, the user is redirected to the order confirmation page  as shown in the following screenshot:


9.       Now log in tthe Sandbox sitfor the merchant accounto see if the payments are credited:


What just happened?
We just used PayPal Express Checkout treceive payments in our web application and complete the simple store application. As you can see, the PayPal API makes it relatively easto set up the paymengateway.
In your nextask, make use of the DoDirectPayment API call to directly make a payment on the website without having tredirect the user to the PayPal website and back again.

Q1. Which of the following methods is used to send the initial payment information for PayPal redirection?

1.       RedirectExpressCheckout
2.       SetExpressCheckout
3.       GetExpressCheckoutDetails
4.       DoExpressCheckoutPayment

Q2. Which of the following fields is needed for requesting payment information from PayPal?

1.       token
2.       payerId
3.       paymentDetails
4.       orderID


Summary


In this chapter we have learned the basics of setting up a simple store online and trying to receive payments using PayPal. As you can see from the previous example, ZendFramework's use of modules simplifies applicationdevelopment by giving developers the ability to download and install external third-party modules based on their integration needs. In the next chapter, we will be working on HTML5 development with Zend Framework 2.0.

Không có nhận xét nào:

Đăng nhận xét