@@ -8,6 +8,14 @@ class ControllerPaymentOmise extends Controller
88 */
99 public function checkout ()
1010 {
11+ // Define 'OMISE_USER_AGENT_SUFFIX'
12+ if (!defined ('OMISE_USER_AGENT_SUFFIX ' ) && defined ('VERSION ' ))
13+ define ('OMISE_USER_AGENT_SUFFIX ' , 'OmiseOpenCart/1.5.0.2 OpenCart/ ' .VERSION );
14+
15+ // Define 'OMISE_API_VERSION'
16+ if (!defined ('OMISE_API_VERSION ' ))
17+ define ('OMISE_API_VERSION ' , '2014-07-27 ' );
18+
1119 // If has a `post['omise_token']` request.
1220 if (isset ($ this ->request ->post ['omise_token ' ])) {
1321 // Load `omise-php` library.
@@ -25,43 +33,85 @@ public function checkout()
2533
2634 // Create a order history with `Processing` status
2735 $ this ->load ->model ('checkout/order ' );
28- $ this ->model_checkout_order ->confirm ($ this ->session ->data ['order_id ' ], 2 );
29-
30- try {
31- // Try to create a charge and capture it.
32- $ omise_charge = OmiseCharge::create (
33- array (
34- "amount " => $ this ->request ->post ['amount ' ],
35- "currency " => "thb " ,
36- "description " => $ this ->request ->post ['description ' ],
37- "card " => $ this ->request ->post ['omise_token ' ]
38- ),
39- $ omise ['public_key ' ],
40- $ omise ['secret_key ' ]
41- );
42-
43- if (is_null ($ omise_charge ['failure_code ' ]) && is_null ($ omise_charge ['failure_code ' ]) && $ omise_charge ['captured ' ])
44- $ this ->model_checkout_order ->update ($ this ->session ->data ['order_id ' ], 15 );
45- else
46- $ this ->model_checkout_order ->update ($ this ->session ->data ['order_id ' ], 10 );
36+ $ order_id = $ this ->session ->data ['order_id ' ];
37+ $ order_info = $ this ->model_checkout_order ->getOrder ($ order_id );
38+ $ order_total = number_format ($ order_info ['total ' ], 2 , '' , '' );
39+
40+ if ($ order_info ) {
41+ try {
42+ // Try to create a charge and capture it.
43+ $ omise_charge = OmiseCharge::create (
44+ array (
45+ "amount " => $ order_total ,
46+ "currency " => 'thb ' ,
47+ "description " => $ this ->request ->post ['description ' ],
48+ "card " => $ this ->request ->post ['omise_token ' ]
49+ ),
50+ $ omise ['public_key ' ],
51+ $ omise ['secret_key ' ]
52+ );
53+
54+ if (is_null ($ omise_charge ['failure_code ' ]) && is_null ($ omise_charge ['failure_code ' ]) && $ omise_charge ['captured ' ]) {
55+ // Status: processed.
56+ $ this ->model_checkout_order ->confirm ($ order_id , 15 );
57+ } else {
58+ // Status: failed.
59+ $ this ->model_checkout_order ->update ($ order_id , 10 );
60+ }
4761
48- echo json_encode (
49- array (
50- 'failure_code ' => $ omise_charge ['failure_code ' ],
51- 'failure_message ' => $ omise_charge ['failure_message ' ],
52- 'captured ' => $ omise_charge ['captured ' ],
53- 'omise ' => $ omise_charge
54- )
55- );
56- } catch (Exception $ e ) {
57- $ this ->model_checkout_order ->update ($ this ->session ->data ['order_id ' ], 10 );
58- echo json_encode (array ('error ' => $ e ->getMessage ()));
62+ echo json_encode (
63+ array (
64+ 'failure_code ' => $ omise_charge ['failure_code ' ],
65+ 'failure_message ' => $ omise_charge ['failure_message ' ],
66+ 'captured ' => $ omise_charge ['captured ' ],
67+ 'omise ' => $ omise_charge
68+ )
69+ );
70+ } catch (Exception $ e ) {
71+ // Status: failed.
72+ $ this ->model_checkout_order ->update ($ this ->session ->data ['order_id ' ], 10 );
73+ echo json_encode (array ('error ' => $ e ->getMessage ()));
74+ }
75+ } else {
76+ echo json_encode (array ('error ' => 'Cannot find your order, please try again. ' ));
5977 }
6078 } else {
6179 return 'not authorized ' ;
6280 }
6381 }
6482
83+ /**
84+ * Retrieve list of months translation
85+ *
86+ * @return array
87+ */
88+ public function getMonths ()
89+ {
90+ $ months = array ();
91+ for ($ index =1 ; $ index <= 12 ; $ index ++) {
92+ $ month = ($ index < 10 ) ? '0 ' .$ index : $ index ;
93+ $ months [$ month ] = $ month ;
94+ }
95+ return $ months ;
96+ }
97+
98+ /**
99+ * Retrieve array of available years
100+ *
101+ * @return array
102+ */
103+ public function getYears ()
104+ {
105+ $ years = array ();
106+ $ first = date ("Y " );
107+
108+ for ($ index =0 ; $ index <= 10 ; $ index ++) {
109+ $ year = $ first + $ index ;
110+ $ years [$ year ] = $ year ;
111+ }
112+ return $ years ;
113+ }
114+
65115 /**
66116 * Omise card information form
67117 * @return void
0 commit comments