1. Go to this page and download the library: Download henshall/php-stripe-wrapper library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
henshall / php-stripe-wrapper example snippets
$sw = new StripeWrapper;
$key = $sw->setApiKey("sk_test_Gsdfsdfsdfsdfsdfdsfsdfsdfsdfsdf");
$sw->validateApiKey($key);
$sw->charge(['amount' => 1000, 'currency' => "USD", 'description' => "Payment for xyz service or product", "source" => $_POST["stripeToken"]]);
if ($sw->error) {
// Where to put your logic if there is an error. (Save error to DB, or log file, or email to yourself etc.)
// die($sw->error);
}
$sw = new StripeWrapper;
$key = $sw->setApiKey("sk_test_Gsdfsdfsdfsdfsdfdsfsdfsdfsdfsdf");
$sw->validateApiKey($key);
$customer = $sw->createCustomer(["name" => "testing dude", "email" => "[email protected]", "description" => "im a real person", "source" => $_POST["stripeToken"]]);
$sw->charge(['amount' => 1000, 'currency' => "USD", 'description' => "Payment for xyz service or product", 'customer' => $customer]);
if ($sw->error) {
// Where to put your logic if there is an error. (Save error to DB, or log file, or email to yourself etc.)
// die($sw->error);
}
// instantiate object
$sw = new StripeWrapper;
// set secret key
$key = $sw->setApiKey("sk_test_Gsdfsdfsdfsdfsdfdsfsdfsdfsdfsdf");
// validate secret key
$sw->validateApiKey($key);
// pass the customer id from stripe to retrieve the customer object
$customer = $sw->retrieveCustomer("cus_GPeOHGPqGH1fdd");
// create charge
$sw->charge(['amount' => 1000, 'currency' => "USD", 'description' => "Payment for xyz service or product", 'customer_id' => $customer]);
if ($sw->error) {
// Where to put your logic if there is an error. (Save error to DB, or log file, or email to yourself etc.)
// die($sw->error);
}
$sw = new StripeWrapper;
$key = $sw->setApiKey("sk_test_Gsdfsdfsdfsdfsdfdsfsdfsdfsdfsdf");
$sw->validateApiKey($key);
// (Note: do not run this code with every subscription - we only need to create a plan one time.)
// Make sure the amount is in cents
// Currency types suppoted found here: https://stripe.com/docs/currencies (Ex. USD, EUR, CAD, Etc.)
// Interval types logic if there is an error. (Save error to DB, or log file, or email to yourself etc.)
// die($sw->error);
}
$sw = new StripeWrapper;
$key = $sw->setApiKey("sk_test_Gsdfsdfsdfsdfsdfdsfsdfsdfsdfsdf");
$sw->validateApiKey($key);
$plan = $sw->retrievePlan("40_dollar_monthly_subscription");
$customer = $sw->createCustomer(["name" => "testing dude", "email" => "[email protected]", "description" => "im a real person", "source" => $_POST["stripeToken"]]);
$sw->createSubscription(["customer" => $customer, "items" => [["plan" => $plan]]]);
if ($sw->error) {
// Where to put your logic if there is an error. (Save error to DB, or log file, or email to yourself etc.)
// die($sw->error);
}
$sw = new StripeWrapper;
$key = $sw->setApiKey("sk_test_Gsdfsdfsdfsdfsdfdsfsdfsdfsdfsdf");
$sw->validateApiKey($key);
$customer = $sw->retrieveCustomer("cus_GPeOHsPqdH1Qhc");
$plan = $sw->retrievePlan("40_dollar_monthly_subscription");
$sw->createSubscription(["customer" => $customer, "items" => [["plan" => $plan]]]);
if ($sw->error) {
// Where to put your logic if there is an error. (Save error to DB, or log file, or email to yourself etc.)
// die($sw->error);
}
$sw = new StripeWrapper;
$key = $sw->setApiKey("sk_test_Gsdfsdfsdfsdfsdfdsfsdfsdfsdfsdf");
$sw->createSubscription(["customer" => "cus_GPeOHGPqGH1Qhc", "items" => [["plan" => '40_dollar_monthly_subscription']]]);
if ($sw->error) {
// Where to put your logic if there is an error. (Save error to DB, or log file, or email to yourself etc.)
// die($sw->error);
}
$sw = new StripeWrapper;
$key = $sw->setApiKey("sk_test_Gsdfsdfsdfsdfsdfdsfsdfsdfsdfsdf");
$sw->validateApiKey($key);
$sw->cancelSubscription($sw->retrieveSubscription($stripe_sub_id));
if ($sw->error) {
// Where to put your logic if there is an error. (Save error to DB, or log file, or email to yourself etc.)
// die($sw->error);
}
$sw = new StripeWrapper;
$key = $sw->setApiKey("sk_test_Gsdfsdfsdfsdfsdfdsfsdfsdfsdfsdf");
$sw->validateApiKey($key);
$customer = $sw->retrieveCustomer("cus_GPeOHsPqdH1Qhc");
$sw->deleteCustomer( $customer );
if ($sw->error) {
// Where to put your logic if there is an error. (Save error to DB, or log file, or email to yourself etc.)
// die($sw->error);
}
$sw = new StripeWrapper;
$webhook_data = $sw->getWebhookInput(@file_get_contents("php://input"));
if ($sw->error) {
// Where to put your logic if there is an error. (Save error to DB, or log file, or email to yourself etc.)
var_dump(http_response_code(500));
die($this->error);
}
$sw = new StripeWrapper;
$key = $sw->setApiKey("sk_test_Gsdfsdfsdfsdfsdfdsfsdfsdfsdfsdf");
$webhookData = [ 'url' => 'https://example.com/my/webhook/endpoint', 'enabled_events' => [
'invoice.payment_failed',
'customer.subscription.deleted',
],
];
$sw->createWebhook($webhookData);
if ($sw->error) {
// Where to put your logic if there is an error. (Save error to DB, or log file, or email to yourself etc.)
// die($sw->error);
}
$sw = new StripeWrapper;
$key = $sw->setApiKey("sk_test_Gsdfsdfsdfsdfsdfdsfsdfsdfsdfsdf");
$sw->retrieveWebhook($webhookId);
if ($sw->error) {
// Where to put your logic if there is an error. (Save error to DB, or log file, or email to yourself etc.)
// die($sw->error);
}
...
...
...
try {
// stores subscription in database.
$sub = SubscriptionDBModel::create($subscription_info);
//updates to show that user has paid.
$user->paid = 1;
$user->save();
} catch (\Exception $e) {
$error = new Error;
$error->message = "subscription Failure before payment ----> " . $e ;
$error->save();
//return back to payment page with error message.
return redirect()->back()->with("error", "Card not charged, could not create subscription");
}
$sw = new StripeWrapper;
$sw->setApiKey("sk_test_Gsdfsdfsdfsdfsdfdsfsdfsdfsdfsdf");
$customer = $sw->createCustomer(["name" => "testing dude", "email" => "[email protected]", "description" => "im a real person", "source" => $_POST["stripeToken"]]);
$sw->charge(['amount' => 1000, 'currency' => "USD", 'description' => "Payment for xyz service or product", 'customer' => $customer]);
if ($sw->error) {
// Create error message for admins to see if payment failed. This is important!
$error = new Error;
$error->message = "subscription Failure during payment, card not charged. ----> " . $e ;
$error->save();
// Here we can reverse the changes to the user object previous modified as well as delete the subscription..
$sub->delete();
$user->paid = 0;
$user->save();
//return back to payment page with error message.
return redirect()->back()->with("error", "Card not charged, could not create subscription");
}
// Now we know that the payment has been successful, we are free to return the user with payment successful message.
return redirect()->back()->with("success", "Payment Successful!");
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.