MDL-69166 core_payment: add payment accounts
[moodle.git] / payment / gateway / paypal / classes / external / transaction_complete.php
CommitLineData
4403b8f6
SR
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This class contains a list of webservice functions related to the PayPal payment gateway.
19 *
20 * @package pg_paypal
21 * @copyright 2020 Shamim Rezaie <shamim@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25declare(strict_types=1);
26
27namespace pg_paypal\external;
28
895f38cc 29use core_payment\helper;
4403b8f6
SR
30use external_api;
31use external_function_parameters;
32use external_value;
048a6521
SR
33use core_payment\helper as payment_helper;
34use pg_paypal\paypal_helper;
4403b8f6
SR
35
36defined('MOODLE_INTERNAL') || die();
37
38require_once($CFG->libdir . '/externallib.php');
39
40class transaction_complete extends external_api {
41
42 /**
43 * Returns description of method parameters.
44 *
45 * @return external_function_parameters
46 */
47 public static function execute_parameters() {
48 return new external_function_parameters([
49 'component' => new external_value(PARAM_COMPONENT, 'The component name'),
50 'componentid' => new external_value(PARAM_INT, 'The item id in the context of the component'),
51 'orderid' => new external_value(PARAM_TEXT, 'The order id coming back from PayPal'),
4403b8f6
SR
52 ]);
53 }
54
55 /**
56 * Perform what needs to be done when a transaction is reported to be complete.
57 * This function does not take cost as a parameter as we cannot rely on any provided value.
58 *
59 * @param string $component Name of the component that the componentid belongs to
60 * @param int $componentid An internal identifier that is used by the component
61 * @param string $orderid PayPal order ID
4403b8f6
SR
62 * @return array
63 */
048a6521 64 public static function execute(string $component, int $componentid, string $orderid): array {
4403b8f6
SR
65 global $USER, $DB;
66
67 self::validate_parameters(self::execute_parameters(), [
68 'component' => $component,
69 'componentid' => $componentid,
70 'orderid' => $orderid,
4403b8f6
SR
71 ]);
72
895f38cc 73 $config = (object)helper::get_gateway_configuration($component, $componentid, 'paypal');
bbb887f6 74 $sandbox = $config->environment == 'sandbox';
4403b8f6
SR
75
76 [
77 'amount' => $amount,
895f38cc
MG
78 'currency' => $currency,
79 'accountid' => $accountid,
048a6521 80 ] = payment_helper::get_cost($component, $componentid);
4403b8f6 81
1882bb47
SR
82 // Add surcharge if there is any.
83 if ($config->surcharge) {
84 $amount += $amount * $config->surcharge / 100;
85 }
86
048a6521
SR
87 $paypalhelper = new paypal_helper($config->clientid, $config->secret, $sandbox);
88 $orderdetails = $paypalhelper->get_order_details($orderid);
4403b8f6
SR
89
90 $success = false;
91 $message = '';
92
048a6521
SR
93 if ($orderdetails) {
94 if ($orderdetails['status'] == paypal_helper::ORDER_STATUS_APPROVED &&
95 $orderdetails['intent'] == paypal_helper::ORDER_INTENT_CAPTURE) {
96 $item = $orderdetails['purchase_units'][0];
97 if ($item['amount']['value'] == $amount && $item['amount']['currency_code'] == $currency) {
98 $capture = $paypalhelper->capture_order($orderid);
99 if ($capture && $capture['status'] == paypal_helper::CAPTURE_STATUS_COMPLETED) {
100 $success = true;
101 // Everything is correct. Let's give them what they paid for.
102 try {
103 payment_helper::deliver_order($component, $componentid);
104
105 $paymentid = payment_helper::save_payment($component, $componentid, (int) $USER->id, $amount,
106 $currency, 'paypal');
107
108 // Store PayPal extra information.
109 $record = new \stdClass();
110 $record->paymentid = $paymentid;
111 $record->pp_orderid = $orderid;
112
113 $DB->insert_record('pg_paypal', $record);
114 } catch (\Exception $e) {
115 debugging('Exception while trying to process payment: ' . $e->getMessage(), DEBUG_DEVELOPER);
116 $success = false;
117 $message = get_string('internalerror', 'pg_paypal');
118 }
119 } else {
4403b8f6 120 $success = false;
048a6521 121 $message = get_string('paymentnotcleared', 'pg_paypal');
4403b8f6 122 }
048a6521 123 } else {
4403b8f6 124 $success = false;
048a6521
SR
125 $message = get_string('amountmismatch', 'pg_paypal');
126 }
127 } else {
128 $success = false;
129 $message = get_string('paymentnotcleared', 'pg_paypal');
4403b8f6
SR
130 }
131 } else {
132 // Could not capture authorization!
133 $success = false;
048a6521 134 $message = get_string('cannotfetchorderdatails', 'pg_paypal');
4403b8f6
SR
135 }
136
137 return [
138 'success' => $success,
139 'message' => $message,
140 ];
141 }
142
143 /**
144 * Returns description of method result value.
145 *
146 * @return external_function_parameters
147 */
148 public static function execute_returns() {
149 return new external_function_parameters([
150 'success' => new external_value(PARAM_BOOL, 'Whether everything was successful or not.'),
151 'message' => new external_value(PARAM_TEXT, 'Message (usually the error message).', VALUE_OPTIONAL),
152 ]);
153 }
154}