Bonjour,
Voila je rencontre un petit problème avec mon code.
mes routes:
// Paypal
Route::group(array('prefix' => 'paypal'), function () {
Route::get('create', 'PaymentController@paypal');
Route::get('execute', 'PaymentController@executePayment');
});
public function paypal()
{
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
getenv('PAYPAL_CLIENT_ID'),
getenv('PAYPAL_SECRET')
)
);
$list = new \PayPal\Api\ItemList();
$item = (new \PayPal\Api\Item())
->setName('Lettre de résiliation')
->setPrice("7.25")
->setCurrency('EUR')
->setQuantity(1);
$list->addItem($item);
$details = (new \PayPal\Api\Details())
->setSubtotal("7.25");
$amount = (new \PayPal\Api\Amount())
->setTotal("7.25")
->setCurrency("EUR")
->setDetails($details);
$transaction = (new \PayPal\Api\Transaction())
->setItemList($list)
->setDescription('Achat sur resilier.dev')
->setAmount($amount)
->setCustom('demo-1');
$payment = new \PayPal\Api\Payment();
$payment->setTransactions([$transaction]);
$payment->setIntent('sale');
$redirectUrls = (new \PayPal\Api\RedirectUrls())
->setReturnUrl('http://resilier.dev/paypal/execute')
->setCancelUrl('http://resilier.dev/paiement.php');
$payment->setRedirectUrls($redirectUrls);
$payment->setPayer((new \PayPal\Api\Payer())->setPaymentMethod('paypal'));
try {
$payment->create($apiContext);
return json_encode([
'id' => $payment->getId()
]);
} catch (\PayPal\Exception\PayPalConnectionException $e) {
var_dump(json_decode($e->getData()));
}
}
public function executePayment()
{
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
getenv('PAYPAL_CLIENT_ID'),
getenv('PAYPAL_SECRET')
)
);
$payment = \PayPal\Api\Payment::get(Input::get('paymentID'), $apiContext);
$execution = (new \PayPal\Api\PaymentExecution())
->setPayerId(Input::get('payerID'));
try {
$payment->execute($execution, $apiContext);
} catch (\PayPal\Exception\PayPalConnectionException $e) {
header('HTTP 500 Internal Server Error', true, 500);
var_dump(json_decode($e->getData()));
}
}
je n'arrive pas à adapter au button js , les functions marche je n'arrive juste pas a executer mon paiement sur la fenetre. code js :
var CREATE_PAYMENT_URL = '/paypal/create';
var EXECUTE_PAYMENT_URL = '/paypal/execute';
paypal.Button.render({
env: 'sandbox', // Or 'production'
commit: true, // Show a 'Pay Now' button
// Pour customiser le button
style: {
color: 'gold',
size: 'responsive'
},
payment: function () {
return paypal.request.get(CREATE_PAYMENT_URL).then(function (data) {
return data.id;
});
},
onAuthorize: function (data) {
return paypal.request.get(EXECUTE_PAYMENT_URL, {
paymentID: data.paymentID,
payerID: data.payerID
}).then(function () {
});
}
}, '#paypal-button');
Préfère le comportement php dans tes vues, le js n'est pas la solution. Car quelle que soit l'action, on sera en asynchrone.