Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.17 KB | None | 0 0
  1. package com.flowerservice.flowers.fragments;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.app.AlertDialog;
  5. import android.app.DatePickerDialog;
  6. import android.app.TimePickerDialog;
  7. import android.content.Context;
  8. import android.content.DialogInterface;
  9. import android.content.Intent;
  10. import android.graphics.Bitmap;
  11. import android.os.Bundle;
  12. import android.os.Handler;
  13. import android.support.annotation.NonNull;
  14. import android.support.annotation.Nullable;
  15. import android.support.v4.app.Fragment;
  16. import android.support.v7.widget.CardView;
  17. import android.view.LayoutInflater;
  18. import android.view.View;
  19. import android.view.ViewGroup;
  20. import android.widget.CheckBox;
  21. import android.widget.CompoundButton;
  22. import android.widget.DatePicker;
  23. import android.widget.EditText;
  24. import android.widget.RadioButton;
  25. import android.widget.ScrollView;
  26. import android.widget.TextView;
  27. import android.widget.TimePicker;
  28. import android.widget.Toast;
  29.  
  30. import com.flowerservice.flowers.Constants;
  31. import com.flowerservice.flowers.FlowersApp;
  32. import com.flowerservice.flowers.MainActivity;
  33. import com.flowerservice.flowers.R;
  34. import com.flowerservice.flowers.network.bodies.OrderBody;
  35. import com.flowerservice.flowers.network.models.POrderModel;
  36. import com.flowerservice.flowers.network.models.PhotoPostModel;
  37. import com.flowerservice.flowers.temp.BufferedOrderObject;
  38. import com.google.android.gms.common.api.Status;
  39. import com.google.android.gms.location.places.Place;
  40. import com.google.android.gms.location.places.ui.PlaceSelectionListener;
  41. import com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment;
  42. import com.google.android.gms.maps.CameraUpdateFactory;
  43. import com.google.android.gms.maps.GoogleMap;
  44. import com.google.android.gms.maps.OnMapReadyCallback;
  45. import com.google.android.gms.maps.SupportMapFragment;
  46. import com.google.android.gms.maps.model.LatLng;
  47. import com.google.android.gms.maps.model.LatLngBounds;
  48. import com.google.android.gms.maps.model.MarkerOptions;
  49.  
  50. import java.io.ByteArrayOutputStream;
  51. import java.io.File;
  52. import java.io.FileOutputStream;
  53. import java.io.IOException;
  54. import java.util.Calendar;
  55. import java.util.HashMap;
  56. import java.util.Objects;
  57.  
  58. import okhttp3.MediaType;
  59. import okhttp3.MultipartBody;
  60. import okhttp3.RequestBody;
  61. import okio.Buffer;
  62. import retrofit2.Call;
  63. import retrofit2.Callback;
  64. import retrofit2.Response;
  65. import ru.cloudpayments.sdk.PaymentWidget;
  66. import ru.cloudpayments.sdk.business.domain.model.BaseResponse;
  67. import ru.cloudpayments.sdk.view.PaymentTaskListener;
  68.  
  69. /**
  70. * ComposeDeliveryFragment.
  71. * Created by MATHAHAKAR (Oleynik Pavel) on 15.05.2018.
  72. * Project: Flowers.
  73. */
  74. public class ComposeDeliveryFragment extends Fragment implements OnMapReadyCallback, PlaceSelectionListener, View.OnClickListener,
  75. CompoundButton.OnCheckedChangeListener, MemoryFragment {
  76.  
  77. private MainActivity.ExtFragmentResolver activityExtFragmentResolver;
  78. private Context context;
  79.  
  80. private DatePickerDialog datePickerDialog;
  81. private TimePickerDialog timePicker;
  82.  
  83. private String placeAddress = null;
  84.  
  85. private GoogleMap mMap;
  86. private SupportMapFragment mapFragment;
  87. private SupportPlaceAutocompleteFragment autocompleteFragment;
  88.  
  89. private ScrollView scrollView;
  90. private RadioButton rbToMe;
  91. private RadioButton rbToAnother;
  92. private CheckBox cbDontKnowAddress;
  93. private CardView cvFlat;
  94. private CardView cvMap;
  95. private TextView tvDontKnowHint;
  96. private EditText etRecipientPhone;
  97. private EditText etFlat;
  98. private EditText etComment;
  99. private EditText etDate;
  100. private EditText etTime;
  101. private EditText etCustomerPhone;
  102. private View btPay;
  103.  
  104. private Handler animationHandler;
  105.  
  106. private final static int UI_STATE_DEFAULT = 10;
  107. private final static int UI_STATE_DELIVERY_TO_ANOTHER_PERSON = 20;
  108. private final static int UI_STATE_DELIVERY_TO_ANOTHER_PERSON_WITHOUT_ADDRESS = 30;
  109.  
  110. @SuppressLint("DefaultLocale")
  111. @Nullable
  112. @Override
  113. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  114. final View v = inflater.inflate(R.layout.fragment_compose_delivery, container, false);
  115.  
  116. initDateTimeDialogs();
  117.  
  118. mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
  119. mapFragment.getMapAsync(this);
  120.  
  121. autocompleteFragment =
  122. (SupportPlaceAutocompleteFragment) getChildFragmentManager()
  123. .findFragmentById(R.id.place_autocomplete_fragment);
  124. autocompleteFragment.setOnPlaceSelectedListener(this);
  125. autocompleteFragment.setBoundsBias(new LatLngBounds(
  126. new LatLng(59.741497, 30.081964),
  127. new LatLng(60.097260, 30.554265)
  128. ));
  129.  
  130. etRecipientPhone = v.findViewById(R.id.etRecipientPhone);
  131. etCustomerPhone = v.findViewById(R.id.etCustomerPhone);
  132. etDate = v.findViewById(R.id.etDate);
  133. etTime = v.findViewById(R.id.etTime);
  134. scrollView = v.findViewById(R.id.svDeliveryDetails);
  135. rbToMe = v.findViewById(R.id.rbDeliveryMe);
  136. rbToAnother = v.findViewById(R.id.rbDeliveryPresent);
  137. cbDontKnowAddress = v.findViewById(R.id.cbDoNotKnowAddress);
  138. cvFlat = v.findViewById(R.id.cvFlat);
  139. cvMap = v.findViewById(R.id.cvMap);
  140. tvDontKnowHint = v.findViewById(R.id.tvRecipientHint);
  141. etComment = v.findViewById(R.id.etDeliveryComment);
  142. etFlat = v.findViewById(R.id.etFlat);
  143. btPay = v.findViewById(R.id.btBouquetPayNow);
  144.  
  145. btPay.setOnClickListener(this);
  146.  
  147. Calendar calendar = Calendar.getInstance();
  148. String dateStr = calendar.get(Calendar.DAY_OF_MONTH) + "/"
  149. + (calendar.get(Calendar.MONTH) + 1) + "/"
  150. + calendar.get(Calendar.YEAR);
  151. etDate.setText(dateStr);
  152. calendar.add(Calendar.HOUR_OF_DAY, 1);
  153. String timeStr = String.format("%2d", calendar.get(Calendar.HOUR_OF_DAY)) + ":"
  154. + String.format("%02d", calendar.get(Calendar.MINUTE));
  155. etTime.setText(timeStr);
  156. etDate.setKeyListener(null);
  157. etDate.setOnClickListener(this);
  158. etTime.setKeyListener(null);
  159. etTime.setOnClickListener(this);
  160.  
  161. etDate.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  162. @Override
  163. public void onFocusChange(View view, boolean hasFocus) {
  164. if (hasFocus) {
  165. showDateDialog();
  166. }
  167. }
  168. });
  169.  
  170. rbToMe.setOnCheckedChangeListener(this);
  171. rbToAnother.setOnCheckedChangeListener(this);
  172.  
  173. cbDontKnowAddress.setOnCheckedChangeListener(this);
  174.  
  175. animationHandler = new Handler();
  176. updateUi(UI_STATE_DEFAULT, false);
  177.  
  178. loadData();
  179.  
  180. return v;
  181. }
  182.  
  183. private void initDateTimeDialogs () {
  184. Calendar currentTime = Calendar.getInstance();
  185. datePickerDialog = new DatePickerDialog(
  186. context, new DatePickerDialog.OnDateSetListener() {
  187. @Override
  188. public void onDateSet(DatePicker datePicker, int year, int month, int day) {
  189. // todo check if time is right
  190. String date = day + "/" + month + "/" + year;
  191. etDate.setText(date);
  192. }
  193. }, currentTime.get(Calendar.YEAR), currentTime.get(Calendar.MONTH), currentTime.get(Calendar.DAY_OF_MONTH));
  194. datePickerDialog.setTitle(getString(R.string.select_date));
  195.  
  196. timePicker = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
  197. @SuppressLint("DefaultLocale")
  198. @Override
  199. public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
  200. // todo check if time is right
  201. String time = String.format("%2d", selectedHour) + ":"
  202. + String.format("%02d", selectedMinute);
  203. etTime.setText(time);
  204. }
  205. }, currentTime.get(Calendar.HOUR_OF_DAY), currentTime.get(Calendar.MINUTE), true);
  206. timePicker.setTitle(getString(R.string.select_time));
  207. }
  208.  
  209. private void showDateDialog () {
  210. datePickerDialog.show();
  211. }
  212.  
  213. private void showTimeDialog () {
  214. timePicker.show();
  215. }
  216.  
  217. private void updateUi (int uiState, boolean animation) {
  218. switch (uiState) {
  219. case UI_STATE_DEFAULT:
  220. hideView(cbDontKnowAddress, animation);
  221. hideView(tvDontKnowHint, animation);
  222. hideView(etRecipientPhone, animation);
  223. showView(cvMap, animation);
  224. showView(cvFlat, animation);
  225. break;
  226. case UI_STATE_DELIVERY_TO_ANOTHER_PERSON:
  227. showView(cbDontKnowAddress, animation);
  228. hideView(tvDontKnowHint, animation);
  229. hideView(etRecipientPhone, animation);
  230. showView(cvMap, animation);
  231. showView(cvFlat, animation);
  232. break;
  233. case UI_STATE_DELIVERY_TO_ANOTHER_PERSON_WITHOUT_ADDRESS:
  234. showView(cbDontKnowAddress, animation);
  235. showView(tvDontKnowHint, animation);
  236. showView(etRecipientPhone, animation);
  237. hideView(cvMap, animation);
  238. hideView(cvFlat, animation);
  239. break;
  240. }
  241. }
  242.  
  243. private void hideView (final View view, boolean animation) {
  244. if (!animation) {
  245. view.setVisibility(View.GONE);
  246. return;
  247. }
  248. long duration = 250;
  249. view.animate()
  250. .translationX(-20)
  251. .alpha(0.0f)
  252. .setDuration(duration);
  253. animationHandler.postDelayed(
  254. new Runnable() {
  255. @Override
  256. public void run() {
  257. view.setVisibility(View.GONE);
  258. }
  259. }, duration + 100
  260. );
  261. }
  262.  
  263. private void showView (final View view, boolean animation) {
  264. if (view.getVisibility() == View.VISIBLE) return;
  265. view.setVisibility(View.VISIBLE);
  266. if (!animation) return;
  267. long duration = 250;
  268. view.animate()
  269. .translationX(0)
  270. .alpha(1.0f)
  271. .setDuration(duration);
  272. }
  273.  
  274. @Override
  275. public void onAttach(Context context) {
  276. this.context = context;
  277. super.onAttach(context);
  278. }
  279.  
  280. @Override
  281. public void onDestroyView() {
  282. super.onDestroyView();
  283. }
  284.  
  285. @Override
  286. public void onMapReady(GoogleMap googleMap) {
  287. mMap = googleMap;
  288. mMap.getUiSettings().setCompassEnabled(true);
  289. mMap.getUiSettings().setZoomControlsEnabled(true);
  290. mMap.getUiSettings().setAllGesturesEnabled(false);
  291. mMap.getUiSettings().setMapToolbarEnabled(false);
  292. LatLng spb = new LatLng(59.9431111, 30.3646389);
  293. mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(spb,10.0f)); // 2.0-21.0
  294. }
  295.  
  296. @Override
  297. public void onPlaceSelected(Place place) {
  298. scrollView.scrollTo(0, 0);
  299. mMap.clear();
  300. mMap.addMarker(new MarkerOptions().position(place.getLatLng()).title(getString(R.string.place_delivery)));
  301. mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(),17.0f));
  302. this.placeAddress = String.valueOf(place.getAddress());
  303. }
  304.  
  305. @Override
  306. public void onError(Status status) {
  307. Toast.makeText(context, R.string.err_unresolved, Toast.LENGTH_SHORT).show();
  308. }
  309.  
  310. @Override
  311. public void onClick(View view) {
  312. switch (view.getId()) {
  313. case R.id.etTime:
  314. showTimeDialog();
  315. break;
  316. case R.id.etDate:
  317. showDateDialog();
  318. break;
  319. case R.id.btBouquetPayNow:
  320. if (isFieldsAreOk()) {
  321. saveData();
  322. showAlert();
  323. }
  324. break;
  325. }
  326. }
  327.  
  328. @Override
  329. public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
  330. switch (compoundButton.getId()) {
  331. case R.id.rbDeliveryMe:
  332. if (checked) {
  333. updateUi(UI_STATE_DEFAULT, true);
  334. cbDontKnowAddress.setChecked(false);
  335. }
  336. break;
  337. case R.id.rbDeliveryPresent:
  338. if (checked) {
  339. if (!cbDontKnowAddress.isChecked()) {
  340. updateUi(UI_STATE_DELIVERY_TO_ANOTHER_PERSON, true);
  341. } else {
  342. updateUi(UI_STATE_DELIVERY_TO_ANOTHER_PERSON_WITHOUT_ADDRESS, true);
  343. }
  344. }
  345. break;
  346. case R.id.cbDoNotKnowAddress:
  347. if (checked) {
  348. updateUi(UI_STATE_DELIVERY_TO_ANOTHER_PERSON_WITHOUT_ADDRESS, true);
  349. } else {
  350. updateUi(UI_STATE_DELIVERY_TO_ANOTHER_PERSON, true);
  351. }
  352. break;
  353. }
  354. }
  355.  
  356. private void showAlert () {
  357. final BufferedOrderObject boo = ((FlowersApp) context.getApplicationContext()).getBufferedOrderObject();
  358. AlertDialog.Builder ad = new AlertDialog.Builder(context);
  359.  
  360. StringBuilder msg = new StringBuilder();
  361. msg.append(getString(R.string.adPrice)).append(" ").append(String.valueOf(boo.getPrice()));
  362. msg.append(System.getProperty("line.separator"));
  363. msg.append(getString(R.string.adColor)).append(" ");
  364. switch (Integer.parseInt(boo.getColor())) {
  365. case Constants.Orders.BOUQUET_COLOR_GENTLE:
  366. msg.append(getString(R.string.cl_gentle));
  367. break;
  368. case Constants.Orders.BOUQUET_COLOR_BRIGHT:
  369. msg.append(getString(R.string.cl_bright));
  370. break;
  371. case Constants.Orders.BOUQUET_COLOR_DARK:
  372. msg.append(getString(R.string.cl_dark));
  373. break;
  374. case Constants.Orders.BOUQUET_COLOR_RANDOM:
  375. msg.append(getString(R.string.cl_random));
  376. break;
  377. }
  378. msg.append(System.getProperty("line.separator"));
  379. if (boo.getCharacteristic() != null) {
  380. msg.append(getString(R.string.adCharacteristic)).append(" ").append(boo.getCharacteristic());
  381. }
  382. msg.append(System.getProperty("line.separator"));
  383. if (boo.getPostCard() != null) {
  384. msg.append(getString(R.string.adPostcard)).append(" ").append(boo.getPostCard());
  385. msg.append(System.getProperty("line.separator"));
  386. }
  387. if (boo.getFlowersDetails() != null) {
  388. msg.append(getString(R.string.adBouquetDetails)).append(" ").append(boo.getFlowersDetails());
  389. msg.append(System.getProperty("line.separator"));
  390. }
  391. if (boo.getDeliveryAddress() != null) {
  392. msg.append(getString(R.string.adDeliveryAddress)).append(" ").append(boo.getDeliveryAddress());
  393. msg.append(System.getProperty("line.separator"));
  394. }
  395. if (boo.getDeliveryDetails() != null) {
  396. msg.append(getString(R.string.adDeliveryDetails)).append(" ").append(boo.getDeliveryDetails());
  397. msg.append(System.getProperty("line.separator"));
  398. }
  399. msg.append(getString(R.string.adDeliveryTime)).append(" ").append(boo.getDeliveryTime());
  400. msg.append(System.getProperty("line.separator"));
  401. msg.append(getString(R.string.adCustomerPhone)).append(" ").append(boo.getCustomerPhone());
  402. msg.append(System.getProperty("line.separator"));
  403. if (boo.getRecipientPhone() != null) {
  404. msg.append(getString(R.string.adRecipientPhone)).append(" ").append(boo.getRecipientPhone());
  405. }
  406.  
  407. ad.setTitle(R.string.adTitle);
  408. ad.setMessage(msg.toString());
  409. ad.setPositiveButton(R.string.adConfirmBtn, new DialogInterface.OnClickListener() {
  410. public void onClick(DialogInterface dialog, int arg1) {
  411. sendRequest(boo);
  412. }
  413. });
  414. ad.setNegativeButton(R.string.adCancelBtn, new DialogInterface.OnClickListener() {
  415. public void onClick(DialogInterface dialog, int arg1) {
  416. // nothing to do
  417. }
  418. });
  419. ad.show();
  420. }
  421.  
  422. private void sendRequest (final BufferedOrderObject boo) {
  423. OrderBody body = new OrderBody();
  424. body.setPrice(boo.getPrice());
  425. body.setDeliveryTime(boo.getDeliveryTime());
  426. body.setColor(boo.getColor());
  427. body.setCharacteristic(boo.getCharacteristic());
  428. if (boo.getPostCard() != null) {
  429. body.setPostCard(boo.getPostCard());
  430. }
  431. if (boo.getFlowersDetails() != null) {
  432. body.setFlowersDetails(boo.getFlowersDetails());
  433. }
  434. if (boo.getDeliveryDetails() != null) {
  435. body.setDeliveryDetails(boo.getDeliveryDetails());
  436. }
  437. if (boo.getDeliveryAddress() != null) {
  438. body.setDeliveryAddress(boo.getDeliveryAddress());
  439. }
  440. if (boo.getRecipientPhone() != null) {
  441. body.setRecipientPhone(boo.getRecipientPhone());
  442. }
  443. body.setCustomerPhone(boo.getCustomerPhone());
  444. body.setStatus(String.valueOf(Constants.Orders.ORDER_STATUS_SUBMITTED));
  445.  
  446. FlowersApp.getApi().placeOrder(
  447. ((FlowersApp) context.getApplicationContext()).getBearerToken(),
  448. Constants.CONTENT_TYPE_JSON,
  449. body
  450. ).enqueue(new Callback<POrderModel>() {
  451. @Override
  452. public void onResponse(@NonNull Call<POrderModel> call, @NonNull Response<POrderModel> response) {
  453. if (response.code() == 201) { // 201: created
  454. if (response.body() != null) {
  455. uploadPhotos(boo, Objects.requireNonNull(response.body()).getId());
  456. startPayment(boo, Objects.requireNonNull(response.body()).getId());
  457. clearData(); // TODO !!! REMOVE THIS LINE WHEN WILL BE CONFIGURED REAL PAYMENT !!!
  458. }
  459. Toast.makeText(context, R.string.order_submitted, Toast.LENGTH_SHORT).show();
  460. } else {
  461. Toast.makeText(context, R.string.err_unresolved, Toast.LENGTH_SHORT).show();
  462. }
  463. }
  464.  
  465. @Override
  466. public void onFailure(@NonNull Call<POrderModel> call, @NonNull Throwable t) {
  467. Toast.makeText(context, R.string.weak_internet, Toast.LENGTH_SHORT).show();
  468. }
  469. });
  470. }
  471.  
  472. private boolean isFieldsAreOk () {
  473. if (etCustomerPhone.getText().toString().trim().length() == 0) {
  474. Toast.makeText(context, R.string.pls_type_customer_phone, Toast.LENGTH_SHORT).show();
  475. return false;
  476. }
  477. if (!cbDontKnowAddress.isChecked() && placeAddress == null) {
  478. Toast.makeText(context, R.string.pls_choose_address, Toast.LENGTH_SHORT).show();
  479. return false;
  480. }
  481. if (!cbDontKnowAddress.isChecked() && etFlat.getText().toString().trim().length() == 0) {
  482. Toast.makeText(context, R.string.pls_flat_num, Toast.LENGTH_SHORT).show();
  483. return false;
  484. }
  485. if (rbToAnother.isChecked() && cbDontKnowAddress.isChecked()
  486. && etRecipientPhone.getText().toString().trim().length() == 0) {
  487. Toast.makeText(context, R.string.pls_recipient_phone, Toast.LENGTH_SHORT).show();
  488. return false;
  489. }
  490. return true;
  491. }
  492.  
  493. private void uploadPhotos(BufferedOrderObject boo, int orderId) {
  494. if (boo.getBitmapA() != null) uploadPhoto(boo, orderId, 1);
  495. if (boo.getBitmapB() != null) uploadPhoto(boo, orderId, 2);
  496.  
  497. }
  498.  
  499. private void uploadPhoto (BufferedOrderObject boo, int orderId, final int photoPos) {
  500. String url = "api/" + orderId + "/" + "ph" + photoPos;
  501.  
  502. MultipartBody.Part filePartA = null;
  503. try {
  504. File file = new File(context.getCacheDir(), "tmp_photo");
  505. boolean created = file.createNewFile();
  506. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  507. Bitmap bitmap = photoPos == 1 ? boo.getBitmapA() : boo.getBitmapB();
  508. bitmap.compress(Bitmap.CompressFormat.JPEG, 35, outStream);
  509. byte[] bitmapData = outStream.toByteArray();
  510. FileOutputStream fos = new FileOutputStream(file);
  511. fos.write(bitmapData);
  512. fos.flush();
  513. fos.close();
  514. filePartA = MultipartBody.Part.createFormData(
  515. "file",
  516. file.getName(),
  517. RequestBody.create(MediaType.parse("image/*"), file)
  518. );
  519. } catch (IOException e) {
  520. e.printStackTrace();
  521. }
  522.  
  523. FlowersApp.getApi().uploadPhoto(
  524. ((FlowersApp) context.getApplicationContext()).getBearerToken(),
  525. url,
  526. filePartA
  527. ).enqueue(new Callback<PhotoPostModel>() {
  528. @Override
  529. public void onResponse(@NonNull Call<PhotoPostModel> call, @NonNull Response<PhotoPostModel> response) {
  530. if (response.code() == 200) { // 200: OK
  531. Toast.makeText(context, "Photo uploaded " + String.valueOf(photoPos), Toast.LENGTH_SHORT).show();
  532. }
  533. }
  534.  
  535. @Override
  536. public void onFailure(@NonNull Call<PhotoPostModel> call, @NonNull Throwable t) {
  537. Toast.makeText(context, R.string.can_not_upload_photos, Toast.LENGTH_SHORT).show();
  538. t.printStackTrace();
  539. }
  540. });
  541. }
  542.  
  543. private void startPayment(BufferedOrderObject boo, int orderId) {
  544. int price = boo.getPrice();
  545. String description = "Payment for order with ID: " + orderId;
  546. String currency = "RUB";
  547. String extraData =
  548. "Name: " + ((FlowersApp) context.getApplicationContext()).getUser().getFirstName()
  549. + " Surname: " + ((FlowersApp) context.getApplicationContext()).getUser().getSecondName()
  550. + "Phone: " + boo.getCustomerPhone();
  551. String customerId = ((FlowersApp) context.getApplicationContext()).getUser().getUserId();
  552.  
  553. Intent paymentIntent = new Intent(context, PaymentWidget.class);
  554. paymentIntent.putExtra(PaymentWidget.EXTRA_AMOUNT, price); // Amount (price)
  555. paymentIntent.putExtra(PaymentWidget.EXTRA_DESCRIPTION, description); // Description
  556. paymentIntent.putExtra(PaymentWidget.EXTRA_CURRENCY, currency); // Currency code
  557. paymentIntent.putExtra(PaymentWidget.EXTRA_PUBLIC_ID, Constants.CLOUDPAYMENTS_PUBLIC_ID); // Public ID
  558. paymentIntent.putExtra(PaymentWidget.EXTRA_INVOICE_ID, orderId); // order ID
  559. paymentIntent.putExtra(PaymentWidget.EXTRA_ACCOUNT_ID, customerId); // customer ID
  560. paymentIntent.putExtra(PaymentWidget.EXTRA_DATA, extraData); // Extra data
  561. paymentIntent.putExtra(PaymentWidget.EXTRA_TYPE, PaymentWidget.TYPE_CHARGE); // TYPE_CHARGE (одностадийный); TYPE_AUTH (двухстадийный)
  562. PaymentWidget.taskListener = new PaymentTaskListener() {
  563. @Override
  564. public void success(BaseResponse baseResponse) {
  565. Toast.makeText(context, baseResponse.message, Toast.LENGTH_LONG).show();
  566. clearData();
  567. startHistoryFragment();
  568. }
  569.  
  570. @Override
  571. public void error(BaseResponse baseResponse) {
  572. Toast.makeText(context, baseResponse.message, Toast.LENGTH_LONG).show();
  573. }
  574.  
  575. @Override
  576. public void cancel() {
  577. Toast.makeText(context, R.string.payment_cancelled, Toast.LENGTH_SHORT).show();
  578. clearData();
  579. startHistoryFragment();
  580. }
  581. };
  582.  
  583. startActivity(paymentIntent);
  584. }
  585.  
  586. private void startHistoryFragment () {
  587. try {
  588. Fragment historyFragment = HistoryFragment.class.newInstance();
  589. activityExtFragmentResolver.setFragment(historyFragment, getString(R.string.history));
  590. } catch (Exception e) {
  591. e.printStackTrace();
  592. }
  593. }
  594.  
  595. private void clearData () {
  596. /* Setting up NULL to BufferedOrderObject */
  597. ((FlowersApp) context.getApplicationContext()).setBufferedOrderObject(null);
  598. }
  599.  
  600. @Override
  601. public void saveData() {
  602. BufferedOrderObject boo = ((FlowersApp) context.getApplicationContext()).getBufferedOrderObject();
  603. if (boo == null) return;
  604.  
  605. if (!cbDontKnowAddress.isChecked()) {
  606. boo.setDeliveryAddress(placeAddress + ", " + etFlat.getText().toString());
  607. if (etComment.getText().toString().trim().length() > 0) {
  608. boo.setDeliveryDetails(etComment.getText().toString());
  609. }
  610. }
  611. boo.setDeliveryTime(etDate.getText().toString() + " " + etTime.getText().toString());
  612. boo.setCustomerPhone(etCustomerPhone.getText().toString());
  613. if (etRecipientPhone.getText().toString().trim().length() > 0) {
  614. boo.setRecipientPhone(etRecipientPhone.getText().toString());
  615. }
  616.  
  617. }
  618.  
  619. @Override
  620. public boolean loadData() {
  621. boolean dataExist = false;
  622. BufferedOrderObject boo = ((FlowersApp) context.getApplicationContext()).getBufferedOrderObject();
  623. if (boo == null) return false;
  624. if (boo.getCustomerPhone() != null || boo.getRecipientPhone() != null || boo.getDeliveryAddress() != null) {
  625. dataExist = true;
  626. }
  627. if (boo.getDeliveryAddress() != null) {
  628. autocompleteFragment.setText(boo.getDeliveryAddress());
  629. }
  630. return dataExist;
  631. }
  632.  
  633. public void setActivityExtFragmentResolver(MainActivity.ExtFragmentResolver activityExtFragmentResolver) {
  634. this.activityExtFragmentResolver = activityExtFragmentResolver;
  635. }
  636.  
  637. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement