-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaypal-payment.php
More file actions
111 lines (92 loc) · 3.37 KB
/
paypal-payment.php
File metadata and controls
111 lines (92 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
// Include PayPal SDK and configure API credentials
use PayPal\Api\Agreement;
use PayPal\Api\AgreementStateDescriptor;
use PayPal\Api\ChargeModel;
use PayPal\Api\Currency;
use PayPal\Api\MerchantPreferences;
use PayPal\Api\Patch;
use PayPal\Api\PatchRequest;
use PayPal\Api\PaymentDefinition;
use PayPal\Api\Plan;
require 'vendor/autoload.php';
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'client_id',
'secret'
)
);
// Create a new billing plan
$plan = new Plan();
$plan->setName('Monthly Subscription')
->setDescription('Monthly Subscription for premium content')
->setType('INFINITE');
// Set up payment definition
$paymentDefinition = new PaymentDefinition();
$paymentDefinition->setName('Regular Payments')
->setType('REGULAR')
->setFrequency('Month')
->setFrequencyInterval('1')
->setCycles('0')
->setAmount(new Currency(array('value' => 9.99, 'currency' => 'USD')));
// Set up charge models
$chargeModel = new ChargeModel();
$chargeModel->setType('SHIPPING')
->setAmount(new Currency(array('value' => 0.0, 'currency' => 'USD')));
$paymentDefinition->setChargeModels(array($chargeModel));
// Set up merchant preferences
$merchantPreferences = new MerchantPreferences();
$merchantPreferences->setReturnUrl('https://www.example.com/return')
->setCancelUrl('https://www.example.com/cancel')
->setAutoBillAmount('yes')
->setInitialFailAmountAction('CONTINUE')
->setMaxFailAttempts('0')
->setSetupFee(new Currency(array('value' => 9.99, 'currency' => 'USD')));
$plan->setPaymentDefinitions(array($paymentDefinition));
$plan->setMerchantPreferences($merchantPreferences);
// Create the plan
try {
$createdPlan = $plan->create($apiContext);
try {
$agreement = new Agreement();
$agreement->setName('Monthly Subscription')
->setDescription('Monthly Subscription for premium content')
->setStartDate(gmdate("Y-m-d\TH:i:s\Z", time() + 3600));
$plan = Plan::get($createdPlan->getId(), $apiContext);
$agreement->setPlan($plan);
// Add payer information
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$agreement->setPayer($payer);
// Create agreement
$agreement = $agreement->create($apiContext);
// Extract approval URL to redirect user
$approvalUrl = $agreement->getApprovalLink();
header("Location: {$approvalUrl}");
exit;
} catch (Exception $ex) {
// Handle any errors that may have occurred
echo "Agreement creation failed: " . $ex->getMessage();
}
} catch (Exception $ex) {
// Handle any errors that may have occurred
echo "Plan creation failed: " . $ex->getMessage();
}
// Update database table when payment is successful
if (isset($_GET['token']) && $_GET['token'] != '') {
try {
// Execute agreement
$agreement = new Agreement();
$agreement->execute($_GET['token'], $apiContext);
$agreement = Agreement::get($agreement->getId(), $apiContext);
// Update database table with agreement information
$conn = mysqli_connect("host", "username", "password", "database");
$query = "UPDATE users SET agreement_id = '" . $agreement->getId() . "', agreement_status = '" . $agreement->getState() . "' WHERE user_id = '" . $user_id . "'";
mysqli_query($conn, $query);
mysqli_close($conn);
} catch (Exception $ex) {
// Handle any errors that may have occurred
echo "Payment execution failed: " . $ex->getMessage();
}
}
?>