1<?php
2namespace PhpPot\Service;
3
4require_once 'vendor/stripe/autoload.php';
5
6use \Stripe\Stripe;
7use \Stripe\Customer;
8use \Stripe\ApiOperations\Create;
9use \Stripe\Charge;
10
11class StripePayment
12{
13
14 private $apiKey;
15
16 private $stripeService;
17
18 public function __construct()
19 {
20 require_once "config.php";
21 $this->apiKey = STRIPE_SECRET_KEY;
22 $this->stripeService = new \Stripe\Stripe();
23 $this->stripeService->setVerifySslCerts(false);
24 $this->stripeService->setApiKey($this->apiKey);
25 }
26
27 public function addCustomer($customerDetailsAry)
28 {
29
30 $customer = new Customer();
31
32 $customerDetails = $customer->create($customerDetailsAry);
33
34 return $customerDetails;
35 }
36
37 public function chargeAmountFromCard($cardDetails)
38 {
39 $customerDetailsAry = array(
40 'email' => $cardDetails['email'],
41 'source' => $cardDetails['token']
42 );
43 $customerResult = $this->addCustomer($customerDetailsAry);
44 $charge = new Charge();
45 $cardDetailsAry = array(
46 'customer' => $customerResult->id,
47 'amount' => $cardDetails['amount']*100 ,
48 'currency' => $cardDetails['currency_code'],
49 'description' => $cardDetails['item_name'],
50 'metadata' => array(
51 'order_id' => $cardDetails['item_number']
52 )
53 );
54 $result = $charge->create($cardDetailsAry);
55
56 return $result->jsonSerialize();
57 }
58}
1<?php if(!empty($successMessage)) { ?>
2<div id="success-message"><?php echo $successMessage; ?></div>
3<?php } ?>
4<div id="error-message"></div>
5
6<form id="frmStripePayment" action="" method="post">
7 <div class="field-row">
8 <label>Card Holder Name</label> <span id="card-holder-name-info"
9 class="info"></span><br> <input type="text" id="name"
10 name="name" class="demoInputBox">
11 </div>
12 <div class="field-row">
13 <label>Email</label> <span id="email-info" class="info"></span><br>
14 <input type="text" id="email" name="email" class="demoInputBox">
15 </div>
16 <div class="field-row">
17 <label>Card Number</label> <span id="card-number-info"
18 class="info"></span><br> <input type="text" id="card-number"
19 name="card-number" class="demoInputBox">
20 </div>
21 <div class="field-row">
22 <div class="contact-row column-right">
23 <label>Expiry Month / Year</label> <span id="userEmail-info"
24 class="info"></span><br> <select name="month" id="month"
25 class="demoSelectBox">
26 <option value="08">08</option>
27 <option value="09">9</option>
28 <option value="10">10</option>
29 <option value="11">11</option>
30 <option value="12">12</option>
31 </select> <select name="year" id="year"
32 class="demoSelectBox">
33 <option value="18">2018</option>
34 <option value="19">2019</option>
35 <option value="20">2020</option>
36 <option value="21">2021</option>
37 <option value="22">2022</option>
38 <option value="23">2023</option>
39 <option value="24">2024</option>
40 <option value="25">2025</option>
41 <option value="26">2026</option>
42 <option value="27">2027</option>
43 <option value="28">2028</option>
44 <option value="29">2029</option>
45 <option value="30">2030</option>
46 </select>
47 </div>
48 <div class="contact-row cvv-box">
49 <label>CVC</label> <span id="cvv-info" class="info"></span><br>
50 <input type="text" name="cvc" id="cvc"
51 class="demoInputBox cvv-input">
52 </div>
53 </div>
54 <div>
55 <input type="submit" name="pay_now" value="Submit"
56 id="submit-btn" class="btnAction"
57 onClick="stripePay(event);">
58
59 <div id="loader">
60 <img alt="loader" src="LoaderIcon.gif">
61 </div>
62 </div>
63 <input type='hidden' name='amount' value='0.5'> <input type='hidden'
64 name='currency_code' value='USD'> <input type='hidden'
65 name='item_name' value='Test Product'> <input type='hidden'
66 name='item_number' value='PHPPOTEG#1'>
67</form>
1<?php
2use \PhpPot\Service\StripePayment;
3
4if (!empty($_POST["token"])) {
5 require_once 'StripePayment.php';
6 $stripePayment = new StripePayment();
7
8 $stripeResponse = $stripePayment->chargeAmountFromCard($_POST);
9
10 require_once "DBController.php";
11 $dbController = new DBController();
12
13 $amount = $stripeResponse["amount"] /100;
14
15 $param_type = 'ssdssss';
16 $param_value_array = array(
17 $_POST['email'],
18 $_POST['item_number'],
19 $amount,
20 $stripeResponse["currency"],
21 $stripeResponse["balance_transaction"],
22 $stripeResponse["status"],
23 json_encode($stripeResponse)
24 );
25 $query = "INSERT INTO tbl_payment (email, item_number, amount, currency_code, txn_id, payment_status, payment_response) values (?, ?, ?, ?, ?, ?, ?)";
26 $id = $dbController->insert($query, $param_type, $param_value_array);
27
28 if ($stripeResponse['amount_refunded'] == 0 && empty($stripeResponse['failure_code']) && $stripeResponse['paid'] == 1 && $stripeResponse['captured'] == 1 && $stripeResponse['status'] == 'succeeded') {
29 $successMessage = "Stripe payment is completed successfully. The TXN ID is " . $stripeResponse["balance_transaction"];
30 }
31}
32?>