@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhortuneAccountSubscriptionAutopayController
4 extends PhortuneAccountController {
5
6 protected function shouldRequireAccountEditCapability() {
7 return true;
8 }
9
10 protected function handleAccountRequest(AphrontRequest $request) {
11 $viewer = $this->getViewer();
12 $account = $this->getAccount();
13
14 $subscription = id(new PhortuneSubscriptionQuery())
15 ->setViewer($viewer)
16 ->withIDs(array($request->getURIData('subscriptionID')))
17 ->withAccountPHIDs(array($account->getPHID()))
18 ->requireCapabilities(
19 array(
20 PhabricatorPolicyCapability::CAN_VIEW,
21 PhabricatorPolicyCapability::CAN_EDIT,
22 ))
23 ->executeOne();
24 if (!$subscription) {
25 return new Aphront404Response();
26 }
27
28 $method = id(new PhortunePaymentMethodQuery())
29 ->setViewer($viewer)
30 ->withIDs(array($request->getURIData('methodID')))
31 ->withAccountPHIDs(array($subscription->getAccountPHID()))
32 ->withMerchantPHIDs(array($subscription->getMerchantPHID()))
33 ->withStatuses(
34 array(
35 PhortunePaymentMethod::STATUS_ACTIVE,
36 ))
37 ->executeOne();
38 if (!$method) {
39 return new Aphront404Response();
40 }
41
42 $next_uri = $subscription->getURI();
43
44 $autopay_phid = $subscription->getDefaultPaymentMethodPHID();
45 $is_stop = ($autopay_phid === $method->getPHID());
46
47 if ($request->isFormOrHisecPost()) {
48 if ($is_stop) {
49 $new_phid = null;
50 } else {
51 $new_phid = $method->getPHID();
52 }
53
54 $xactions = array();
55
56 $xactions[] = $subscription->getApplicationTransactionTemplate()
57 ->setTransactionType(
58 PhortuneSubscriptionAutopayTransaction::TRANSACTIONTYPE)
59 ->setNewValue($new_phid);
60
61 $editor = $subscription->getApplicationTransactionEditor()
62 ->setActor($viewer)
63 ->setContentSourceFromRequest($request)
64 ->setContinueOnNoEffect(true)
65 ->setContinueOnMissingFields(true)
66 ->setCancelURI($next_uri);
67
68 $editor->applyTransactions($subscription, $xactions);
69
70 return id(new AphrontRedirectResponse())->setURI($next_uri);
71 }
72
73 $method_phid = $method->getPHID();
74 $subscription_phid = $subscription->getPHID();
75
76 $handles = $viewer->loadHandles(
77 array(
78 $method_phid,
79 $subscription_phid,
80 ));
81
82 $method_handle = $handles[$method_phid];
83 $subscription_handle = $handles[$subscription_phid];
84
85 $method_display = $method_handle->renderLink();
86 $method_display = phutil_tag(
87 'strong',
88 array(),
89 $method_display);
90
91 $subscription_display = $subscription_handle->renderLink();
92 $subscription_display = phutil_tag(
93 'strong',
94 array(),
95 $subscription_display);
96
97 $body = array();
98 if ($is_stop) {
99 $title = pht('Stop Autopay');
100
101 $body[] = pht(
102 'Remove %s as the automatic payment method for subscription %s?',
103 $method_display,
104 $subscription_display);
105
106 $body[] = pht(
107 'This payment method will no longer be charged automatically.');
108
109 $submit = pht('Stop Autopay');
110 } else {
111 $title = pht('Start Autopay');
112
113 $body[] = pht(
114 'Set %s as the automatic payment method for subscription %s?',
115 $method_display,
116 $subscription_display);
117
118 $body[] = pht(
119 'This payment method will be used to automatically pay future '.
120 'charges.');
121
122 $submit = pht('Start Autopay');
123 }
124
125 $dialog = $this->newDialog()
126 ->setTitle($title)
127 ->addCancelButton($next_uri)
128 ->addSubmitButton($submit);
129
130 foreach ($body as $graph) {
131 $dialog->appendParagraph($graph);
132 }
133
134 return $dialog;
135 }
136
137}