· 7 years ago · Oct 13, 2018, 05:56 PM
1<?php namespace Jiri\JKShop\Components;
2
3use Cms\Classes\ComponentBase;
4use Redirect;
5
6class CustomPaymentStripeCheckout extends ComponentBase
7{
8
9 public function componentDetails()
10 {
11 return [
12 'name' => 'Custom Payment Stripe Checkout',
13 'description' => 'Custom payment gateway stripe implementation use one page checkout.'
14 ];
15 }
16
17 public function defineProperties()
18 {
19 return [
20 'slug' => [
21 'title' => 'URL Order ID',
22 'description' => 'URL Order ID',
23 'default' => '{{ :slug }}',
24 'type' => 'string'
25 ],
26 'stripe_publishable_key' => [
27 'title' => 'Publishable Key',
28 'description' => 'For testing you can use: "pk_test_6pRNASCoBOKtIshFeQd4XMUh"',
29 'type' => 'string'
30 ],
31 'stripe_secret_key' => [
32 'title' => 'Secret Key',
33 'description' => 'For testing you can use: "sk_test_BQokikJOvBiI2HlWgH4olfQ2"',
34 'type' => 'string'
35 ],
36 'stripe_return_url' => [
37 'title' => 'Success URL',
38 'description' => 'Use full url where will be your customer redirect after paid "http://www.example.com/paypal-success"',
39 'type' => 'string'
40 ],
41
42 ];
43 }
44
45 public function onRun()
46 {
47 $order = \Jiri\JKShop\Models\Order::findOrderBySlugForPaymentGateway($this->param("slug"));
48 if ($order == null) { return \Response::make($this->controller->run('404'), 404); }
49
50 $this->page["order"] = $order;
51
52 // paypal details
53 $this->page["stripe_publishable_key"] = $this->property('stripe_publishable_key');
54 $this->page["stripe_secret_key"] = $this->property('stripe_secret_key');
55 $this->page["stripe_currency_code"] = $order->paymentGateway->gateway_currency;
56 $this->page["stripe_return_url"] = $this->property('stripe_return_url');
57 }
58
59 public function onSubmitPayForm() {
60 $stripe = [
61 'secret_key' => $this->property('stripe_secret_key'),
62 'publishable_key' => $this->property('stripe_publishable_key')
63 ];
64 \Stripe\Stripe::setApiKey($stripe['secret_key']);
65
66
67 // save IN to events
68 $eventLog = new \System\Models\EventLog();
69 $data = array();
70 $data["name"] = "Stripe Response";
71 $data["post"] = post();
72 $eventLog->message = json_encode($data);
73 $eventLog->save();
74
75 // get order
76 $order = \Jiri\JKShop\Models\Order::findOrderBySlugForPaymentGateway($this->param("slug"));
77
78 if ($order != null) {
79 $token = post('stripeToken');
80 $customer = \Stripe\Customer::create(array(
81 'email' => $order->contact_email,
82 'card' => $token
83 ));
84
85 try{
86
87 $charge = \Stripe\Charge::create(array(
88 'customer' => $customer->id,
89 'amount' => ($order->total_price + $order->shipping_price)*100,
90 'currency' => $order->paymentGateway->gateway_currency
91 ));
92 }
93 catch(\Stripe\Error\Card $e)
94 {
95 Session::put('errormessage', 'Your card has been declined');
96
97 return Redirect::back()->withInput();
98
99 }
100
101 // check response
102 $order->orderstatus = $order->paymentGateway->orderStatusAfter;
103 $order->paid_date = \Carbon\Carbon::now();
104 $order->paid_detail = $charge;
105 $order->save();
106
107 return Redirect::to($this->property('stripe_return_url')); //Redirect::to("/");
108 }
109
110 }
111}