· 9 years ago · Nov 06, 2016, 03:22 AM
1<?
2 include_once "paypal_init.php";
3
4 use PayPal\Api\ChargeModel;
5 use PayPal\Api\Currency;
6 use PayPal\Api\MerchantPreferences;
7 use PayPal\Api\PaymentDefinition;
8 use PayPal\Api\Plan;
9
10
11 use PayPal\Api\Patch;
12 use PayPal\Api\PatchRequest;
13
14 use PayPal\Common\PayPalModel;
15
16
17class myBilling extends myPaypalInit
18{
19
20
21 function __construct($ClientID, $SecretKEY, $sandbox)
22 {
23 parent::__construct($ClientID, $SecretKEY, $sandbox);
24
25 }
26
27 public function create($val){
28
29 $plan = new Plan();
30 $plan->setName($val['name'])
31 ->setDescription($val['desc'])
32 ->setType($val['type']);
33
34#-----------------------------------------------------
35 $paymentDefinition = new PaymentDefinition();
36
37 $paymentDefinition->setName('Regular Payments')
38 ->setType('REGULAR')
39 ->setFrequency( $val['frequency'] )
40 ->setFrequencyInterval( $val['freauencyInterval'] )
41 ->setCycles("0")
42 ->setAmount(new Currency(array('value' => $val['price'], 'currency' => $val['currency'] )));
43
44 $chargeModel = new ChargeModel();
45 $chargeModel->setType('SHIPPING')
46 ->setAmount(new Currency(array('value' => 0, 'currency' => $val['currency'] )));
47
48 $paymentDefinition->setChargeModels(array($chargeModel));
49
50 $merchantPreferences = new MerchantPreferences();
51
52
53 $merchantPreferences->setReturnUrl( $this->returnURL )
54 ->setCancelUrl( $this->cancelURL )
55 ->setAutoBillAmount("yes")
56 ->setInitialFailAmountAction("CONTINUE")
57 ->setMaxFailAttempts("0")
58 ->setSetupFee(new Currency(array('value' => "0.00", 'currency' => $val['currency'])));
59
60#------------------------------------------------------
61
62
63 $paymentDefinition2 = new PaymentDefinition();
64
65 $paymentDefinition2->setName('Trial period')
66 ->setType('TRIAL')
67 ->setFrequency( "DAY" )
68 ->setFrequencyInterval( "1" )
69 ->setCycles("1")
70 ->setAmount(new Currency(array('value' => "33.00", 'currency' => $val['currency'] )));
71
72
73 $paymentDefinition2->setChargeModels(array($chargeModel));
74
75
76
77#------------------------------------------------------
78
79
80
81 $plan->setPaymentDefinitions(array($paymentDefinition2,$paymentDefinition));
82 $plan->setMerchantPreferences($merchantPreferences);
83
84 $request = clone $plan;
85
86 try {
87 $output = $plan->create($this->api);
88 } catch (Exception $ex) {
89 echo 'Error!';
90 var_dump($ex);
91 die(1);
92 }
93 $this->activate($output);
94 echo 'Success!';
95 var_dump($output);
96 }
97
98
99 public function activate($plan){
100 try {
101 $patch = new Patch();
102
103 $value = new PayPalModel('{
104 "state":"ACTIVE"
105 }');
106
107 $patch->setOp('replace')
108 ->setPath('/')
109 ->setValue($value);
110 $patchRequest = new PatchRequest();
111 $patchRequest->addPatch($patch);
112
113 $plan->update($patchRequest, $this->api);
114
115 $plan = Plan::get($plan->getId(), $this->api);
116 } catch (Exception $ex) {
117 var_dump($ex);
118 echo "ERROR!";
119 }
120 }
121
122 public function delete($plan)
123 {
124 try {
125 $result = $plan->delete($this->api);
126 } catch (Exception $ex) {
127 $this->render($ex);
128 }
129
130 }
131
132 public function getPlan($id){
133 try {
134 $plan = Plan::get($id, $this->api);
135 } catch (Exception $ex) {
136 $this->render($ex);
137 }
138
139 return $plan;
140 }
141
142 /**
143 * Get all plans
144 */
145 public function get_all($status = 'active'){
146 try
147 {
148 $params = array('page_size' => '12', 'status' => $status);
149 $planList = Plan::all($params, $this->api);
150 }
151 catch (Exception $ex) {
152 echo 'Error!';
153 var_dump($ex);
154 }
155
156 return $planList->plans;
157 }
158
159 public function render($var){
160 parent::render($var);
161 }
162
163}
164
165 $b = new myBilling('secret','key', true);
166
167
168 $arr['name']="My test plan";
169 $arr['desc'] ='description';
170 $arr['type']='INFINITE';
171 $arr['frequency']='DAY';
172 $arr['freauencyInterval']='1';
173 $arr['price']='10';
174 $arr['currency']='USD';
175 $arr['setupfee']='0';
176
177
178 $res = $b->create( $arr);
179
180 $b->render($res);
181
182
183
184?>