php - latam - Implementar pagos recurrentes de Payum/Laravel.
payu sdk laravel (2)
Tengo algunos problemas tratando de hacer que esto funcione, he implementado el checkout express (o parece ser) con éxito, pero también mi sistema necesita una opción de suscripción, siguiendo este example .
Ahora, mi problema es que en Laravel no puedes simplemente poner algunos archivos aleatorios, así que estoy tratando de hacerlo de la manera correcta, lamentablemente, no hay documentación de las clases y métodos incluidos en la biblioteca.
He creado algunas funciones dentro de los controladores (no sé si esta es la manera correcta), el problema que estoy enfrentando ahora es tratar de crear un Pago Recurrente () para aplicar la cantidad deseada del pago recurrente, es el paso final, supongo.
Gracias por tu ayuda
app / controllers / PaypalController.php
public function prepareExpressCheckout(){ $storage = $this->getPayum()->getStorage(''Payum/Core/Model/ArrayObject''); $details = $storage->createModel(); $details[''PAYMENTREQUEST_0_CURRENCYCODE''] = ''USD''; $details[''PAYMENTREQUEST_0_AMT''] = 1.23; $storage->updateModel($details); $captureToken = $this->getTokenFactory()->createCaptureToken(''paypal_es'', $details, ''payment_done''); $details[''RETURNURL''] = $captureToken->getTargetUrl(); $details[''CANCELURL''] = $captureToken->getTargetUrl(); $storage->updateModel($details); return /Redirect::to($captureToken->getTargetUrl()); } public function prepareSubscribe(){ $storage = $this->getPayum()->getStorage(''Payum/Core/Model/ArrayObject''); $details = $storage->createModel(); $details[''PAYMENTREQUEST_0_AMT''] = 0; $details[''L_BILLINGTYPE0''] = Api::BILLINGTYPE_RECURRING_PAYMENTS; $details[''L_BILLINGAGREEMENTDESCRIPTION0''] = "Suscripción por X meses"; $details[''NOSHIPPING''] = 1; $storage->updateModel($details); $captureToken = $this->getTokenFactory()->createCaptureToken(''paypal_es'', $details, ''payment_done''); $storage->updateModel($details); return /Redirect::to($captureToken->getTargetUrl()); } public function createRecurringPayment(){ $payum_token = Input::get(''payum_token''); $request = /App::make(''request''); $request->attributes->set(''payum_token'', $payum_token); $token = ($request); //$this->invalidate($token); $agreementStatus = new GetHumanStatus($token); $payment->execute($agreementStatus); if (!$agreementStatus->isSuccess()) { header(''HTTP/1.1 400 Bad Request'', true, 400); exit; } $agreementDetails = $agreementStatus->getModel(); $storage = $this->getPayum()->getStorage(''Payum/Core/Model/ArrayObject''); $recurringPaymentDetails = $storage->createModel(); $recurringPaymentDetails[''TOKEN''] = $agreementDetails[''TOKEN'']; $recurringPaymentDetails[''DESC''] = ''Subscribe to weather forecast for a week. It is 0.05$ per day.''; $recurringPaymentDetails[''EMAIL''] = $agreementDetails[''EMAIL'']; $recurringPaymentDetails[''AMT''] = 0.05; $recurringPaymentDetails[''CURRENCYCODE''] = ''USD''; $recurringPaymentDetails[''BILLINGFREQUENCY''] = 7; $recurringPaymentDetails[''PROFILESTARTDATE''] = date(DATE_ATOM); $recurringPaymentDetails[''BILLINGPERIOD''] = Api::BILLINGPERIOD_DAY; $payment->execute(new CreateRecurringPaymentProfile($recurringPaymentDetails)); $payment->execute(new Sync($recurringPaymentDetails)); $doneToken = $this->createToken(''paypal_es'', $recurringPaymentDetails, ''payment_done''); return /Redirect::to($doneToken->getTargetUrl()); }
app / route.php
Route::get(''/payment'', array(''as'' => ''payment'', ''uses'' => ''PaymentController@payment'')); Route::get(''/payment/done'', array(''as'' => ''payment_done'', ''uses'' => ''PaymentController@done'')); Route::get(''/payment/paypal/express-checkout/prepare'', array(''as'' => ''paypal_es_prepare'', ''uses'' => ''PaypalController@prepareExpressCheckout'')); Route::get(''/payment/paypal/subscribe/prepare'', array(''as'' => ''paypal_re_prepare'', ''uses'' => ''PaypalController@prepareSubscribe'')); Route::get(''/payment/paypal/subscribe/create'', array(''as'' => ''payment_create'', ''uses'' => ''PaypalController@createRecurringPayment''));
En mi proyecto usé la siguiente biblioteca y me ayudó mucho:
https://github.com/amirduran/duranius-paypal-rest-api-php-library
Aquí están algunas características:
- Fácil de instalar - Solo un archivo
- La biblioteca se implementa como clase PHP.
- Es compatible con pagos periódicos. Es compatible con los pagos de ExpressCheckout.
- Todos los métodos API de PayPal disponibles están envueltos en métodos de pertenencia
- Bien documentada
He encontrado el problema. Es con los parámetros que pasamos a la función de crear pagos recurrentes. Aquí están las funciones para la creación de acuerdos y pagos. Debería funcionar bien.
<?php
namespace App/Http/Controllers;
use Payum/Core/Request/GetHumanStatus;
use Payum/LaravelPackage/Controller/PayumController;
use Payum/Paypal/ExpressCheckout/Nvp/Api;
use Payum/Core/Request/Sync;
use Payum/Paypal/ExpressCheckout/Nvp/Request/Api/CreateRecurringPaymentProfile;
use Illuminate/Http/Request;
use App/Http/Requests;
use App/Http/Controllers/Controller;
class PayPalController extends PayumController {
public function prepareSubscribeAgreement() {
$storage = $this->getPayum()->getStorage(''Payum/Core/Model/ArrayObject'');
$details = $storage->create();
$details[''PAYMENTREQUEST_0_AMT''] = 0;
$details[''L_BILLINGTYPE0''] = Api::BILLINGTYPE_RECURRING_PAYMENTS;
$details[''L_BILLINGAGREEMENTDESCRIPTION0''] = "Weather subscription";
//$details[''NOSHIPPING''] = 1;
$storage->update($details);
$captureToken = $this->getPayum()->getTokenFactory()->createCaptureToken(''paypal_ec'', $details, ''paypal_subscribe'');
return /Redirect::to($captureToken->getTargetUrl());
}
public function createSubscribePayment(Request $request) {
$request->attributes->set(''payum_token'', $request->input(''payum_token''));
$token = $this->getPayum()->getHttpRequestVerifier()->verify($request);
$gateway = $this->getPayum()->getGateway($token->getGatewayName());
$agreementStatus = new GetHumanStatus($token);
$gateway->execute($agreementStatus);
if (!$agreementStatus->isCaptured()) {
header(''HTTP/1.1 400 Bad Request'', true, 400);
exit;
}
$agreement = $agreementStatus->getModel();
$storage = $this->getPayum()->getStorage(''Payum/Core/Model/ArrayObject'');
$recurringPayment = $storage->create();
$recurringPayment[''TOKEN''] = $agreement[''TOKEN''];
$recurringPayment[''PAYERID''] = $agreement[''PAYERID''];
$recurringPayment[''PROFILESTARTDATE''] = date(DATE_ATOM);
$recurringPayment[''DESC''] = $agreement[''L_BILLINGAGREEMENTDESCRIPTION0''];
$recurringPayment[''BILLINGPERIOD''] = Api::BILLINGPERIOD_DAY;
$recurringPayment[''BILLINGFREQUENCY''] = 7;
$recurringPayment[''AMT''] = 0.05;
$recurringPayment[''CURRENCYCODE''] = ''USD'';
$recurringPayment[''COUNTRYCODE''] = ''US'';
$recurringPayment[''MAXFAILEDPAYMENTS''] = 3;
$gateway->execute(new CreateRecurringPaymentProfile($recurringPayment));
$gateway->execute(new Sync($recurringPayment));
$captureToken = $this->getPayum()->getTokenFactory()->createCaptureToken(''paypal_ec'', $recurringPayment, ''payment_done'');
return /Redirect::to($captureToken->getTargetUrl());
}
public function done(Request $request) {
/** @var Request $request */
//$request = /App::make(''request'');
$request->attributes->set(''payum_token'', $request->input(''payum_token''));
$token = $this->getPayum()->getHttpRequestVerifier()->verify($request);
$gateway = $this->getPayum()->getGateway($token->getGatewayName());
$gateway->execute($status = new GetHumanStatus($token));
return /Response::json(array(
''status'' => $status->getValue(),
''details'' => iterator_to_array($status->getFirstModel())
));
}
}
Las rutas:
Route::get(''paypal/agreement'', ''PayPalController@prepareSubscribeAgreement'');
Route::get(''paypal/subscribe'', [
''as'' => ''paypal_subscribe'',
''uses'' => ''PayPalController@createSubscribePayment''
]);
Route::get(''paydone'', [
''as'' => ''payment_done'',
''uses'' => ''PayPalController@done''
]);
Simplemente abra www.example.com/paypal/agreement y debería llevarlo a PayPal