Bonjour,
Voila je rencontre un petit problème avec mon code.
je voudrais passer du mode sandbox au mode live
subscribe.php
<?php
require 'inc/secret_header.php';
require 'inc/function.php';
require_once 'inc/db.php';
require 'inc/Offer.php';
logged_only();
if (isset($_POST['offer'])){
$_SESSION['offer_key'] = $_POST['offer'];
header('location: payment.php');
}
?>
<h3>S'abonner</h3>
<form action="" method="post" class="user">
<ul>
<?php foreach (Offer::getoffers() as $k => $offer ?>
<li><input type="radio" name="offer" value="<?= $k; ?>"><?= $offer['name']; ?> = <?= $offer['price_text']; ?></li>
<?php endforeach; ?>
</ul>
<button class="btn btn-success" type="submit">S'Abonner</button>pay.php
<?php
require_once 'inc/db.php';
require 'inc/function.php';
require 'inc/Offer.php';
require 'vendor/autoload.php';
logged_only();
$ids = require 'paypal.php';
//on cree un ApiContext ki contient nos identifiants
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
$ids['id'],
$ids['secret']
)
);
//on recupere la liste des offres choisie par l'utilisateur
$key_offer = $_SESSION['offer_key'];
$offer = Offer::getoffers()[$key_offer];
//var_dump($offer);
//on recupere le paiement
$pay = $payemet = \PayPal\Api\Payment::get($_GET['paymentId'], $apiContext);
//on recupere les infos sur l'acheteur
$payer_infos = $payemet->getPayer()->getPayerInfo();
//on recupere les dates de debut et de fin d'inscription au forfait de l'acheteur et quelques donnees sur la transaction
$period_start = (new DateTime($_SESSION['payment']->create_time))->getTimestamp();
$_SESSION['subscription_start'] = $subscription_start = gmdate("Y-m-d H:i:s", $period_start);
$period = $offer['period'] === 'Month' ? new DateInterval($offer['interval']) : new DateInterval('P1Y');
$period_end = (new DateTime())->add($period)->getTimestamp();
$_SESSION['subscription_end'] = $subscription_end = gmdate("Y-m-d H:i:s", $period_end);
$payer_id = $payer_infos->payer_id;
$payer_email = $payer_infos->email;
$payer_first_name = $payer_infos->first_name;
$payer_last_name = $payer_infos->last_name;
$payer_country_code = $payer_infos->country_code;
$price_text = $offer['price_text'];
$username = $_SESSION['auth']->username;
$pdo->prepare("UPDATE users SET subscription_start = ?, subscription_end = ?, payer_id = ?, payer_email = ?, payer_first_name = ?, payer_last_name = ?, payer_country_code = ?, price_text = ? where username = ?")->execute([$_SESSION['subscription_start'], $_SESSION['subscription_end'], $payer_id, $payer_email, $payer_first_name, $payer_last_name, $payer_country_code, $price_text, $username]);
$execution = (new \PayPal\Api\PaymentExecution())
->setPayerId($_GET['PayerID'])
->setTransactions($payemet->getTransactions());
try {
$pay->execute($execution, $apiContext);
$_SESSION['flash']['success']="Votre paiement s'est deroulé avec succès et prendra effet lors de votre prochaine reconnection ";
header('location: logoutpaypal.php');
} catch (\Paypal\Exception\PayPalConnectionException $e) {
header('HTTP 500 Internal Server Error', true, 500);
var_dump(json_decode($e->getData()));
}
payment.php
<?php
require_once 'inc/db.php';
require 'inc/function.php';
require 'inc/Offer.php';
require 'vendor/autoload.php';
logged_only();
$ids = require 'paypal.php';
//on cree un ApiContext ki contient nos identifiants
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
$ids['id'],
$ids['secret']
)
);
//on recupere le pannier selectionne par l'utilisateur
$key_offer = $_SESSION['offer_key'];
$offer = Offer::getoffers()[$key_offer];
//on cree une liste d'item
$list = new \PayPal\Api\ItemList();
if (isset($offer)) {
$item = (new \PayPal\Api\Item())
->setName($offer['name'])
->setPrice($offer['price'])
->setCurrency('EUR')
->setQuantity(1);
$list->addItem($item);
//on cree une partie amount ki contient le paiement et les details sur le paiement
$details = (new \PayPal\Api\Details())
->setSubtotal($offer['price'])
->setTax($offer['tax']);
$amount = (new \PayPal\Api\Amount())
->setTotal($offer['total_price'])
->setCurrency('EUR')
->setDetails($details);
//A la fin on envoie la transaction au paiement
$transaction = (new \PayPal\Api\Transaction())
->setItemList($list)
->setDescription($offer['description'])
->setAmount($amount)
->setCustom('id_utilisateur');
$payment = new \PayPal\Api\Payment();
$payment->setTransactions([$transaction]);
$payment->setIntent('sale');
$redirectUrls = (new \PayPal\Api\RedirectUrls())
//A modifier
->setReturnUrl('https://thelockedfile.com/pay.php')
->setCancelUrl('https://thelockedfile.com/subscribe.php');
//End modif
$payment->setRedirectUrls($redirectUrls);
$payment->setPayer((new \PayPal\Api\Payer())->setPaymentMethod('paypal'));
try {
$payment->create($apiContext);
//on sauvegarde le tout dans une variable globale
$_SESSION['payment'] = $payment;
header('location: ' . $payment->getApprovalLink());
} catch (\Paypal\Exception\PayPalConnectionException $e) {
json_decode($e->getData());
}
}
ayant changé mon client ID et mon Secret je voudrais pouvoir me connecter au serveur paypal via mon applocation
au lieu de cela j'ai plutot une page blanche qui s'exécute pourtant bien en mode sandbox (https : \thelockedfile.com\payment.php), comme si il ya une petite touche que je devrais faire. Merci pour votre aide