无法从paypal支付speckPaypal zf2获取令牌
问题描述:
i'm trying to use the module speckpaypal for zf2 , but can't get the token , here is my code :
public function passTransactionAction(){
$config = $this->getServiceLocator()->get('Config');
$paypalConfig = new \SpeckPaypal\Element\Config($config);
//Then, set up http client using curl libraries provided by Zf2:
$client = new \Zend\Http\Client;
$client->setMethod('POST');
$client->setAdapter(new \Zend\Http\Client\Adapter\Curl);
$paypalRequest = new \SpeckPaypal\Service\Request;
$paypalRequest->setClient($client);
$paypalRequest->setConfig($paypalConfig);
$paymentDetails = new \SpeckPaypal\Element\PaymentDetails(array(
'amt' => '20.00'
));
$express = new \SpeckPaypal\Request\SetExpressCheckout(array('paymentDetails' => $paymentDetails));
$express->setReturnUrl('http://project.local/return');
$express->setCancelUrl('http://project.local/cancel');
$response = $paypalRequest->send($express);
echo $response->isSuccess();
$token = $response->getToken();
echo "---------------------- token : $token----------------------";
$this->redirect()->toUrl('https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token='.$token);
}
i had set the params in the config file ( a test account sandbox.paypal ), and when showing the $response value , i get this :
SpeckPaypal\Response\Response Object ( [_rawResponse:protected] => [_success:protected] => [_errors:protected] => Array ( [0] => ACK key not found in response. [1] => Configuration is not valid. ) [_response:protected] => [_multiFieldMap:protected] => Array ( [ERRORS] => Array ( [0] => LONGMESSAGE [1] => SEVERITYCODE [2] => SHORTMESSAGE [3] => ERRORCODE ) [FILTERS] => Array ( [0] => FMFfilterID [1] => FMFfilterNAME ) [ITEMS] => Array ( [0] => NAME [1] => DESC [2] => AMT [3] => NUMBER [4] => QTY [5] => TAXAMT [6] => ITEMWEIGHTVALUE [7] => ITEMWEIGHTUNIT [8] => ITEMLENGTHVALUE [9] => ITEMLENGTHUNIT [10] => ITEMWIDTHVALUE [11] => ITEMWIDTHUNIT [12] => ITEMHEIGHTVALUE [13] => ITEMHEIGHTUNIT [14] => ITEMCATEGORY ) ) )
Any idea from where comes the bug ? Thanks.
答
here is the correct way to implement SpeckPaypal ZF2 for ExpressCheckOut , i followed this tuto : http://phpcantho24h.blogspot.com/2014/04/paypal-express-checkout-creating-simple.html
// fonction permettant de generer le paypal request :
protected function getPaypalRequest() {
// recuperer la configuration du speckPaypal ( depuis son fichier de configuration speck-paypal.local.php dans config/autoload/ )
$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;
}
//Fonction qui va envoyer les informations de payment et rediriger le client vers PayPal Express Checkout:
public function paypalExpressCheckoutAction() {
// recuperer les infos depuis le form :
$request = $this->getRequest();
$montant = $request->getPost('montant');
$paypalRequest = $this->getPaypalRequest();
$fk_annonceur = $request->getPost('annonceur');
// preparer le paymentDetails :
$paymentDetails = new \SpeckPaypal\Element\PaymentDetails(array('amt' => $montant));
$express = new \SpeckPaypal\Request\SetExpressCheckout(array('paymentDetails' => $paymentDetails));
// définir les liens de retour en cas de Success ( ReturnUrl ) et de Annulation (CancelUrl )
$express->setReturnUrl('/application/client/paymentConfirmh');
$express->setCancelUrl('/application/client/paymentCancel');
// Envoyer les infos à PayPal
$response = $paypalRequest->send($express);
// Récuperer le token generé :
$token = $response->getToken();
// Créer une variable session contenant les infos utiles ( à utiliser plus tard dans la fonction de confirm de payment ) :
$paypalSession = new \Zend\Session\Container('paypal');
$paypalSession->tokenId = $token;
$paypalSession->montant = $montant;
$paypalSession->client= $client;
// Return le lien pour rediriger l'utilisateur vers Paypal Express Checkout :
$this->redirect()->toUrl('https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=' . $token);
}
// fonction en cas de success de payment from Express Checkout , recuperer les infos from Paypal , enregistrer les données dans prepaiement :
public function paymentConfirmAction() {
$this->layout('layout/anlayout');
// Recuperer payment information depuis PayPal:
// Recuperer Payer Information depuis PayPal
$paypalSession = new \Zend\Session\Container('paypal');
$paypalRequest = $this->getPaypalRequest();
$expressCheckoutInfo = new \SpeckPaypal\Request\ GetExpressCheckoutDetails();
$expressCheckoutInfo->setToken($paypalSession->tokenId);
$response = $paypalRequest->send($expressCheckoutInfo);
$montant = $paypalSession->montant;
$paymentDetails = new \SpeckPaypal\Element\PaymentDetails(array('amt' => $montant));
$token = $response->getToken();
$payerId = $response->getPayerId();
$captureExpress = new \SpeckPaypal\Request\ DoExpressCheckoutPayment(
array(
'token' => $token, 'payerId' => $payerId, 'paymentDetails' => $paymentDetails
));
$confirmPaymentResponse = $paypalRequest->send($captureExpress);
// Enregistrer le prepaiement :
$prepaiementObject = new \Application\Entity\Prepaiement();
$prepaiementObject->setClient($client);
$prepaiementObject->setPaymentamount($montant);
$prepaiementObject->setPaymentapi('Paypal');
$prepaiementObject->setPaymentdate(new \DateTime("now"));
$prepaiementObject->setCorrelatinid($response->getCorrelationId());
$this->getEntityManager()->persist($prepaiementObject);
$this->getEntityManager()->flush();
// recuperer les infos de payer :
/* $first_name = $response->getFirstName();
$last_name = $response->getLastName();
$ship_to_street = $response->getShipToStreet();
$ship_to_city = $response->getShipToCity();
$ship_to_state = $response->getShipToState();
$ship_to_zip = $response->getShipToZip();
$email = $response->getEmail();*/
}
// Fonction pour traiter payment en cas d'annulation depuis Express Checkout :
public function paymentCancelAction() {
$this->layout('layout/anlayout');
$paypalSession = new \Zend\Session\Container('paypal');
$view = new ViewModel();
return $view;
}
Hope it helps.