@@ -72,69 +72,93 @@ public function forcePay(Product $product): Transfer
7272
7373 /**
7474 * @param Product $product
75+ * @param bool $gifts
7576 * @return null|Transfer
7677 */
77- public function paid (Product $ product ): ?Transfer
78+ public function paid (Product $ product, bool $ gifts = false ): ?Transfer
7879 {
80+ $ status = [Transfer::STATUS_PAID ];
81+ if ($ gifts ) {
82+ $ status [] = Transfer::STATUS_GIFT ;
83+ }
84+
7985 /**
8086 * @var Model $product
87+ * @var Transfer $query
8188 */
82- return $ this ->transfers ()
89+ $ query = $ this ->transfers ();
90+ return $ query
8391 ->where ('to_type ' , $ product ->getMorphClass ())
8492 ->where ('to_id ' , $ product ->getKey ())
85- ->where ( ' refund ' , 0 )
93+ ->whereIn ( ' status ' , $ status )
8694 ->orderBy ('id ' , 'desc ' )
8795 ->first ();
8896 }
8997
9098 /**
9199 * @param Product $product
92100 * @param bool $force
101+ * @param bool $gifts
93102 * @return bool
94103 * @throws
95104 */
96- public function refund (Product $ product , bool $ force = false ): bool
105+ public function refund (Product $ product , bool $ force = false , bool $ gifts = false ): bool
97106 {
98- $ transfer = $ this ->paid ($ product );
107+ $ transfer = $ this ->paid ($ product, $ gifts );
99108
100109 if (!$ transfer ) {
101110 throw (new ModelNotFoundException ())
102111 ->setModel ($ this ->transfers ()->getMorphClass ());
103112 }
104113
105114 return DB ::transaction (function () use ($ product , $ transfer , $ force ) {
115+ $ transfer ->load ('withdraw.payable ' );
116+
106117 if ($ force ) {
107- $ product ->forceTransfer ($ this , $ transfer ->deposit ->amount , $ product ->getMetaProduct ());
118+ $ product ->forceTransfer (
119+ $ transfer ->withdraw ->payable ,
120+ $ transfer ->deposit ->amount ,
121+ $ product ->getMetaProduct ()
122+ );
108123 } else {
109- $ product ->transfer ($ this , $ transfer ->deposit ->amount , $ product ->getMetaProduct ());
124+ $ product ->transfer (
125+ $ transfer ->withdraw ->payable ,
126+ $ transfer ->deposit ->amount ,
127+ $ product ->getMetaProduct ()
128+ );
110129 }
111130
112- return $ transfer ->update (['refund ' => 1 ]);
131+ return $ transfer ->update ([
132+ 'status ' => Transfer::STATUS_REFUND ,
133+ 'status_last ' => $ transfer ->status ,
134+ ]);
113135 });
114136 }
115137
116138 /**
117139 * @param Product $product
118140 * @param bool $force
141+ * @param bool $gifts
119142 * @return bool
120143 */
121- public function safeRefund (Product $ product , bool $ force = false ): bool
144+ public function safeRefund (Product $ product , bool $ force = false , bool $ gifts = false ): bool
122145 {
123146 try {
124- return $ this ->refund ($ product , $ force );
147+ return $ this ->refund ($ product , $ force, $ gifts );
125148 } catch (\Throwable $ throwable ) {
126149 return false ;
127150 }
128151 }
129152
130153 /**
131154 * @param Product $product
155+ * @param bool $gifts
132156 * @return bool
133157 * @throws
134158 */
135- public function forceRefund (Product $ product ): bool
159+ public function forceRefund (Product $ product, bool $ gifts = false ): bool
136160 {
137- return $ this ->refund ($ product , true );
161+ return $ this ->refund ($ product , true , $ gifts );
138162 }
139163
140164}
0 commit comments